Create a fixed left column in Bootstrap that stays in place until the footer comes into view

I'm currently working on adding a left-hand column to my website that will stick in place when scrolling down, then switch to an absolute position at the bottom once it reaches the footer. As the user scrolls back up, the column should become fixed again while still allowing them to scroll to the top of the column.

The behavior I am aiming for is demonstrated on this page: .

When the page is scrolled, the left column remains fixed but moves along with the scroll until reaching the bottom. At that point, it changes to an absolute position. When scrolling back up, the column becomes fixed once more, allowing users to easily reach the top.

I have created a JSFiddle to show my progress so far: https://jsfiddle.net/peLwk1cs/2/. If anyone can offer assistance in achieving the Google-like functionality, I would greatly appreciate it.

Answer №1

To make things easier, you could simply utilize the sticky-top class without requiring any JavaScript...

<header>HEADER</header>
<div class="container">
    <div class="row">
        <div id="filtersCol" class="col col-4">
            <div class="filterContainer sticky-top"> FILTER COL </div>
        </div>
        <div id="resultsCol" class="col col-8"> RESULTS COL </div>
    </div>
</div>
<footer>FOOTER</footer>

Check out the demonstration here:

If you want to keep the header fixed, you can adjust for its height by adding the following CSS...

body {
    padding-top: 100px;
}

.sticky-top {
    top: 100px;
}

Answer №2

To demonstrate the desired behavior, you can refer to this JSFiddle example:

http://jsfiddle.net/bN4qu/5/

Essentially, when the user scrolls down, the left column should stick when it reaches the bottom. The use of 'sticky-top' keeps the left column at the top, which is not the intended outcome here.

Conversely, when the user scrolls up, the left column should correctly stick to the top.

The provided JSFiddle uses CoffeeScript, but please note that our project does not incorporate this language. Additionally, we are utilizing a more recent version of JQuery.

<div id="header">HEADER</div>
<div id="content">
  <div class="side" id="stick">
    TOP OF LEFT COLUMN
    <div class="bottom">
      BOTTOM OF LEFT COLUMN
    </div>
  </div>
  <div class="main">MAIN CONTENT</div>
</div>
<div id="footer">FOOTER</div>

body {
   margin: 0;
   padding: 10px;
}
#header {
    height: 100px;
    margin: 0 0 10px;
    background: red;
}
#content {
    position: relative;
    float: left;
    width: 100%;
    height: auto;
    margin: 0;
}
.side {
    float: left;
    width: 100px;
    height: 900px;
    margin: 0;
    background: linear-gradient(red, yellow);
}

.bottom {
  position: absolute;
  bottom: 0;
}

.main {
    height: 2500px;
    margin: 0;
    background: lightgray;
}
#footer {
    clear: both;
    height: 300px;
    background: orange;
}


$.fn.stickyTopBottom = (options = {}) ->
    # compatible with browsers supporting CSS3 transforms (e.g., IE9+)

    ## ##############
    #initialization
    options = $.extend  
      container: $('body') #reference element for starting and stopping the sticking 
