Skip to content

Instantly share code, notes, and snippets.

@forsvunnet
Created June 24, 2019 07:20
Show Gist options
  • Save forsvunnet/1d6cfb8d9f966540228220653a6982c5 to your computer and use it in GitHub Desktop.
Save forsvunnet/1d6cfb8d9f966540228220653a6982c5 to your computer and use it in GitHub Desktop.
Add classes to elements on scroll when they are in view
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