Last active
February 22, 2023 03:11
-
-
Save mikeott/c06f29b6b4e5077d36b0e76c83ebaebd to your computer and use it in GitHub Desktop.
JS do something when 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 = $("#my_element").offset().top; | |
var bottom_of_element = $("#my_element").offset().top + $("#my_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)){ | |
// element is in view, do something | |
} else { | |
// element is out of view, do something else | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment