这个想法是让一定数量的元素摇摆,并且只使用一个函数来做到这一点(我认为这是最好的选择)。我制作了一个可以让单个元素摇摆的函数,效果非常好,但后来我试图让它适用于多个元素,但我遇到了真正的问题。
更复杂的是,摇摆函数的设计目的是在每次调用时产生略微不同的直径和速度,因此摇摆看起来很自然。它还可以无限期地循环运动。
因此,该函数分为三个部分,第一个设置它将应用到的元素数组,第二个(称为 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>
我预计该函数可能会卡在循环的第一个数组元素上,现在基本代码已经修复,我正好处于这种情况。有没有人有任何想法如何解决这个问题或替代方法。
CSS 非常简单,只是一些基本的样式
.box {
width:100px;
height:100px;
position: absolute;
background-color:red;}
.html .body {position: absolute;}