gpt4 book ai didi

jQuery - 如何编写自定义队列?

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

我正在尝试创建一个自定义队列,你能告诉我我做错了什么吗?

这是正在运行的代码: http://dl.dropbox.com/u/1292831/hell/index2.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" href="style/style.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>

<style type="text/css">
.tester {
background:red;
width: 100px;
height: 100px;
left: 900px;
top: 300px;
position: absolute;
}

.counter {
position: absolute;
left: 0;
top: 0;
width: 150px;
font-size: 18px;
}
</style>

<script type="text/javascript">

$(function(){

// animation for the 'FX' queue:
$('.tester').fadeOut(1000).fadeIn(1000)


// animation for the 'lolo' queue:
$('.tester').queue('lolo',function(next){
$(this).animate({left: 100}, {duration:1000})
next()
})

$('.tester').queue('lolo',function(next){
$(this).animate({left: 800}, {duration:1000})
next()
})
.dequeue('lolo')
})


// counters
setInterval(function(){
var count = $('.tester').queue('fx').length
$('.counter #c1').html(count)

var count = $('.tester').queue('lolo').length
$('.counter #c2').html(count)

}, 250)

</script>


</head>
<body>

<p class="counter">
items in the 'fx' queue <span id="c1"></span> <br />
items in the 'lolo' queue <span id="c2"></span>
</p>

<div class="tester"></div>

</body>
</html>

最佳答案

编辑:从 jQuery 1.7 开始,animate 确实采用了一个选项来指定要添加动画的自定义队列。

<小时/>

尚不完全清楚这里的问题是什么,但我认为通过查看示例,您希望队列中的动画位于不同的队列上。

问题就在这里。 Animate 始终位于效果队列中。我不知道有什么方法可以将其放在另一个队列中。因此,您总是在自定义队列上看到 0 的原因是您在队列中排队的事情会立即完成。他们只需调用 animate(将动画放入 fx 队列)并完成即可。这也是您最初在 fx 队列中看到 4 的原因。

解决此问题的一种方法是使用 queue:false 在自定义队列中运行动画,然后在该队列中自行处理延迟。例如:

http://jsfiddle.net/jRawX/6/

    $(function(){

// animation for the 'FX' queue:
$('.tester').fadeOut(1000).fadeIn(1000)


// animation for the 'lolo' queue:
$('.tester')
.queue('lolo',function(next){
$(this).animate({left: 100}, {duration:1000, queue:false, complete: next})
})
.queue('lolo',function(next){
$(this).animate({left: 600}, {duration:1000, queue:false, complete: next})
})
.dequeue('lolo')
})


// counters
setInterval(function(){
var count = $('.tester').queue('fx').length
$('.counter #c1').html(count)

var count = $('.tester').queue('lolo').length
$('.counter #c2').html(count)

}, 250)

可能有更好的方法来做到这一点,我刚刚提出了这个。但我无法找到任何方法在不同的队列上制作动画。

编辑:略有改进,在动画回调上调用下一个。

关于jQuery - 如何编写自定义队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950625/

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