Created
March 30, 2018 08:59
-
-
Save ahmedghazi/190e030d6fc95837c408ced5ce8eb3e1 to your computer and use it in GitHub Desktop.
is in view port
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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