1// this function runs every time you are scrolling
2
3$(window).scroll(function() {
4 var top_of_element = $("#element").offset().top;
5 var bottom_of_element = $("#element").offset().top + $("#element").outerHeight();
6 var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();
7 var top_of_screen = $(window).scrollTop();
8
9 if ((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
10 // the element is visible, do something
11 } else {
12 // the element is not visible, do something else
13 }
14});
15
1$.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;};