Is there a way to make my sticky sidebar automatically scroll back up when the page reaches the footer?

I'm facing a common issue on StackOverflow, but the solutions I've come across so far haven't completely resolved my problem. To understand the issue better, you can take a look at this fiddle that I found referenced here. My challenge lies in applying this solution to my HTML structure, which is different from the one shown below:

This is how my HTML section appears:

        <div id="cms-menu" data-pagename="FAQS">
            Header html
        </div>

        <section class="help-centre xs-pt-20 xs-pb-20">
            <div class="container-fluid left-container-padding right-container-padding">
                <div class="row">
                    <div class="col-xs-12 text-left">
                        <h2>Help Centre</h2>
                        <p>Search or browse our frequently asked questions below</p>
                    </div>
                </div>
            </div>
        </section>

        ... (HTML content continues)
        

As the page scrolls towards the footer, my sticky sidebar disappears (similar to the scenario depicted in this fiddle linked above). How can I make it stick to the page as it reaches the footer? Given that I'm utilizing Umbraco and unable to modify the existing HTML structure, I seek guidance on how to handle this with CSS expertise beyond my reach. Below is the relevant CSS styles:

... (CSS styling content excluded for brevity)

Note: The dynamically generated content sections (A to I) may expand in the future.

SOLUTION
An additional div has been inserted before

<nav id="nav-categories-scroll">

Subsequently, the JavaScript code was updated as displayed below:

$(document).scroll(function (e) {

    var window_top = $(window).scrollTop();
    var footer_top = $("#footer").offset().top;
    var div_top = $('#sticky-anchor').offset().top;
    
    ...  (JavaScript logic continued)
});

Answer №1

Here's how I solved it: change the #sticky positioning to absolute when it reaches the footer.

function adjust_sticky_position() {
    var window_top = $(window).scrollTop();
    var footer_top = $("#footer").offset().top;
    var sticky_top = $('#sticky-anchor').offset().top;
    var sticky_height = $("#sticky").height();
    
    if (window_top + sticky_height > footer_top) {
        $('#sticky').removeClass('stick');   
        $('#sticky').addClass('absolute');
    } 
    else if (window_top > sticky_top) {
        $('#sticky').removeClass('absolute');
        $('#sticky').addClass('stick');
    } else {
        $('#sticky').removeClass('stick');
        $('#sticky').removeClass('absolute');
    }
}

$(function () {
    $(window).scroll(adjust_sticky_position);
    adjust_sticky_position();
});
#sticky {
    padding: 0.5ex;
    width: 200px;
    height:300px;
    background-color: #333;
    color: #fff;
    font-size: 2em;
    border-radius: 0.5ex;
    float:left;
}
#sticky.stick {
    position: fixed;
    top: 0;
    z-index: 10;
    border-radius: 0 0 0.5em 0.5em;
}
.content-holder {
  margin-left:225px;
}
.absolute {
  position: absolute;
  bottom: 600px;
}
body {
    margin: 1em;
    position: relative
}
p {
    margin: 1em auto;
}
#footer {
  width:100%;
  height:600px;
  background:#ccc;
  z-index:999999
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Created for my post: <a href="http://blog.yjl.im/2010/01/stick-div-at-top-after-scrolling.html">Stick div at top after scrolling</a>.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit....

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

The presence of an iframe disrupts the order of the

