12

I'm using Bootstrap 3 and want to achieve this effect when the user scrolls past the large header image on my page. I need the background of the navbar to go from transparent to white. I looked in their code and I KNOW it is done with javascript, and even saw WHERE it was happening I think (look for the ID '#main-header' in that JS)...

Not knowing advanced Javascript aside, I'm looking for a way to apply this to my navigation bar when scrolling past a certain point. The class for my code is called 'navbar' and I would like it to turn white when it passes "#main". Let me know if you need more information, and thanks in advance if anyone wants to help!

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
katgarcia
  • 173
  • 1
  • 4
  • 14
  • http://stackoverflow.com/questions/13864785/add-class-to-anchor-link-when-scrolling-past-anchor-div-jquery – Christina Nov 29 '13 at 05:53

3 Answers3

36

The easiest way to accomplish what you're trying to do is a combination of some simple javascript (jQuery powered in this case) and CSS3 transitions.

We'll use JS to check for the windows scroll position on every scroll event and compare it to the distance of the bottom of the #main element - if the scroll position is greater, then we'll apply a class to the body to indicate we've scrolled past #main, and then we'll use CSS to define the nav styling for that "state."


So, our basic markup:

<nav class="nav">
    <a href="#" class="logo">[logo]</a>
</nav>
<div id="main">#main</div>
<div id="below-main">#below-main</div>

And our javascript:

// get the value of the bottom of the #main element by adding the offset of that element plus its height, set it as a variable
var mainbottom = $('#main').offset().top + $('#main').height();

// on scroll, 
$(window).on('scroll',function(){

    // we round here to reduce a little workload
    var stop = Math.round($(window).scrollTop());

    if (stop > mainbottom) {
        $('.nav').addClass('past-main');
    } else {
        $('.nav').removeClass('past-main');
    }

});

And, our styles:

.nav {
    background-color:transparent;
    color:#fff;
    transition: all 0.25s ease;
    position:fixed;
    top:0;
    width:100%;
    background-color:#ccc;
    padding:1em 0;
    /* make sure to add vendor prefixes here */
}

.nav.past-main {
    background-color:#fff;
    color:#444;
}

#main {
  height:500px;
  background-color:red;
}

#below-main {
  height:1000px;
  background-color:#eee;
}

A working example on Codepen

This is how I did it here. I also employ some scroll throttling and a bit more complicated styling semantics, but this is the gist of it.

taylorleejones
  • 481
  • 1
  • 3
  • 4
  • Hey! I just saw this. Thanks so much for responding. I actually got it to work with Bootstrap's affix eventually, it was for a class project [http://www.ourdesign.us/2013/12/13/some-of-my-students-work-at-northeastern-university-this-fall-2013/, mine is the bottom right.. haven't put it on my own portfolio yet]. Awesome, awesome, awesome work with the Whiteboard website, I seriously have looked at it for inspiration so many times. I'll keep this in mind when I undoubtedly want to achieve this effect again eventually without the Bootstrap safety net. – katgarcia Jan 17 '14 at 17:21
12

If you're using Twitter Bootstrap this can be achieved with the 'Affix' plugin

It's pretty straight forward to set up, here is the documentation

Daimz
  • 3,243
  • 14
  • 49
  • 76
  • Thanks so much! I'm trying to implement it right now but it's not working/I'm not sure how to exactly work it... I have the `
    ` around my navigation, and then in my CSS I have`.affix`, `.affix-top`, and `.affix-bottom`. However, I'm not sure where/how to refer to the anchor points that you scroll past on the page and whether I'm missing something. Sorry, I'm pretty bad at figuring out docs..
    – katgarcia Nov 29 '13 at 02:49
  • Ok this is covered over here: http://stackoverflow.com/a/13151016/1031184 And one kind person actually build a jsfiddle to show how it should work so head over here and heck it out it's pretty clear: http://jsfiddle.net/namuol/Uaa3U/ – Daimz Nov 30 '13 at 03:00
1

You could probably just use javascript element.scrollTop along with Jquery addClass and removeClass. Haven't tried it myself though.

Here's an overflow link for getting scrollbar position: How to get scrollbar position with Javascript?

Community
  • 1
  • 1
user3047190
  • 319
  • 2
  • 7