使用 DHTML 和 JavaScript 移动对象

Using DHTML and JavaScript to move object

我卡住了。我是一名正在学习 JavaScript 的学生,这项作业让我很不爽……为什么?因为没有点text/tutorials等帮助。被告知要研究代码并使其运行。最初它从左向右移动......能够改变它并让它从上到下移动。我被卡住的地方不是我必须移动它在页面上沿对角线移动。我找不到任何东西......请帮忙。不是要求这样做,而是给出教程方向的提示或指向...

这是我现在的代码:

<html>
<head>
    <title>DHTML</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <script language="JavaScript" type="text/JavaScript">
    <!--

        var row = 0;

        function moveBoatRight()
       {  //function moves boat towards the right shore     

            document.getElementById("Man").style.top=100+(row*12);  //This accesses the graphic on the page and moves its position

            if (row < 20)
            {
                 row++;  //only need 20 iterations to make it across the river
                window.setTimeout("moveBoatRight();",130);  //Recursive function that gives a slight delay in time to look more fulid
            } 

       }//end of moveBoatRight Function

     //-->
    </script>
</head>

<body bgcolor="#FFFFCE" >
                <h1><strong>Using DHTML</strong></h1>

                    <div id="Man" style="position:relative; left:100px; top:100px; visibility:visible; y-index:1; width: 100px; height: 103px;"><img border="0" src="arrowDown.png" width="95" height="103"></div> 
    </div>

    <form>
        <input type="button" id="sbutton" value="submit" onClick="moveBoatRight();">
    </form>

</body>

这将为您解决问题:

function moveBoatdiagonally() { 

        document.getElementById("Man").style.top=100+(row*12);  //This accesses the graphic on the page and moves its position
        document.getElementById("Man").style.left=100+(row*12);  //This accesses the graphic on the page and moves its position

        if (row < 20) {
             row++;  //only need 20 iterations to make it across the river
            window.setTimeout("moveBoatRight();",130);  //Recursive function that gives a slight delay in time to look more fulid
        } 

   }

如您所见,在循环的每次迭代中,您首先在 y 方向移动 "boat",然后在 x 方向移动。结果看起来像对角线方向。

如果您出于某些原因愿意,当然可以切换它们。

背景是您有一个矩形像素网格,以及一种实际移动对象的非常快速的方法。你无法注意到它实际上是两个截然不同的动作。