使用 jquery 中断并重新启动 setTimeOut 事件序列?
抱歉,如果这没有意义,我是一个完全的初学者!
基本上,我试图让一系列文本逐渐淡入然后淡出,直到最后一个文本出现并保持静态。但是,如果用户在最后一个文本出现之前移动鼠标/键盘,我希望序列被中断。我希望出现“重新开始”文本,然后重新开始序列。
这是我到目前为止所想到的。但我不知道如何编写中断代码!
Javascript:
$(document).ready(function() {
setTimeout( "jQuery('#text1').fadeIn();",1000 );
setTimeout( "jQuery('#text1').fadeOut();",5000 );
setTimeout( "jQuery('#text2').fadeIn();",6000 );
setTimeout( "jQuery('#text2').fadeOut();",10000 );
setTimeout( "jQuery('#text3').fadeIn();",11000 );
setTimeout( "jQuery('#text3').fadeOut();",15000 );
setTimeout( "jQuery('#text4').fadeIn();",16000 );
setTimeout( "jQuery('#text4').fadeOut();",20000 );
setTimeout( "jQuery('#text5').fadeIn();",21000 );
});
CSS:
.hidden {
display: none;
}
HTML:
<div id="startagain" class=hidden">Start Again!</div>
<div id="text1" class="hidden">Text 1</div>
<div id="text2" class="hidden">Text 2</div>
<div id="text3" class="hidden">Text 3</div>
<div id="text4" class="hidden">Text 4</div>
<div id="text5" class="hidden">Text 5</div>
非常感谢任何帮助! :-)
Sorry if this makes no sense, I am a complete beginner!
Basically i am trying to get a sequence of texts to fade in then out one after the other until finally the last one appears and stays static. However, i want the sequence to be interrupted if the user moves the mouse/keyboard before the last text has appeared. I'd like the text "begin again" to appear and then the sequence to begin again.
This is what I've come up with so far. But I'm not sure how to code the interruption!
Javascript:
$(document).ready(function() {
setTimeout( "jQuery('#text1').fadeIn();",1000 );
setTimeout( "jQuery('#text1').fadeOut();",5000 );
setTimeout( "jQuery('#text2').fadeIn();",6000 );
setTimeout( "jQuery('#text2').fadeOut();",10000 );
setTimeout( "jQuery('#text3').fadeIn();",11000 );
setTimeout( "jQuery('#text3').fadeOut();",15000 );
setTimeout( "jQuery('#text4').fadeIn();",16000 );
setTimeout( "jQuery('#text4').fadeOut();",20000 );
setTimeout( "jQuery('#text5').fadeIn();",21000 );
});
CSS:
.hidden {
display: none;
}
HTML:
<div id="startagain" class=hidden">Start Again!</div>
<div id="text1" class="hidden">Text 1</div>
<div id="text2" class="hidden">Text 2</div>
<div id="text3" class="hidden">Text 3</div>
<div id="text4" class="hidden">Text 4</div>
<div id="text5" class="hidden">Text 5</div>
Any help really appreciated! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@莎拉
无法在评论中发布代码,所以在这里:
@Sarah
Unable to post code in the comment back so here ya go:
您的基本设置和清除计时器...
如果您正在排队动画,最好使用回调函数而不是计时器,如下所示:
希望这有帮助!
Your basic setting and clearing of timers...
If you are queueing up animations, its best to use callback functions instead of timers, like so:
Hope this helps!
您可以通过利用 jquery 的动画队列来实现这种效果,如下
HTML:
Javascript:
You could achieve this effect by taking advantage of jquery's animation queue like so
HTML:
Javascript: