复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>匀速运动演示</title>
<style type="text/css">
<!--
#div1 {width:1px;height:200px;background-color: black;position:absolute;left:100px;top:70px;float:left}
#div1 span {position:absolute;top:-15px}
#div2 {width:1px;height:200px;background-color:midnightblue;position:absolute;left:800px;top:70px;float: left}
#div2 span {position:absolute;top:-15px}
#div3 {width:1px;height:200px;background-color:fuchsia;position:absolute;left:300px;top:70px;float: left}
#div3 span {position:absolute;top:-15px}
#div4 {width:1px;height:200px;background-color:darkmagenta;position:absolute;left:500px;top:70px;float: left}
#div4 span {position:absolute;top:-15px}
#grap {width:200px;height:200px;background:red;position:absolute;left:300px;top:74px;float: left;}
input {width:100px;margin-left: 90px;}
-->
</style>
<script type="text/javascript">
var timeId
function startMove(target){
var oDiv=document.getElementById('grap')
clearInterval(timeId); //这个地方就是为了防止多次点击速度加快的现象,很重要
speed=oDiv.offsetLeft<target?8:-9;
timeId=setInterval(function(){
if(Math.abs(oDiv.offsetLeft-target)<=6){
oDiv.style.left=target+'px'; //只要矩形移动到接近到目标点处就直接移动到目标点上,肉眼无法察觉速度变化
clearInterval(timeId); document.title="目标"+oDiv.style.left;
}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
</script>
</head>
<body>
<div id="div1"><span>100px</span></div>
<div id="div2"><span>800px</span></div>
<div id="div3"><span>300px</span></div>
<div id="div4"><span>500px</span></div>
<input type="button" value="移动到100px处" onclick="startMove(100)"/>
<input type="button" value="移动到300px处" onclick="startMove(300)"/>
<input type="button" value="移动到500px处" onclick="startMove(500)"/>
<input type="button" value="移动到800px处" onclick="startMove(800)"/>
<div id="grap" ></div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>匀速运动演示</title>
<style type="text/css">
<!--
#div1 {width:1px;height:200px;background-color: black;position:absolute;left:100px;top:70px;float:left}
#div1 span {position:absolute;top:-15px}
#div2 {width:1px;height:200px;background-color:midnightblue;position:absolute;left:800px;top:70px;float: left}
#div2 span {position:absolute;top:-15px}
#div3 {width:1px;height:200px;background-color:fuchsia;position:absolute;left:300px;top:70px;float: left}
#div3 span {position:absolute;top:-15px}
#div4 {width:1px;height:200px;background-color:darkmagenta;position:absolute;left:500px;top:70px;float: left}
#div4 span {position:absolute;top:-15px}
#grap {width:200px;height:200px;background:red;position:absolute;left:300px;top:74px;float: left;}
input {width:100px;margin-left: 90px;}
-->
</style>
<script type="text/javascript">
var timeId
function startMove(target){
var oDiv=document.getElementById('grap')
clearInterval(timeId); //这个地方就是为了防止多次点击速度加快的现象,很重要
speed=oDiv.offsetLeft<target?8:-9;
timeId=setInterval(function(){
if(Math.abs(oDiv.offsetLeft-target)<=6){
oDiv.style.left=target+'px'; //只要矩形移动到接近到目标点处就直接移动到目标点上,肉眼无法察觉速度变化
clearInterval(timeId); document.title="目标"+oDiv.style.left;
}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
</script>
</head>
<body>
<div id="div1"><span>100px</span></div>
<div id="div2"><span>800px</span></div>
<div id="div3"><span>300px</span></div>
<div id="div4"><span>500px</span></div>
<input type="button" value="移动到100px处" onclick="startMove(100)"/>
<input type="button" value="移动到300px处" onclick="startMove(300)"/>
<input type="button" value="移动到500px处" onclick="startMove(500)"/>
<input type="button" value="移动到800px处" onclick="startMove(800)"/>
<div id="grap" ></div>
</body>
</html>
标签:
JS,匀速运动
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“JS匀速运动演示示例代码”评论...