jQuery 中的动画问题和大量 stop() 调用
这就是我想做的: 我有几个带有价格的 div,以及一个具有固定最小值的滑块,我可以在其中设置最高价格。这样我就可以过滤 div,因此只有价格在滑块范围内的 div 才会显示。
没有动画就没有问题,只需 hide() 和 show(),但我试图让它顺利进行。
vehicles[0] = { id: 1, price: 100 };
vehicles[1] = { id: 2, price: 250 };
vehicles[2] = { id: 3, price: 700 };
vehicles[3] = { id: 4, price: 300 };
...
slide: function(event, ui) {
for (i = 0; i < vehicles.length; i++) {
if (vehicles[i].price > ui.value && $('#vehicle'+vehicles[i].id).data('visible') == true) {
$('#vehicle'+vehicles[i].id).data('visible',false).stop(true).hide('blind',500);
}
if (vehicles[i].price <= ui.value && $('#vehicle'+vehicles[i].id).data('visible') == false) {
$('#vehicle'+vehicles[i].id).data('visible',true).stop(true).show('blind',500);
}
}
}
...
<div id="vehicle1">100€</div>
<div id="vehicle1">250€</div>
<div id="vehicle1">700€</div>
<div id="vehicle1">300€</div>
这是我的代码,这是我的问题:当将滑块推到一侧或一点时,它工作正常,但如果将其推到 0€ 并立即回到 700€(当 hide() 动画仍在运行时),所有 div被隐藏(但它们的数据(“可见”)设置为 true)。您可以在这里看到我的运行代码: http://work4.bywulf.de/index.php ?page=Vehicles 只需将滑块快速向左滑动,然后再向右滑动即可。
看起来 stop() 方法没有正确停止当前的“隐藏”动画,并且“显示”动画没有播放。
现在我做错了什么,还是有另一种方法来隐藏动画元素,但中途停止并再次完全显示它们?
我希望您知道我的意思以及我想做什么,谢谢您的帮助。
(jQuery 1.5,jQueryUI 1.8.9)
--Wulf
here is what I'm trying to do:
I have a few div's with prices in them, and a slider with fixed minimum where I can set the maximum price. With that I can filter the divs, so only the div's with prices in the slider range will be displayed.
Without animation it would be no problem, just hide() and show(), but I'm trying to do it smooth.
vehicles[0] = { id: 1, price: 100 };
vehicles[1] = { id: 2, price: 250 };
vehicles[2] = { id: 3, price: 700 };
vehicles[3] = { id: 4, price: 300 };
...
slide: function(event, ui) {
for (i = 0; i < vehicles.length; i++) {
if (vehicles[i].price > ui.value && $('#vehicle'+vehicles[i].id).data('visible') == true) {
$('#vehicle'+vehicles[i].id).data('visible',false).stop(true).hide('blind',500);
}
if (vehicles[i].price <= ui.value && $('#vehicle'+vehicles[i].id).data('visible') == false) {
$('#vehicle'+vehicles[i].id).data('visible',true).stop(true).show('blind',500);
}
}
}
...
<div id="vehicle1">100€</div>
<div id="vehicle1">250€</div>
<div id="vehicle1">700€</div>
<div id="vehicle1">300€</div>
That's my code and here is my problem: When pushing the slider to one side or point, it works fine, but f.e. pushing it to 0€ and immediately back to 700€ (while the hide() animation is still running), all divs are hidden (but their data('visible') is set to true). You can see my running code here: http://work4.bywulf.de/index.php?page=Vehicles Just slide the slider fast to the left and back to the right.
It looks like the stop() method is not correctly stopping their current "hide" animation, and the "show" animation is not playing.
Now what am I doing wrong or is there another way to hide elements animated, but stop them half way and show them again completely?
I hope you know what I mean and what I'm trying to do, thank you for your help.
(jQuery 1.5, jQueryUI 1.8.9)
--Wulf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题解决了,只是做了一个自己的.animation()。我认为问题是,show() 和 hide() 按原样查看项目,当项目仅显示 50% 时,它会很困难。 .animation() 将从 50% 开始并在给定尺寸处结束。所以我详细做的是:
首先我初始化了容器,因此保存了高度:
然后,当需要动画时,执行这部分:
无论如何,谢谢你的建议。
Problem solved, just did an own .animation(). I think the problem was, that show() and hide() see the item as it is, and when the item is shown only 50%, it struggles. .animation() will start at the 50% and end at the given dimentions. So what I did in detail was:
First i initialized the container, so the height is saved:
Then, when animation was needed, this part is executed:
Thanks for your advice anyway.
我建议:
或者也许slideUp/slideDown有不同的效果:
show()和hide()似乎确实有问题
I would propose:
Or maybe slideUp/slideDown for a different effect:
show() and hide() seems buggy indeed
您可能遇到重入问题。
当另一个事件触发代码再次运行时,代码已经在运行。
您可以尝试这样的操作:
当用户最终放开滑块时,您仍然需要一种方法来确保视图同步到滑块位置。
You might be experiencing a re-entrancy problem.
The code is already running when another event triggers it to run again.
You could try something like this:
You would still need a way to make sure the view is synced to the slider position when the user finally lets go of the slider.