简体   繁体   English

滚动动画

[英]Animation on Scroll

Hello I work on create simple animation on scroll, this animation depend on animate.css Library but there are two problems. 您好我在滚动上创建简单的动画,此动画取决于animate.css库,但是有两个问题。

First Problem: 第一个问题:

That I need to run the animation on user scroll to bottom only. 我只需要在用户滚动到底部时运行动画。


Second Problem: 第二个问题:

That is there are a strange animation on I scroll, The animation is not work well you can note this if you run the code snippet. 那是我滚动的一个奇怪的动画,该动画无法正常运行,如果您运行代码段,则可以注意这一点。

Here my code 这是我的代码

 $(function () { var $animation_elements = $('.myDiv'); $(window).on('scroll resize', function () { var viewportHeight = $(window).height(); $animation_elements.each(function () { var $el = $(this); var position = this.getBoundingClientRect(); // This Function Will Return With object Contains top left Width Height if (position.top > viewportHeight || position.bottom < 0) { this.inView && $el.removeClass('animated bounceIn'); this.inView = false; } else { !this.inView && $el.addClass('animated bounceIn'); this.inView = true; } }); }); }); 
 body{ height:4000px; margin-top:800px; } .myContainer{ width:1000px; margin:50px auto; } .myContainer .myDiv { width: 400px; height: 200px; margin: auto; background-color: #00e675; -moz-animation-duration: 5s !important; -o-animation-duration: 5s !important; -webkit-animation-duration: 5s !important; animation-duration: 5s !important; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="myContainer"> <div class="myDiv"> </div> </div> 

Note: Please run code snippet in full page. 注意:请在整页中运行代码段。

This worked for me... tried in firefox and now work well... I used these references: 这对我有用...在firefox中尝试过,现在可以正常工作了...我使用了以下引用:

  1. Detect user scroll down or scroll up in jQuery 检测用户在jQuery中向下滚动或向上滚动
  2. jQuery scroll() detect when user stops scrolling jQuery scroll()检测用户何时停止滚动

The problem was that scroll event produced for one scroll a lot of events and for every event animation started. 问题是滚动事件为一个滚动产生了很多事件,并且为每个事件动画启动了。 For that I packed your function to another function to create an event only after stopping scrolling and after that animation will start. 为此,我将您的函数打包到另一个函数中,以仅在停止滚动后才创建事件,然后动画将开始。 (you can notice that normal person that come to site and want even see something will stop to scroll for a few seconds, that's the time to show up your animation in order to fulfilling your condition about scrolling down only). (您会注意到,来到现场并且甚至希望看到东西的普通人将停止滚动几秒钟,这是显示动画的时间,以便满足仅向下滚动的条件)。

 switching = 0 var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x // detect when scroll down and catch it $('html').bind(mousewheelevt, function(e){ var evt = window.event || e //equalize event object evt = evt.originalEvent ? evt.originalEvent : evt; //convert to originalEvent if possible var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF if(delta > 0) { //scroll up switching = 0; } else{ switching = 1; //scroll down } }); //create event only after scroll stop $(window).scroll(function() { clearTimeout($.data(this, 'scrollTimer')); $.data(this, 'scrollTimer', setTimeout(function() { // do your animation $(function () { var $animation_elements = $('.myDiv'); $(window).on('scroll resize', function () { //console.log(switching); if (switching){ var viewportHeight = $(window).height(); $animation_elements.each(function () { var $el = $(this); var position = this.getBoundingClientRect(); // This Function Will Return With object Contains top left Width Height if (position.top > viewportHeight || position.bottom < 0) { this.inView && $el.removeClass('animated bounceIn'); this.inView = false; } else { !this.inView && $el.addClass('animated bounceIn'); this.inView = true; } }); } }); }); // console.log("Haven't scrolled in 250ms!"); // console.log(switching); }, 250)); }); 
 body{ height:4000px; margin-top:800px; } .myContainer{ width:1000px; margin:50px auto; } .myContainer .myDiv { width: 400px; height: 200px; margin: auto; background-color: #00e675; -moz-animation-duration: 5s !important; -o-animation-duration: 5s !important; -webkit-animation-duration: 5s !important; animation-duration: 5s !important; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <body> <div class="myContainer"> <div class="myDiv" > </div> </div> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM