如何在多个元素上同时运行一个jQuery动画函数?

如何在多个元素上同时运行一个jQuery动画函数?,jquery,animation,Jquery,Animation,这个想法是让一定数量的元素摇摆,并且只使用一个函数(我认为这是最好的选择)。我做了一个函数,可以使一个元素摇摆,它运行得非常好,但后来我尝试将其应用于多个元素,结果遇到了实际问题 更复杂的是,每次调用sway函数时,sway函数都会生成稍微不同的直径和速度,因此sway看起来很自然。它还可以无限期地循环运动 因此,该函数分为三部分,第一部分设置它将应用到的元素数组,第二部分(称为main)生成元素摆动所需的变量,第三部分(称为act)运行并循环摆动动画 <!DOCTYPE HTML PUB

这个想法是让一定数量的元素摇摆,并且只使用一个函数(我认为这是最好的选择)。我做了一个函数,可以使一个元素摇摆,它运行得非常好,但后来我尝试将其应用于多个元素,结果遇到了实际问题

更复杂的是,每次调用sway函数时,sway函数都会生成稍微不同的直径和速度,因此sway看起来很自然。它还可以无限期地循环运动

因此,该函数分为三部分,第一部分设置它将应用到的元素数组,第二部分(称为main)生成元素摆动所需的变量,第三部分(称为act)运行并循环摆动动画

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Balloon Proof of Concept Page</title>

    <link rel="stylesheet" type="text/css" href="main.css">

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var now;
            var balloons = new Array();
            balloons[0] = "#box1"
            balloons[1] = "#box2"
            balloons[2] = "#box3"

            var main = function(hello) {

                var speed = Math.random() * (2500 - 2000 + 1) + 2000;
                var change = Math.random() * (35 - 10 + 1) + 10;

                var act = function() {
                    $(hello).animate({
                        left : change
                    }, speed);
                    $(hello).animate({
                        left : -change
                    }, speed);
                    act();
                }
                act();
            }

        for( now = 0; now < balloons.length; now++) {
                main(balloons[now]);
            }
        });
    </script>
</head>

<body>

    <div class="box" id="box1" style="margin-left:300px; margin-top:60px">
        Proj 1
    </div>
    <div class="box" id="box2" style="margin-left:500px; margin-top:20px">
        Proj 2
    </div>
    <div class="box" id="box3" style="margin-left:700px; margin-top:50px">
        Proj 3
    </div>

</body>
</html>

调用
.animate()
时,它会对动画进行排队,然后立即返回

由于您随后再次递归调用
act
,因此代码将进入无限循环

请尝试以下方法:

function act() {
    $(hello)
       .animate({ left :  change }, speed)
       .animate({ left : -change }, speed, act);
}

i、 e.注册
act
作为动画第二阶段的完成回调。

您可以使用计时器,这将在一段时间后重复移动

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Balloon Proof of Concept Page</title>

    <link rel="stylesheet" type="text/css" href="main.css">

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var now;
            var balloons = new Array();
            balloons[0] = "#box1"
            balloons[1] = "#box2"
            balloons[2] = "#box3"

            var main = function(hello) {

                var speed = Math.random() * (2500 - 2000 + 1) + 2000;
                var change = Math.random() * (35 - 10 + 1) + 10;

                var act = function() {
                    $(hello).animate({
                        left : change
                    }, speed);
                    $(hello).animate({
                        left : -change
                    }, speed);
                    act();
                }
                act();
            }

        for( now = 0; now < balloons.length; now++) {
                main(balloons[now]);
            }
        });
    </script>
</head>

<body>

    <div class="box" id="box1" style="margin-left:300px; margin-top:60px">
        Proj 1
    </div>
    <div class="box" id="box2" style="margin-left:500px; margin-top:20px">
        Proj 2
    </div>
    <div class="box" id="box3" style="margin-left:700px; margin-top:50px">
        Proj 3
    </div>

</body>
</html>
<script type="text/javascript">                                                                                                                                                                                                                
        $(document).ready(function() {                                                                                                                                                                                                             
            var now;                                                                                                                                                                                                                               
            var balloons = new Array();                                                                                                                                                                                                            
            balloons[0] = "#box1"                                                                                                                                                                                                                  
            balloons[1] = "#box2"                                                                                                                                                                                                                  
            balloons[2] = "#box3"                                                                                                                                                                                                                  

            var main = function(hello) {                                                                                                                                                                                                           

                var speed = Math.random() * (2500 - 2000 + 1) + 2000;                                                                                                                                                                              
                var change = Math.random() * (35 - 10 + 1) + 10;                                                                                                                                                                                   

                var act = function() {                                                                                                                                                                                                             
                    $(hello).animate({                                                                                                                                                                                                             
                        left : change                                                                                                                                                                                                              
                    }, speed);                                                                                                                                                                                                                     
                    $(hello).animate({                                                                                                                                                                                                             
                        left : -change                                                                                                                                                                                                             
                    }, speed);                                                                                                                                                                                                                     
                }                                                                                                                                                                                                                              
                act();                                                                                                                                                                                                                             
                setInterval( act, 1000);                                                                                                                                                                                                           

            }                                                                                                                                                                                                                                      
var now = 0;                                                                                                                                                                                                                               
for( now = 0; now < balloons.length; now++) {                                                                                                                                                                                              
     main(balloons[now]);                                                                                                                                                                                                               
}                                                                                                                                                                                                                                          

});                                                                                                                                                                                                                                        

$(文档).ready(函数(){
var现在;
var ballobs=新数组();
气球[0]=“#盒子1”
气球[1]=“盒子2”
气球[2]=“盒子3”
var main=函数(hello){
var speed=Math.random()*(2500-2000+1)+2000;
var change=Math.random()*(35-10+1)+10;
var act=function(){
$(您好)。设置动画({
左:改变
},速度);
$(您好)。设置动画({
左:-改变
},速度);
}                                                                                                                                                                                                                              
act();
设置间隔(act,1000)
$(function(){
    function act(jqObj, speed, change) {
        jqObj.animate({
            'left': change // stage 1
        }, speed).animate({
            'left': -change // stage 2
        }, speed, function(){
            act(jqObj, speed, change); // repeat stages 1 and 2 (indefinitely)
        });
    };
    var balloons = [ // array of jQuery objects.
        $("#box1"),
        $("#box2"),
        $("#box3")
    ];
    for (i = 0; i < balloons.length; i++) {
        var speed = 2000 + Math.random() * 501;
        var change = 10 + Math.random() * 26;
        act(balloons[i], speed, change);
    }
});