Skip to content

Instantly share code, notes, and snippets.

@cherihung
Last active December 16, 2015 19:09
Show Gist options
  • Save cherihung/5482802 to your computer and use it in GitHub Desktop.
Save cherihung/5482802 to your computer and use it in GitHub Desktop.
Sample code for adding/removing css class names according to a user's scrolling position on the page. example result is to change the background color of navigational items. $j for use with jQuery.noConflict();
$j(window).scroll(function(){
$navp = $j('#mm-nav');
$nav = $j('#mm-nav li a');
$navp = $j('#mm-nav');
$nav = $j('#mm-nav li a');
for(var i=1; i<8; i++) {
var os = i === 7 ? 190 : 90;
if($j(this).scrollTop() >= $j('.p'+i).offset().top - os) {
$nav.removeClass("sel");
$j('#mm-nav #a'+i ).addClass("sel");
}
}
if ($j(this).scrollTop() > 250 && $navp.css('position') != 'fixed'){
$navp.addClass("fixedhead");
} else if ($j(this).scrollTop() < 250 && $navp.css('position') == 'fixed'){
$navp.removeClass("fixedhead");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment