Created
June 24, 2019 07:20
-
-
Save forsvunnet/1d6cfb8d9f966540228220653a6982c5 to your computer and use it in GitHub Desktop.
Add classes to elements on scroll when they are in view
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
var suffix = '-x'; | |
var windowHeight; | |
var products = $('.animate' + suffix); | |
products.addClass('animate' + suffix); | |
var checkPosition = function () { | |
products.each(function() { | |
var elem = $(this); | |
var top_of_element = elem.offset().top; | |
var bottom_of_element = elem.offset().top + elem.outerHeight(); | |
var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight(); | |
var top_of_screen = $(window).scrollTop(); | |
if ((bottom_of_screen - 100 > top_of_element)){ | |
elem.addClass('animating' + suffix); | |
} else { | |
// elem.removeClass('fade-in-element'); | |
} | |
} ); | |
}; | |
$(window).scroll(checkPosition); | |
checkPosition(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment