如何重复(循环)Jquery fadein - fadeout - fadein
我正在努力编写我的第一个 jQuery 脚本。我的页面上有一个 DIV,该 DIV 设置为通过 CSS 隐藏。然后,我运行这个脚本以使其淡入、淡出,然后再次淡入:
<script type="text/javascript">
(function($) {
$(function() {
$('#abovelogo').fadeIn(1000).delay(2000).fadeOut(1500).delay(2000).fadeIn(1500);
});
})(jQuery);
</script>
这部分工作正常。现在,我的问题是:
如何更改它以便该脚本运行循环(永远)而不是只运行一次?
提前致谢! -内特
I am struggling with my first jQuery script. I've got a DIV on my page that is set to hide via CSS. Then, I have this script that runs to make it fade in, fade out, then fade in again:
<script type="text/javascript">
(function($) {
$(function() {
$('#abovelogo').fadeIn(1000).delay(2000).fadeOut(1500).delay(2000).fadeIn(1500);
});
})(jQuery);
</script>
This part works fine. Now, my question:
How do I change it so that this script runs loops (forever) instead of just once?
Thanks in advance!
-Nate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将您的代码放入
setInterval
中:由于只要页面处于活动状态,您就会运行此代码,因此您应该尽一切努力来优化您的代码,例如您可以缓存时间间隔之外的选择:
setInterval
的文档:https://developer.mozilla.org/en/window.setInterval此外,您可以在每个动画中使用回调函数,而不是使用
.delay()
调用一个又一个动画:这是一个演示: http://jsfiddle.net/xsATz/
您可以也使用
setTimeout
并递归调用函数:这是一个演示:http://jsfiddle.net /xsATz/1/
Put your code inside a
setInterval
:Since you will be running this as long as the page is active then you should do everything you can to optimize your code, for instance you can cache the selection outside of the interval:
Docs for
setInterval
: https://developer.mozilla.org/en/window.setIntervalAlso, instead of using
.delay()
you can use the callback functions in each animation to call one animation after another:Here is a demo: http://jsfiddle.net/xsATz/
You can also use
setTimeout
and call a function recursively:Here is a demo: http://jsfiddle.net/xsATz/1/
只是为了想象。你可以用css3来做到这一点。我希望这也能帮助你。做这种事情,对于浏览器性能来说,css 应该比 jQuery 更舒服。
just for imagination. You can do it with css3. I hope that can help you too. Doing this kind of things, css should be more confortable for browser performance instead of jQuery.
我对 setTimeout 方法有一些问题。也许用“trigger”更好:
I have some problmes with setTimeout method. Maybe is better with "trigger":