简体   繁体   English

setInterval循环动画不起作用

[英]setInterval to loop animation not working

I have a simple fadeIn fadeOut animation, it's basically a blinking arrow. 我有一个简单的fadeIn fadeOut动画,基本上是一个闪烁的箭头。 However, it doesn't loop. 但是,它不会循环。 It just goes once, and it's done. 它只执行一次,就完成了。 I found an answer here -> How to repeat (loop) Jquery fadein - fadeout - fadein , yet when I try to follow it, mine doesn't work. 我在这里找到了答案-> 如何重复(循环)jQuery淡入-淡出-淡入 ,但是当我尝试遵循时,我的方法不起作用。

The script for the animation is 动画的脚本是

$(function () {
    setInterval(function () {
        $('#abovelogo').fadeIn(1000).delay(2000).fadeOut(1500).delay(2000).fadeIn(1500);
    }, 5000);
});

the script given in the answer is 答案中给出的脚本是

 $(document).ready(function() {
   setInterval(function () {
        $('#picOne').fadeIn(1000).delay(3000).fadeOut(1000);
       $('#picTwo').delay(5000).fadeIn(1000).delay(3000).fadeOut(1000);
    }, 5000);
});
    </script>

so I assume the end combination would be 所以我认为最终的组合是

  $(document).ready(function() { setInterval(function () { $('#picOne').fadeIn(1000).delay(3000).fadeOut(1000); $('#picTwo').delay(5000).fadeIn(1000).delay(3000).fadeOut(1000); }, 5000); }); </script> 

Could someone please point out what I'm doing wrong? 有人可以指出我做错了吗? thanks 谢谢

Two details : 两个细节:

  • You have to set the interval to 10000 because your animation run 10s 您必须将间隔设置为10000因为动画会运行10秒

  • If you want it to start now, you have to call it one time before executing the interval (the first execution of the interval is after the delay) 如果要立即开始,则必须在执行间隔之前调用一次(间隔的第一次执行是在延迟之后)

-- -

$(document).ready(function() {

   function animate() {
       $('#picOne').fadeIn(1000).delay(3000).fadeOut(1000);
       $('#picTwo').delay(5000).fadeIn(1000).delay(3000).fadeOut(1000);
   }

   animate();
   setInterval(animate, 10000);
});​

Demonstration here : http://jsfiddle.net/bjhG7/1/ 这里的演示: http : //jsfiddle.net/bjhG7/1/

-- -

Alternative code using callback instead of setInterval (see comments): 使用回调代替setInterval的替代代码(请参见注释):

$(document).ready(function() {

   function animate() {
       $('#picOne').fadeIn(1000).delay(3000).fadeOut(1000);
       $('#picTwo').delay(5000).fadeIn(1000).delay(3000).fadeOut(1000, animate);
   }

   animate();
});​

Demonstration here : http://jsfiddle.net/bjhG7/3/ 这里的演示: http : //jsfiddle.net/bjhG7/3/

function fadein(){
    $('#picOne,#picTwo').animate({'opacity':'1'},1000,fadeout())
}
function fadeout(){
    $('#picOne,#picTwo').animate({'opacity':'0'},1000,fadein())
}
fadein()

Take advantage of the callback argument of .fadeOut() . 利用.fadeOut()的回调参数。 Pass a reference to the function that does the fading as the callback parameter. 将对衰落的函数的引用作为回调参数传递。 Choose which image to fade based on a counter: 根据计数器选择要淡出的图像:

$(function() {
    var imgs = $('#picOne,#picTwo');
    var fadeCounter = 0;

    (function fadeImg() {
        imgs.eq(fadeCounter++ % 2).fadeIn(1000).delay(3000).fadeOut(1000, fadeImg);
    })();
});

Demo: http://jsfiddle.net/KFe5h/1 演示: http //jsfiddle.net/KFe5h/1

As animation sequences get more complex, I've found using async.js leads to more readable and maintainable code. 随着动画序列变得越来越复杂,我发现使用async.js会导致更具可读性和可维护性的代码。 Use the async.series call. 使用async.series调用。

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

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