Created
July 14, 2017 04:58
-
-
Save neilbo/a41089d65ef88671cf510b531d65418d to your computer and use it in GitHub Desktop.
animate element when in scroll 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 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