0

我有 3 张图像(汽车)要在屏幕上移动。当我点击“开始”按钮时,相关的汽车应该在屏幕上移动。汽车 1、汽车 2 和汽车 3。每个按钮都需要激活三个不同的定时器事件。每辆车都应该使用“margin-left”样式属性移动,同时通过行进的像素数量增加距离参数。

我也想要一个赢家。无论哪辆车越过或重叠检查的图像,都应显示一个警告框,指示获胜者并停止所有计时器。

这是我迄今为止所拥有的:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome to div races!</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
    .style1
    {
        width: 134px;
        height: 51px;
    }
</style>
</head>

<body>

<div id="header">Div Races!</div>

<div id="track">

<div class="lane"><div id="Car1">
  <img id="Car1" alt="Car1" class="style1" longdesc="Car1" 
      src="Car1.jpg" /></div></div><!--Car1-->
<button onclick="Car1()">Start</button>

<div class="lane"><div id="Car2">
  <img id="Car2" alt="Car2" class="style1" longdesc="Car2" 
      src="Car2.jpg" /></div></div><!--Car2-->
<button onclick="Car2()">Start</button>

<div class="lane"><div id="Car3">
  <img id="Car3" alt="Car3" class="style1" longdesc="Car3" 
      src="Car3.jpg" /></div></div><!--Crankshaft-->
<button onclick="Car3()">Start</button>

</div><!--track-->

<div id="footer">Click on a start button to start a racecar!</div>

</body>
</html>




function Car1() {
var animate, left = 0, imgObj = null;
init();
    function init(){

       imgObj = document.getElementById('rusty');
       imgObj.style.position= 'absolute';
       imgObj.style.top = '240px';
       imgObj.style.left = '-300px';
       imgObj.style.visibility='hidden';

       moveRight();
    }

    function moveRight(){
        left = parseInt(imgObj.style.left, 10);

        if (10 >= left) {
            imgObj.style.left = (left + 5) + 'px';
            imgObj.style.visibility='visible';

            animate = setTimeout(function(){moveRight();},20); 

            //stopanimate = setTimeout(moveRight,20);
        } else {
            stop();
        }
        //f();
    }

    function stop(){
       clearTimeout(animate);
    }

window.onload = function() {init();};
}

function Car2() {
var animate, left = 0, imgObj = null;
init();
    function init(){

       imgObj = document.getElementById('dusty');
       imgObj.style.position= 'absolute';
       imgObj.style.top = '240px';
       imgObj.style.left = '-300px';
       imgObj.style.visibility='hidden';

       moveRight();
    }

    function moveRight(){
        left = parseInt(imgObj.style.left, 10);

        if (10 >= left) {
            imgObj.style.left = (left + 5) + 'px';
            imgObj.style.visibility='visible';

            animate = setTimeout(function(){moveRight();},20); 

            //stopanimate = setTimeout(moveRight,20);
        } else {
            stop();
        }
        //f();
    }

    function stop(){
       clearTimeout(animate);
    }

window.onload = function() {init();};
}

function Car3() {
var animate, left = 0, imgObj = null;
init();
    function init(){

       imgObj = document.getElementById('crankshaft');
       imgObj.style.position= 'absolute';
       imgObj.style.top = '240px';
       imgObj.style.left = '-300px';
       imgObj.style.visibility='hidden';

       moveRight();
    }

    function moveRight(){
        left = parseInt(imgObj.style.left, 10);

        if (10 >= left) {
            imgObj.style.left = (left + 5) + 'px';
            imgObj.style.visibility='visible';

            animate = setTimeout(function(){moveRight();},20); 

            //stopanimate = setTimeout(moveRight,20);
        } else {
            stop();
        }
        //f();
    }

    function stop(){
       clearTimeout(animate);
    }

window.onload = function() {init();};
}

我在正确的轨道上吗?我使用的代码是在网站上找到的。任何建议将不胜感激。

4

0 回答 0