Skip to content

Instantly share code, notes, and snippets.

@stormwarning
Last active June 6, 2020 22:10
Show Gist options
  • Save stormwarning/8981393 to your computer and use it in GitHub Desktop.
Save stormwarning/8981393 to your computer and use it in GitHub Desktop.
Change an element's class when it comes into view.
/**
* Change class when element is in view
*/
var elementTop = $(".container").offset().top;
$(window).on("scroll", function updateClassWhenInView() {
if ($(window).scrollTop() > elementTop - window.innerHeight * 0.9) {
$(window).off("scroll", updateClassWhenInView);
$(".element").removeClass("is-hidden").addClass("is-visible");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment