Skip to content

Instantly share code, notes, and snippets.

@ahmedghazi
Created March 30, 2018 08:59
Show Gist options
  • Save ahmedghazi/190e030d6fc95837c408ced5ce8eb3e1 to your computer and use it in GitHub Desktop.
Save ahmedghazi/190e030d6fc95837c408ced5ce8eb3e1 to your computer and use it in GitHub Desktop.
is in view port
$(window).scroll(function(){
clearTimeout(window.resizedFinished);
window.resizedFinished = setTimeout(function(){
$(".tpl-section").each(function(idx, el){
if($(el).isInViewport()){
//console.log(idx)
$(".sub-menu a").removeClass("active");
$(".sub-menu li").eq(idx).children("a").addClass("active")
}
})
}, 250);
})
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment