gpt4 book ai didi

jquery - 如何在 jQuery 中重复调用函数

转载 作者:行者123 更新时间:2023-12-01 06:27:56 25 4
gpt4 key购买 nike

我有这个代码:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
function loop(){
$('#picOne').fadeIn(0).fadeOut(8000);
$('#picTwo').delay(2000).fadeIn(6000).fadeOut(5000);
$('#picTree').delay(10000).fadeIn(2000).fadeOut(16000);
$('#picFour').delay(12000).fadeIn(16000).fadeOut(5000);
}
loop();
});
</script>

但是当最后一张图片淡出时,代码不会重复。有什么问题吗?

最佳答案

假设您希望每个元素的动画持续时间相同:

var $elements = $('#picOne, #picTwo, #picTree, #picFour');

function anim_loop(index) {
// Get the element with that index and do the animation
$elements.eq(index).fadeIn(1000).delay(3000).fadeOut(1000, function() {
// Kind of recursive call, increasing the index and keeping in the
// the range of valid indexes
anim_loop((index + 1) % $elements.length);
});
}

anim_loop(0); // start with the first element

我不知道动画到底应该是什么样子,但我希望它能让概念清晰。

更新:要在一定时间段后同时淡出和显示图像,请使用 setTimeout 并调用 fadeOutanim_loop 在回调中:

$elements.eq(index).fadeIn(1000, function() {
var $self = $(this);
setTimeout(function() {
$self.fadeOut(1000);
anim_loop((index + 1) % $elements.length);
}, 3000);
});

DEMO

关于jquery - 如何在 jQuery 中重复调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14537522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com