What is the best way to make an inner div fixed once I reach the bottom of the page?

I'm facing a challenge with my nested div setup: I want the inner div to be absolutely positioned, but when scrolling to the bottom it should switch to fixed positioning. Currently, it's set up as a sidebar, but I'd like it to scroll along with the middle content until reaching the bottom of the div, at which point it should become fixed.

Any suggestions?

I've been trying to tweak this code without success:

 
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
    <script type="text/javascript">
        $(function() {
            var offset = $("#right_side_bar").offset();
            var topPadding = 15;
            $(window).scroll(function() {
                if ($(window).scrollTop() > offset.top) {
                document.getElementById("#right_side_bar").style.position = 'fixed';

                } else {

                };
            });
        });
    </script>

Image: https://i.sstatic.net/S2Nbi.png

The above code isn't doing the trick, so here's more context on the issue. I have a large container div and a right sidebar named "right_side_bar." The sidebar contains more content than what is currently visible, so I want it to scroll along with the container until all content is displayed in the sidebar (meaning the user has reached the bottom), at which point it should stop scrolling and remain fixed. If the user scrolls back to the top, it should go back to being absolute. Let me know if that makes sense!

https://i.sstatic.net/S2Nbi.png

  
#right_side_bar {
position:absolute;
margin-top:38px;
    width:272px;
    margin-left:722px;
    background-color:#FFF; /* D6E6F7 */
    height:100%;
    overflow:scroll;
    z-index: 0;
}

#container{

    width: 994px;
    /*height: 885px;*/

    background-color: #D6E6F7;
    font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
    /*padding-bottom: 30px;*/
}

Answer №1

After understanding your request, I have created an outer div with the ID #outer and an inner div with the ID #inner. It seems that you want the inner div to remain fixed once it reaches the bottom after scrolling.

I have attempted to fulfill your requirement in its basic form (although I may not fully comprehend the question).

Here is an example: http://jsfiddle.net/eQtrG/12/

HTML:

<div id="container">
 <!-- LOTS OF CONTENT -->
    <div id="right_side_bar">
      <!-- LOTS OF CONTENT -->
    </div>
 <!-- LOTS OF CONTENT -->
</div>

CSS:

#container { position: absolute; width: 100%; background: #CCC; }
#right_side_bar {position: absolute;width:100%; height: 100px; background: #FF0000; overflow-x: hidden; overflow-y scroll; margin:0px;z-index: 9999; top: 250px; }

​ jQuery:

$("#container").scroll(function(){
  $("#right_side_bar").scrollTop($("#container").scrollTop());
});

$('#right_side_bar').bind('scroll', function() {
 if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
     $(this).css({position:'fixed', top: '60px'})
 }
         else
         {
         $(this).css({position:'absolute', top: '250px'})
         }
});
​

​ ​ ​

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

Tips for adding animation to the div instead of the content

I have implemented a hover animation to animate the div, but unfortunately, when I added the animation to #i :hover {}, it ended up animating the words only. Moreover, the cursor transforms into a pointer solely when hovering over the words instead of the ...

Issue: attempting to write after the end of a node

Currently, I am using the stream() function from https://www.npmjs.com/package/cassandra-driver to read data from Cassandra. I am listening for events and piping the stream to the response object; however, I encountered an error: Error: write after end T ...

A Comprehensive Guide: Obtaining the Final Tab from a JSON using API

What is the method to extract the last tab from a given JSON code? { "claimed_levels": { "level_1", "level_2" } } I want to display the level when someone types "!levels". The desired output format should be: Your current level is "2" ...

What is the best way to compare an array with comma-separated values in JavaScript?

I have a scenario where I have two arrays, one for categories and the other for products. Each product contains multiple categories as a comma-separated string. My goal is to match a specific category with each product's product_category value and the ...

Function invoking React Hook

I'm a beginner to React JS and I've been learning by working on a project. I was creating a basic e-commerce UI with items and attempting to add items to a cart, but encountered an issue. The React Hook "useStateValue" is being called in the fun ...

