Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Farmatique/5eda458715bc8730e074099221fb30b8 to your computer and use it in GitHub Desktop.
Save Farmatique/5eda458715bc8730e074099221fb30b8 to your computer and use it in GitHub Desktop.
detect if element is in viewport
$(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