(doesn't have to contain the element)
      top_offset: 0 #distance from viewport top to stick-top of element
      bottom_offset: 0 #distance from viewport bottom to stick-bottom of element
    , options

    $el = $(this)

     #Obtain the reference element's top. If the container moves, adjust this within the scroll handler. 
    #If there's Y translation in the container, this method may fail.
    container_top = options.container.offset().top 
    element_top = $el.offset().top

    viewport_height = $(window).height()
    $(window).on 'resize', -> 
      viewport_height = $(window).height()


     ## #################
    # Scroll handler
    #
    # Scrolling up or shorter element:
    #    if scrolled above element's top, position top of element to top of viewport
    #      (stick to top)
    # Scrolling down: 
    #    if scrolled past element's bottom, place bottom of element at viewport bottom
    #      (stick to bottom)

    current_translate = 0
    last_viewport_top = document.documentElement.scrollTop || document.body.scrollTop
    $(window).scroll (event) ->
      viewport_top = document.documentElement.scrollTop || document.body.scrollTop
      viewport_bottom = viewport_top + viewport_height
  effective_viewport_top = viewport_top + options.top_offset
  effective_viewport_bottom = viewport_bottom - options.bottom_offset
  
  ...
  ... // Please refer to actual code for remaining content
  ...

 $('#stick').stickyTopBottom
  container: $('#content')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Bootstrap to Position DIVs at the Top, Middle, and Bottom

I'm looking to create a layout with three DIVs inside a container DIV, all centered horizontally. The first DIV should be aligned at the top of the container, the second in the vertical center, and the third at the bottom. While I found some helpful t ...

How can I integrate the jQuery Plugin Mapael with Angular 5?

While exploring various methods that tackled the issue of integrating jQuery Plugins, I decided to start with the fundamentals. To begin with, I installed the jQuery plugin: npm i jquery Next, I included the TypeScript definition: npm install -d @types ...

What is the secret to remaining on the same spot even after the page is reloaded?

How can I maintain the AJAX call data after a page reload? I have attempted to use code that reloads the entire page, but I would like to stay in the same location. Is there a way to achieve this without reloading the entire page? $('#updateAddressPr ...

Utilizing Bootstrap 4's SASS with ASP.NET5

I'm having trouble integrating Bootstrap 4 alpha SASS version with my ASP.NET 5 app. The installation process seems complex, especially with the Grunt file creating CSS and JS files. It's not clear how to streamline the workflow. My minimum goal ...

Adjust CSS to properly position links alongside details summary

Currently enhancing my Hugo site (repository link is here). I am aiming to integrate a details element alongside anchors, maintaining a consistent style. The desired layout is depicted in the initial screenshot where "abstract" serves as the details elemen ...

The headline box is displaying CSS code instead of the content. How can this issue be resolved?

Having an issue with a WordPress template that features a blue "headline" box that I need to change the color of to #333399. I tried inspecting the element, browsing through the code, and identified the code below as what needed modification: #headline, # ...

Center text within a dropdown using Bootstrap styling

In a row within my inline form, I have both an email input and a dropdown. However, after resizing the page, the text within the dropdown is no longer positioned correctly (text on the left, arrow on the right). I attempted to fix this by using: text-alig ...

The quantity addition and subtraction function is malfunctioning

I have implemented a quantity plus/minus feature in HTML which is embedded from a jQuery script provided below. jQuery.ajax({ type: "POST", url: "http://ayambrand-com-my-v1.cloudaccess.host/index.php?option=com_echa ...

Experiencing difficulties with the alignment of Bootstrap columns

I'm encountering an issue with displaying 3 Bootstrap columns in the ContentArea. Even though I can locate them using Developer tools, they seem to be positioned at the bottom of the ContentArea, making them not visible. I've attempted adjusting ...

Combining Jquery and PHP to Implement Date Type in MySQL Database

I am looking for a way to save user birthdates in MySQL as a date type on my website. The data I receive includes: Day - e.g. 1 or 11, 23, etc. Month - e.g. March, April, December Year - e.g. 1975, 1964, 1944, etc. For example: 3 March 1966 How can I c ...

Creating a new web application, I require a loading overlay to appear during transitions between pages

I've checked high and low, but I can't seem to find the solution! My webapp has a page that is bogged down with data causing it to load slowly. Is there a way to display a loading div while transitioning to the next page? Perhaps something like ...

What is the best way to guide the user to a different page with PHP once the preventDefault() function in jQuery has been utilized on a

My approach to form validation involves using jQuery, AJAX, and PHP on my website. I utilize PHP for the actual input validation process as I believe it is more secure and prevents users from manipulating scripts through browser source code inspection. Add ...

What is the best way to improve the design of this division that includes buttons?

I'm having trouble with the layout of three divs I have set up. One acts as a container, while the other two are meant to hold buttons. However, something seems off and I can't figure out how to correct it. .button { display: inline-block; ...

Creating a versatile Google line chart with multiple series that have varying datetime values

I am faced with the challenge of handling an unspecified number of data sets within a JSON feed. The uncertainty lies in not knowing how many datasets will return - it could be one, twelve, thirty, or any other number. Each dataset consists of a date and a ...

Showing one column fixed to the left and the other column scrollable to the right using CSS in Bootstrap 4

I have a bootstrap row with 2 cols inside. One col needs to be sticky on the left side, while the other col on the right side should contain multiple scrollable cards with text content. https://i.sstatic.net/ysYlP.png Looking at the image provided, the " ...

Click on the hyperlinks one by one that trigger ajax events

There is a feature on the popular social media platform reddit.com where you can load more comments by clicking a link. I understand that comments may be hidden for performance reasons, but I would like to automatically expand all comments without having t ...

Is there a way to create a sequence where text fades in only after the previous text has completely faded out?

Currently, I am using JQuery to create a continuous slideshow of text values. However, after some time, multiple texts start to display simultaneously, indicating a timing issue. Even when hidden, the problem persists. My code includes 3 strings of text. ...

I'm struggling to achieve the placement of my logo and navigation bar side by side

Check out the codes here body{ margin: 0; } .logo { text-indent: -999999px; background: url('logo3.png'); width: 216px; height: 219px; } .header{ width: 100%; height: auto; background-color: #eef3f5; padd ...

Run a Python function in Django without any feedback

Currently, I am utilizing Django for a project and I find myself in a situation where I need to carry out certain functions based on user input. If the action requires displaying new values, I know how to handle that. However, when I simply need to execute ...

Utilize the Google Maps API to transmit the latest GPS coordinates to an mySQL database

I am currently working on developing a golf application in jQuery that will allow me to send the GPS coordinates to a MySQL database with the click of a button. As I am new to PHP, I am struggling to find the appropriate code to achieve this. I have set u ...