Is there a way to automatically mute the sound on a parallax website homepage video when I navigate back to the homepage from another page?

Is there a way to automatically mute the sound of the homepage video on a parallax website when navigating back to it from a separate page? If I click on the enter button and navigate to the "Our DNA" page, then click on the events link in the menu from t ...

Unveiling the Power of Source-Maps: A Guide to Debugging Local NPM Dependencies Using `npm link`

Currently, I am utilizing babel-cli to compile the source code of my local NPM dependency. This is how my package.json file looks like: "main": "lib/index.js", "scripts": { "dev": "babel src --watch -d lib --source-maps inline", }, My other applicat ...

Dealing with a JavaScript Problem on Wordpress Using AJAX

Struggling with transitioning a website from Drupal to WordPress, I encountered an issue with a page that utilizes AJAX. A user followed a tutorial on implementing AJAX using JavaScript, PHP, and MySQL. Even though the AJAX functionality works fine on Drup ...

Automatically adapt the height of a bootstrap button based on the dimensions of other bootstrap buttons

First and foremost, my objective is to centrally align a glyphicon both vertically and horizontally within a button. Despite attempting CSS attributes like position: relative; and position: absolute;, my efforts have been in vain. You can check out the sam ...

Creating an arrow dialog box in Material UI: A step-by-step guide

I have successfully created an arrow box (div) using custom CSS and HTML. However, I am facing difficulty in implementing the same design in Material UI with React JS. The code below demonstrates how to achieve this using custom CSS. My question is: How ...

Copying a Pinia state Array without referencing it is not possible

My goal is to duplicate an array within a Pinia store so that I can use it to create a reactive object in various components for input modeling. The intention is to only update the original array if the user decides to save any changes made using the copy ...

Using JavaScript to dynamically insert HTML content and create a toggle effect

Within a div, I have a collection of large images that I am attempting to insert into another div using JavaScript toggle functionality. Please test the code snippet provided below. $(".toggleimages").on("click", function(e) { e.preventDefault(); ...

Attempting to show a div when a radio button is clicked

Could someone assist me in implementing a feature to hide/show a div when a radio button is selected? Here is the JavaScript code I currently have: function showOptions(id) { document.getElementById(id).style.display="block"; } I am working on a PHP ...

Deleting an item from an array in React Native when its ID already exists

In my React Native application, I have implemented a feature where users can select tags from a list. When they click on a tag, it is added to an array of item IDs. If the user clicks on a tag that is already in the array, I want to remove it. Currently, ...

When a row is clicked, retrieve the data specific to that row

I have implemented a data-grid using react-table where I pass necessary props to a separate component for rendering the grid. My issue is that when I click on a particular row, I am unable to retrieve the information related to that row using getTrProps. ...

There seems to be a connection issue between my Jquery and HTML, as they

I've hit a roadblock because my jQuery isn't connecting and I can't seem to figure out why. It's been stumping me for hours. Here is the HTML code snippet from exercise6.html: <!DOCTYPE html> <html lang="en> <h ...

Hiding overflow when navigating back a page in Safari

I need help finding a solution to this issue. On my website, the results page works perfectly fine until I navigate to a product's details page and then return to the results using the "page before" button. At that point, the overflow becomes hidden, ...

Display an image from a Google image search when hovering over a specified data attribute

My goal is to add a code snippet that will assign a class of "hoverme" to a table cell. When a user hovers over that cell, an ajax query will be triggered using the data attribute containing information stored there to perform a Google search. Below is a ...

Protractor Error Listings

In order to improve error handling in my Protractor tests, I am exploring how to handle exceptions such as No element found using locator: and provide more informative error messages. viewCompanyDocumentPage.getAttachmentType().then(function (type) { ...

How can I use Selenium and Python to locate an element on a webpage and extract its input value?

I'm currently working on a specific part of the website and I'm trying to extract the value from . I know how to access the span class using best_offer = driver.find_element_by_class_name('showBestOffer') in Selenium, but I'm wonde ...