Skip to content

Instantly share code, notes, and snippets.

@chancesmith
Created September 12, 2017 12:27
Show Gist options
  • Save chancesmith/c00876775c709023d6ab345ca212efbe to your computer and use it in GitHub Desktop.
Save chancesmith/c00876775c709023d6ab345ca212efbe to your computer and use it in GitHub Desktop.
Animate elements in and out on scroll
// onScroll animation
$(function() {
var $window = $(window),
isTouch = Modernizr.touch;
if (isTouch) { $('.add-animation').addClass('animated'); }
$window.on('scroll', revealAnimation);
function revealAnimation() {
// Showed...
$(".add-animation:not(.animated)").each(function () {
var $this = $(this),
offsetTop = $this.offset().top,
scrolled = $window.scrollTop(),
win_height_padded = $window.height();
if (scrolled + win_height_padded > offsetTop) {
$this.addClass('animated');
}
});
// Hidden...
$(".add-animation.animated").each(function (index) {
var $this = $(this),
offsetTop = $this.offset().top;
scrolled = $window.scrollTop(),
win_height_padded = $window.height() * 0.8;
if (scrolled + win_height_padded < offsetTop) {
$(this).removeClass('animated')
}
});
}
revealAnimation();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment