Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Highlight on scroll
$(window).scroll(function(){
var top = $(window).scrollTop();
var offset = 130;
var trigger = top+offset;
var elements = $('h1, h2, li');
checkElements(trigger, elements);
});
function checkElements(top, el){
// circle through elements
for (var i=0; i<el.length; i++){
// get top and bottom of element
// color is triggered when in between
var up = el[i].offsetTop;
var height = el[i].clientHeight;
var bot = up+height;
if(el[i].className == 'active'){
if(top < up || top > bot){
el[i].className = '';
}
} else if (top > up && top < bot){
el[i].className = 'active';
}
}
}
@clarknelson

This comment has been minimized.

Copy link
Owner Author

@clarknelson clarknelson commented Sep 1, 2014

Wrote this code to highlight elements as you scroll down the page for my Portfolio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment