javascript - 独立为同一类的元素设置动画

标签 javascript jquery

我有一个包含多个嵌套无序列表的无序列表。我正在尝试为多个嵌套 <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 也正在动态设置并正常工作。

最佳答案

您需要一个循环,否则您将向每个动画传递相同的随机值:

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)),
        });   
    });  
});

关于javascript - 独立为同一类的元素设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18881074/

相关文章:

javascript - 页面转换后调用 jquery 事件

javascript - 如何使用 jquery/javascript 删除数组中的特定对象

javascript - 使用 JavaScript 将 _target ="blank"动态更改为 _target ="self"

javascript - AngularJS - 无法使用工厂调用http服务

jQuery 如何获取除第一行之外的所有行 HTML?

javascript - jquery如何删除数组中的特定值

javascript - 移动页面在顶部时div垂直居中,但是当我们向下滚动页面时,出现错误

javascript - 使用 Javascript(或任何其他语言)的浏览器/选项卡关闭检测

javascript - 使用 Jasmine 进行异步回调测试

javascript - 如何使用 javascript 统计包含 html 标签的字数?