Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:04:37+00:00 2026-05-28T08:04:37+00:00

I have a #sidebar (which starts below my #header div) and a #footer (around

  • 0

I have a #sidebar (which starts below my #header div) and a #footer (around 120px off the bottom of the page).

I’m trying to make the sidebar scroll with the content of the page. The code below does this semi-successfully:

/* profile sidebar */
#sidebar>div{ width: 300px; margin-top: 10px; }
#sidebar.fixed>div{position:fixed;top:0;}
#sidebar.fixed_bottom>div{position:fixed;bottom:172px;}

jQuery(function ($) {
    $.fn.scrollBottom = function() { 
        return $(document).height() - this.scrollTop() - this.height(); 
    };

    var el = $('#sidebar'),
    pos = el.position().top;

    $(window).scroll(function() {
        if ($(this).scrollTop() >= pos) {
            if ( $(this).scrollBottom() <= 172 ) { 
                el.removeClass('fixed');
                el.addClass('fixed_bottom');
            } else { 
                el.removeClass('fixed_bottom');
                el.addClass('fixed');
            }
        } else {
            el.removeClass('fixed');
        }
    });
});

The problem is, on smaller resolutions, this makes the sidebar “jump” once you reach a certain position on the page. It stops it from overlapping the footer (which is the problem if you remove the fixed_bottom class) but doesn’t look good.

What I’d like to do is this: user scrolls to the bottom of the page, the sidebar scrolls along with the content until it reaches say 20px above the top of my footer, at which point it stays there until the user scrolls back up.

Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:04:37+00:00Added an answer on May 28, 2026 at 8:04 am

    I believe this should do what you want.

    http://jsfiddle.net/FDv2J/3/

    #sidebar>div{ width: 100px; margin-top: 10px; position:fixed; left: 0; top: 0;}
    

    $(function() {
        $.fn.scrollBottom = function() {
            return $(document).height() - this.scrollTop() - this.height();
        };
    
        var $el = $('#sidebar>div');
        var $window = $(window);
    
        $window.bind("scroll resize", function() {
            var gap = $window.height() - $el.height() - 10;
            var visibleFoot = 172 - $window.scrollBottom();
            var scrollTop = $window.scrollTop()
    
            if(scrollTop < 172 + 10){
                $el.css({
                    top: (172 - scrollTop) + "px",
                    bottom: "auto"
                });
            }else if (visibleFoot > gap) {
                $el.css({
                    top: "auto",
                    bottom: visibleFoot + "px"
                });
            } else {
                $el.css({
                    top: 0,
                    bottom: "auto"
                });
            }
        });
    });
    

    I tried to break things up and name variables in such a way that it would be understandable. Let me know if there’s anything you’re unsure of. Notice that I added resize as well as scroll since it matters if the window changes size.

    EDIT: Modified version using similar technique to the original to find the upper bound:

    http://jsfiddle.net/FDv2J/4/

    $(function() {
      $.fn.scrollBottom = function() {
        return $(document).height() - this.scrollTop() - this.height();
      };
    
      var $el = $('#sidebar>div');
      var $window = $(window);
      var top = $el.parent().position().top;
    
      $window.bind("scroll resize", function() {
        var gap = $window.height() - $el.height() - 10;
        var visibleFoot = 172 - $window.scrollBottom();
        var scrollTop = $window.scrollTop()
    
        if (scrollTop < top + 10) {
          $el.css({
            top: (top - scrollTop) + "px",
            bottom: "auto"
          });
        } else if (visibleFoot > gap) {
          $el.css({
            top: "auto",
            bottom: visibleFoot + "px"
          });
        } else {
          $el.css({
            top: 0,
            bottom: "auto"
          });
        }
      }).scroll();
    });
    body{
      margin: 0;
    }
    #sidebar>div {
      width: 100px;
      height: 300px;
      margin-top: 10px;
      background-color: blue;
      position: fixed;
    }
    #stuff {
      height: 1000px;
      width: 300px;
      background-image: url("http://placekitten.com/100/100")
    }
    #footer,
    #header {
      height: 172px;
      width: 300px;
      background-color: yellow;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <div id="header"></div>
    
    <div id="sidebar">
      <div class="fixed">sidebar</div>
    </div>
    
    <div id="stuff">
    
    </div>
    
    <div id="footer">
    
    </div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this html I basically have a sidebar-div and a page-div with the header,
I'm trying to make a sidebar which on a website which will have a
Hi I have a simple sidebar inside a div which has height:100% and I
I have a sidebar which appears on every page. The first elements of the
I have an article 'tag' sidebar which is positioned correctly on the Home page,
What i'm trying to do is have a sidebar toggle open and close which
I want to have 4 links in the sidebar of master page which every
I have an asp.net page. There is sidebar in which there are several buttons.
I have a left floating div which serves as a sidebar (red). Next to
I have a static menu in the sidebar which I include in every JSF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.