4

I have a site with a bar on the left that should stay with the user. So when the user scrolls, the sidebar scrolls along until it's lets say 5px from the top of the page. From then on, it should be locked in place there.

Of course it's possible that the view-port is smaller than the left bar, so the left bar doesn't fully fit in the screen. Not really dramatic though. However I would like it if the user scrolls to the bottom, the bottom of the sidebar 'hits' the footer of the page and then scrolls along with the page again.

Here's the code I've got: a basic setup of my site and an attempt of the first part of my question (but you'll see it doesn't work): jsfiddle.

I think the first part of the question is rather clear, but the second part might be a bit hard to understand, so here's a mockup:when scrolled down You can see that there is no text shown because the text is above the viewport.

Here's the js for my attempt for part one:

$(document).ready(function () {
    var theLoc = 5;
    var links = $('#left');
    $(window).scroll(function () {
        console.log('scroll');
        if (theLoc >= $(window).scrollTop()) {
            if (links.hasClass('fixed')) {
                links.removeClass('fixed');
            }
        } else {
            if (!links.hasClass('fixed')) {
                links.addClass('fixed');
            }
        }
    });
});

But possibly more a css issue:

.fixed {
    position:fixed;
}

I tried specifying the height etc again (because it show up like very big), but with no advance.

2
  • 1
    checkout this plugin: mojotech.github.io/stickymojo
    – Anil
    Jun 26, 2013 at 14:56
  • I was thinking about writing it myself because often these plugins give more options than needed. Nevertheless, I gave it a try, but I think it doesn't quite work with my table'd layout: jsfiddle.net/kvVHh/1
    – jdepypere
    Jun 26, 2013 at 16:31

1 Answer 1

9

I did this a while ago, here is the code I created to do this: View the JSFiddle.

(You may have to change your markup a bit, Im not sure how well this would work with the table'd layout you have, I would suggest using divs and floating them to layout your content.), alternatively, you can use the code/logic below and roll your own with your own layout.

Basically,
- Get our elements
- Get the position of the sidebar on page load
- Get #content.outerHeight()

Once we have these vars, on window scroll test to see if we are <= (less than or equal to) our original sidebarTop position or check if we are past the blogHeight, if any off the 2 are true, remove the sticky class, elseif our scroll is >= our original sidebar position, then add the .sticky class (which has the position: fixed).

Check the JSFiddle (Click here)


The Javascript is like so:

// Cache our vars for the fixed sidebar on scroll
var $sidebar = $('#sidebar-nav');

// Get & Store the original top of our #sidebar-nav so we can test against it
var sidebarTop = $sidebar.position().top;

// Edit the `- 10` to control when it should disappear when the footer is hit.
var blogHeight = $('#content').outerHeight() - 10;

// Add the function below to the scroll event
$(window).scroll(fixSidebarOnScroll);

// On window scroll, this fn is called (binded above)
function fixSidebarOnScroll(){

    // Cache our scroll top position (our current scroll position)
    var windowScrollTop = $(window).scrollTop();

    // Add or remove our sticky class on these conditions
    if (windowScrollTop >= blogHeight || windowScrollTop <= sidebarTop){
        // Remove when the scroll is greater than our #content.OuterHeight()
        // or when our sticky scroll is above the original position of the sidebar
        $sidebar.removeClass('sticky');
    }
    // Scroll is past the original position of sidebar
    else if (windowScrollTop >= sidebarTop){
        // Otherwise add the sticky if $sidebar doesnt have it already!
        if (!$sidebar.hasClass('sticky')){
            $sidebar.addClass('sticky');
        }
    }   
}

The HTML:

<header>This is the header!</header>
<ul id="sidebar-nav" class="nav nav-list">
    <li><a href="#">Home</a></li>
    <li><a href="#">Blog</a></li>
</ul>
<div id="content">Content in here, scroll down to see the sticky in action!</div>
<div class="clear"></div>
<div id="footer">This is the #footer</div>

The CSS:

/* Sticky our navbar on window scroll */
#sidebar-nav.sticky {position:fixed;top:5px;}

/* Other styling for the html markup */
header {
    border:1px solid #aaa;
    background-color:yellow;
    margin-bottom:5px;
    height:50px;
}
#sidebar-nav {
    width:150px;
    border:1px solid #ddd;
    margin:0;
    padding:0;
    float:left;
}
#sidebar-nav li {
    list-style:none;
    border:1px solid #ddd;
    margin:10px;
    padding:2px;
}
#content {
    height:2000px;
    width:500px;
    padding:10px;
    border:1px solid #ddd;
    margin:0 0 10px 5px;
    float:right;
}
#footer {
    height:800px;
    border:1px solid green;
    background-color:#ddd;
}
.clear {
    clear:both;
}

:)

3
  • With your markup it seems to work, but I'd rather that the sidebar not disappear but rather lock on the footer instead! However, implementing your page-layout as opposed to my table-layout (clear did the magic) with the plugin JustAnil proposed, it seems to work as I wanted (of course with more code than you provided, but oh well). jsfiddle.net/dmLQL/1
    – jdepypere
    Jun 27, 2013 at 19:22
  • 1
    You could've edited the Javascript slightly to make the footer stick to the bottom instead of disappear, but glad I could help!
    – Anil
    Jun 28, 2013 at 7:33
  • @JustAnil How can your fiddle be edited to make the footer stick to the bottom? I'm trying to figure this out myself. Ideally, I'd like to add a class of bottom when the sidebar reaches the end of its container element. I have a similar question set up here: stackoverflow.com/questions/22588635/… If you can take a look, I would hugely appreciate it. I've been having a problem with this one for awhile.
    – J82
    Mar 23, 2014 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.