Created
June 13, 2018 11:26
-
-
Save Farmatique/5eda458715bc8730e074099221fb30b8 to your computer and use it in GitHub Desktop.
detect if element is in viewport
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() { | |
var top_of_element = $("#element").offset().top; | |
var bottom_of_element = $("#element").offset().top + $("#element").outerHeight(); | |
var bottom_of_screen = $(window).scrollTop() + window.innerHeight; | |
var top_of_screen = $(window).scrollTop(); | |
if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){ | |
// The element is visible, do something | |
} | |
else { | |
// The element is not visible, do something else | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment