我有一个带有多个嵌套无序列表的无序列表。我试图独立地将多个嵌套的<ul></ul>动画到包含的div中的一个随机位置。我得到了随机位置部分,但是它给每个嵌套的<ul></ul>,而不是给每个嵌套的位置是自己的随机位置。在下面的HTML中,我试图给每个类mapStart-second一个独特的随机位置,在这种情况下,它们都得到了相同的位置。
示例HTML
<ul id="mapStart">
<li id="mapStart-first">
<ul class="mapStart-second">
<li></li>
</ul>
<ul class="mapStart-second">
<li></li>
</ul>
</li>
</ul>JS/jQuery
jQuery('#mapStart-first').one('mouseenter', function() {
jQuery(this).find('.mapStart-second').fadeIn('slow').animate({
top: Math.floor((Math.random()*aw_positionY)),
left: Math.floor((Math.random()*aw_positionX)),
});
});变量aw_positionsX和aw_positionY也被动态设置并正确工作。
发布于 2013-09-18 19:52:15
您需要一个循环,否则将向每个动画传递相同的随机值:
jQuery('#mapStart-first').one('mouseenter', function() {
jQuery(this).find('.mapStart-second').fadeIn('slow').each(function() {
$(this).animate({
top: Math.floor((Math.random()*aw_positionY)),
left: Math.floor((Math.random()*aw_positionX)),
});
});
});https://stackoverflow.com/questions/18881074
复制相似问题