Start Animation When Scrolled To
Solution 1:
Javascript
var$window = $(window);
var$elem = $(".animation")
functionisScrolledIntoView($elem, $window) {
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
$(document).on("scroll", function () {
if (isScrolledIntoView($elem, $window)) {
$elem.addClass("animate")
}
});
HTML
<divclass="container"><divclass="animation">Content goes here...</div></div>
CSS
.animation.animate {
animation: pulse 5s infinite;//change this to whatever you want
}
JSFiddle to play with (don't forget to scroll)
Solution 2:
no need to wonder about it... just use it
download animate.css and implement this in
<scriptsrc="http://mynameismatthieu.com/WOW/dist/wow.min.js"></script><linkhref="css/animate.css"><script>newWOW().init();
</script><divclass="wow bounceInLeft animated"><h2>animated heading</h2></div>
try this code...
(these classes can be used)
bounce
flash
pulse
rubberBand
shake
headShake
swing
tada
wobble
jello
bounceIn
bounceInDown
bounceInLeft
bounceInRight
bounceInUp
bounceOut
bounceOutDown
bounceOutLeft
bounceOutRight
bounceOutUp
fadeIn
fadeInDown
fadeInDownBig
fadeInLeft
fadeInLeftBig
fadeInRight
fadeInRightBig
fadeInUp
fadeInUpBig
fadeOut
fadeOutDown
fadeOutDownBig
fadeOutLeft
fadeOutLeftBig
fadeOutRight
fadeOutRightBig
fadeOutUp
fadeOutUpBig
flipInX
flipInY
flipOutX
flipOutY
lightSpeedIn
lightSpeedOut
rotateIn
rotateInDownLeft
rotateInDownRight
rotateInUpLeft
rotateInUpRight
rotateOut
rotateOutDownLeft
rotateOutDownRight
rotateOutUpLeft
rotateOutUpRight
hinge
rollIn
rollOut
zoomIn
zoomInDown
zoomInLeft
zoomInRight
zoomInUp
zoomOut
zoomOutDown
zoomOutLeft
zoomOutRight
zoomOutUp
slideInDown
slideInLeft
slideInRight
slideInUp
slideOutDown
slideOutLeft
slideOutRight
slideOutUp
Solution 3:
@WebWeb , you can try this simple formula :
$(window).on('scroll' , function(){
scroll_pos = $(window).scrollTop() + $(window).height();
element_pos = $(yourelement).offset().top + $(yourelement).height() ;
if (scroll_pos > element_pos) {
$(yourelement).addClass('add-anim');
};
})
It is basically checking if the windows scroll position is higher than that of the elements offset from the top of the document(plus the element's height) . It is an age-old formula.
If you are lazy like me though, you can go for waypoints.js an amazing library.
Solution 4:
You can try wow.js it's quick and simple for integrate animation on scroll when element is visible on page. I create simple demo.
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1"><title>Bootstrap 101 Template</title><linkhref="http://mynameismatthieu.com/WOW/css/libs/animate.css"><style>body {
padding-bottom: 200px;
}
</style></head><body><divstyle="height: 110vh"></div><divclass="wow bounceInLeft">
Animation start when Visible
</div><divdata-wow-delay=".5s"class="wow bounceInLeft">
Animation start when Visible after .5s delay
</div><divdata-wow-delay="1s"class="wow bounceInLeft">
Animation start when Visible after 1s delay
</div><divdata-wow-delay="2s"class="wow bounceInLeft">
Animation start when Visible after 2s delay
</div><divstyle="text-align: center; margin-top: 300px;"><spandata-wow-delay=""class="wow bounceInDown">Link 1</span><spandata-wow-delay=".1s"class="wow bounceInDown">Link 3</span><spandata-wow-delay=".2s"class="wow bounceInDown">Link 3</span><spandata-wow-delay=".3s"class="wow bounceInDown">Link 4</span></div><scriptsrc="http://mynameismatthieu.com/WOW/dist/wow.min.js"></script><script>newWOW().init();
</script></body></html>
Solution 5:
If there is anyone wants to use this for an animation that should run when you open the page, hover it, when you scroll and run again when you scroll back, here is how I solved it.
I used what @robert used and added some refinements.
I made this fiddle for this https://jsfiddle.net/hassench/rre4qxhf/
So there you go:
var $window = $(window);
var $elem = $(".my-image-container");
var $gotOutOfFrame = false;
functionisScrolledIntoView($elem, $window) {
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop) && $gotOutOfFrame);
}
functionisScrolledOutView($elem, $window) {
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
return ((elemBottom < docViewBottom) && (elemTop < docViewTop));
}
$(document).on("scroll", function() {
if (isScrolledIntoView($elem, $window)) {
$gotOutOfFrame = false;
$elem.addClass("animate");
$(function() {
setTimeout(function() {
$elem.removeClass("animate");
}, 1500);
});
}
if (isScrolledOutView($elem, $window)) {
$gotOutOfFrame = true;
$elem.removeClass("animate");
}
});
.my-image-container {
top: 50px;
max-width: 22%;
}
.my-image-container:hover {
animation: shake 0.82scubic-bezier(0.36, 0.07, 0.19, 0.97) both;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
}
.my-image-container.my-image {
animation: shake 0.82scubic-bezier(0.36, 0.07, 0.19, 0.97) both;
-moz-animation-delay: 1s;
-webkit-animation-delay: 1s;
-o-animation-delay: 1s;
animation-delay: 1s;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
width: 100%;
}
.animate {
animation: shake 0.82scubic-bezier(0.36, 0.07, 0.19, 0.97) both;
-moz-animation-delay: 0.5s;
-webkit-animation-delay: 0.5s;
-o-animation-delay: 0.5s;
animation-delay: 0.5s;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
}
@keyframes shake {
10%,
90% {
transform: translate3d(-1px, 0, 0);
}
20%,
80% {
transform: translate3d(2px, 0, 0);
}
30%,
50%,
70% {
transform: translate3d(-4px, 0, 0);
}
40%,
60% {
transform: translate3d(4px, 0, 0);
}
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The animation will run when you firt open the page,<br>
and when you hover it,<br>
and when you scroll out then in. <br><divclass="my-image-container"><imgclass="my-image"src="http://www.lifeofpix.com/wp-content/uploads/2017/05/img-5831.jpg"></div><br> Scroll down then back up
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
scroll up
Post a Comment for "Start Animation When Scrolled To"