I have a website () that utilizes CSS to keep the header, menu, and footer fixed while allowing the content to scroll. Recently, I added a photo page using an iframe and it seems to be working well (although some adjustments are still needed for different ...

Problem arising from implementing background and relative positioning in html and css

Encountering an issue with html and css. Today, attempted to create a page with a large background image for the first time. Utilizing the foundation framework, but facing a challenge. Foundation is an adaptive framework, and when resizing the browser, a ...

Mutation observer fails to observe an element if it is housed within a function

I am attempting to initialize the liveChatAvailable value as true and set the isLoading value to false once the crispClient element loads onto the page. However, when the observer object is placed within a function, the if (crispClient) code does not exec ...

Looping Angular Components are executed

I am currently developing an Angular application and encountering an issue with my navbar getting looped. The problem arises when I navigate to the /home route, causing the navbar.component.html components to duplicate and appear stacked on top of each oth ...

Generating a string indicating the range of days chosen

Imagine a scenario where there is a selection of days available to the user (they can choose multiple). The list includes Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, each with an associated number from 0 to 6. For instance, Sunday ...

Exploring Ngu-Carousel in Angular 7: Importing JSON data for a dynamic display

After attempting to import data from JSON and display it using ngu-carousel, I encountered an error. The error shows "length of undefined" Subsequently, when I try to click on the previous/next button, another error appears. This error states "innerHTML ...

Tips for creating a radio button that appears consistent across Firefox, Chrome, and IE11

I am looking to create a set of Radio buttons that will display consistently across Chrome, Firefox, and IE 11. My current solution appears to work well in Firefox, but in Chrome, there is a blue box around the buttons, and in IE 11, it seems that the colo ...

Implementing Ideone API functionality on Codeigniter with the use of ajax, javascript, and soapclient

I am new to using Codeigniter and I apologize if my question is basic. I found some code on this site: Working with IDE One API (Full project code available here) and I'm attempting to incorporate it into Codeigniter. I have been able to get it worki ...

A gathering focused on the pause function of a bootstrap carousel

After carefully reviewing the documentation, it seems there isn't a specific event to detect when the carousel is paused. Is there a workaround for detecting when the bootstrap carousel is paused? The carousel can be paused using various methods, and ...

Scrolling inside a DIV with fixed positioning

Can you help me with a question? I've been using the jScrollPane plugin and I absolutely love it. However, I'm struggling to retrieve the top position of a div on the page while scrolling inside a jscrollpaned DIV. Using $(window).scroll(); works ...

Error encountered: AXIOS request failed due to XSRF-TOKEN Mismatch

My server is receiving different X-XSRF-TOKEN headers compared to the cookies being sent when I make requests 2-3 times per second using axios. let axiosInstance = axios.create({ baseUrl: "MY_BASE_URL_HERE", timeout: 20000 } ...

The deployed app on Heroku is showing a 304 status code with no JSON response

I've been working on a project using React, Express, and MongoDB. While everything works fine locally, I encountered an issue with GET requests after deploying the project on heroku.com. Instead of receiving the expected JSON response, I'm gett ...

Using Angular to include a forward slash "/" in the text input for a date field

Hello everyone, I am a newcomer to AngularJS and I am looking to insert slashes in an input type text element. I prefer not to rely on external packages like angular-ui or input type Date. My goal is to have the format mm/dd/yyyy automatically applied as ...

Unwanted transparency issue in MaterialUI (MUI) BottomNavigation

Greetings fellow hobby developer! I recently embarked on a project using create-react-app and incorporated MUI dependencies. One feature I added was a fixed BottomNavigation, which you can view in action here. Interestingly, in CodeSandbox, the BottomNavi ...

Determining if an element corresponds to a By-object in Selenium using JavaScript

Is it possible to determine if a given WebElement matches a specific By object? The function should return either true or false. For example: foo(myWebElement, By.className("myClass myClass2")); foo(myWebElement, By.css("div")); foo(my ...

The database is failing to reflect any changes even after using the db.insert function

I am currently working on implementing a forgot password feature in my express app using the node mailer package. This functionality involves sending a new password to the user's email and then updating the database with the new password. However, I h ...

Python: When scraping a table, only the tbody is returned without any content

Seeking to extract data from a table on this site: . Attempting to parse the HTML content of the table results in an empty body, as shown below: <thead> <tr> <th width="4%"> <div class="flex">#</div> </t ...

Error: Timthumb is redirecting to a 404 page when trying to load image

Timthumb has been working perfectly on my live site, but suddenly stopped working on localhost. The image source is written as follows... Although the image opens when I click and open in a new tab, it does not display on the current page... <img src= ...

Tips for creating a blur effect on all options except for the selected 2 out of 4 using jQuery

My goal is to choose 3 options from a list and once 3 options are selected, all other options will be blurred to prevent further selection. Please review the code snippet I have provided below. Link to File <!DOCTYPE html> <html> <head> ...

Authenticating child in Azure JWT token using the kid value hardcoded

As I obtained an Azure token, I decided to verify it by checking the kid in the header. I decoded the token on jwt.io and hardcoded the kid into my code for future tokens. However, after a few days, the public keys were updated, rendering the previous kid ...