首页 > 其他 > 详细

运动——匀速运动然后停止

时间:2016-03-11 13:45:38      阅读:195      评论:0      收藏:0      [点我收藏+]

代码:

 

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {
width: 100px;
height: 100px;
background: #ff4b46;
position: absolute;
left: 100px;
top: 50px;

}

#vertical_line {
width: 1px;
height: 300px;
position: absolute;
left: 300px;
top: 0;
background: black;
}
</style>
<script>
var timer = null;
function startMove(iTarget) {

clearInterval(timer);
timer = setInterval(function () {
var oDiv = document.getElementById("div1");

var speed = 0;
if (oDiv.offsetLeft < iTarget) {
speed = 7;
}
else {
speed = -7;
}
if (Math.abs(iTarget - oDiv.offsetLeft) <= 7) {
clearInterval(timer);
oDiv.style.left = iTarget + ‘px‘;
} else {
speed = 7;
oDiv.style.left = oDiv.offsetLeft + speed + "px";
document.title = oDiv.offsetLeft + ‘,‘ + speed;
}
}, 30);
}


</script>
</head>
<body>
<input type="button" value="开始运动" onclick="startMove(300)">

<div id="div1">


</div>
<div id="vertical_line"></div>

</body>
</html>

 

运行结果:

初始界面:

  技术分享

点击鼠标之后界面:

  技术分享

运动——匀速运动然后停止

原文:http://www.cnblogs.com/theWayToAce/p/5265210.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!