js跑步算法的實現(xiàn)代碼
先復制一下,看看運行的效果吧,其中用到的精髓是setInterval()方法:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>JavaScript</title>
<style>
html
{
background-color:silver;
}
.point1
{
position:absolute;
left:10px;
top:40px;
}
.point2
{
position:absolute;
left:100px;
top:40px;
}
.hr1
{
position:absolute;
top:60px;
}
</style>
<script type="text/JavaScript">
document.onmousedown = mousedown;
document.onmouseup = mouseup;
var intervalProcess;
var direct = true;
function mousedown(){
intervalProcess = setInterval("MovePoint()", 1);
}
function mouseup(){
clearInterval(intervalProcess);
}
function MovePoint(){
with (document.getElementById("point1").style){
if (isNaN(parseInt(left)))
left = "10px";
else {
document.getElementById("point2").style.left = "200px";
if (parseInt(left) < 0)
direct = true;
if (parseInt(left) > parseInt(document.getElementById("point2").style.left))
direct = false;
if (direct)
left = parseInt(left) + 1 + "px";
else
left = parseInt(left) - 1 + "px";
}
}
}
</script>
</head>
<body>
<div class="point1" id="point1"><font color=blue>a</font></div>
<div class="point2" id="point2"><font color=red>b</font></div>
<hr class="hr1" />
</body>
</html>
相關文章
js中方法重載如何實現(xiàn)?以及函數(shù)的參數(shù)問題
js中沒有辦法直接實現(xiàn)方法重載,但每一個函數(shù)都有一個特殊的參數(shù)arguments,利用它可以實現(xiàn)方法的重載,具體示例如下2013-08-08JSON+HTML實現(xiàn)國家省市聯(lián)動選擇效果
實現(xiàn)國家省市聯(lián)動的方法有很多,本文要為大家介紹的JSON+HTML如何實現(xiàn),需要的朋友可以參考下2014-05-05基于JavaScript實現(xiàn)購物網(wǎng)站商品放大鏡效果
大家在日常生活中都有網(wǎng)購的經(jīng)驗,有的網(wǎng)站會有商品放大鏡功能,效果非常棒,那么基于js代碼是如何實現(xiàn)的呢?下面小編給大家?guī)砹嘶趈s實現(xiàn)購物網(wǎng)站商品放大鏡效果,非常不錯,感興趣的朋友參考下吧2016-09-09