Skip to content

Instantly share code, notes, and snippets.

@neilbo
Created July 14, 2017 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilbo/a41089d65ef88671cf510b531d65418d to your computer and use it in GitHub Desktop.
Save neilbo/a41089d65ef88671cf510b531d65418d to your computer and use it in GitHub Desktop.
animate element when in scroll view
var inView = false;
function isScrolledIntoView(elementID) {
// This checks if the element is in view
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elementID).offset().top;
var elementBottom = elemTop + $(elementID).height();
return ((elemTop <= docViewBottom) && (elementBottom >= docViewTop));
}
function animateElement(element, animationClass) {
// This adds the animated class when the element is in view
$(window).scroll(function () {
if (isScrolledIntoView(element)) {
// if isScrolledIntoView returns true
// add animation to element
$(element).addClass('animated ' + animationClass);
inView = true;
} else {
// if isScrolledIntoView returns true
// remove animation to element
$(element).removeClass('animated ' + animationClass);
inView = false;
}
});
}
animateElement('.heading-first', 'slideInLeft');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment