Last active
June 6, 2020 22:10
-
-
Save stormwarning/8981393 to your computer and use it in GitHub Desktop.
Change an element's class when it comes into view.
This file contains 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
/** | |
* 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