js运动原理2
来源:互联网 发布:python下载安装 编辑:程序博客网 时间:2024/06/05 07:21
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#div1{
width: 100px;
height: 100px;
background: red;
position:absolute;
top: 50px;
left:0;
}
#div2{
width: 100px;
height: 100px;
background: red;
position:absolute;
top: 250px;
left:600px;
}
</style>
</head>
<body>
<input id="bt1" type="button" value="开始向右">
<input id="bt2" type="button" value="开始向左">
<div id="div1"></div>
<script type="text/javascript">
var oBt2=document.getElementById("bt2");
var oDiv1Timerid=null;
var oBt=document.getElementById("bt1");
oBt.onclick= function () {
var oDiv=document.getElementById("div1");
clearInterval(oDiv1Timerid); //1. 启动定时器之前,需要清理之前的定时器。
oDiv1Timerid=setInterval(function(){//启动定时器,并记录返回值(定时器的标识)
//做运动的部分 和 清理定时器的部分, 放在 if else 里面
if(oDiv.offsetLeft>600){
//清理定时器的 条件 。要注意边界判断!
clearInterval(oDiv1Timerid);
}
else{
oDiv.style.left=oDiv.offsetLeft+9+"px";
}
},30);
}
oBt2.onclick= function () {
var oDiv=document.getElementById("div1");
clearInterval(oDiv1Timerid);
oDiv1Timerid=setInterval(function(){
if(oDiv.offsetLeft<0){
clearInterval(oDiv1Timerid);
}
else{
oDiv.style.left=oDiv.offsetLeft-9+"px";
}
},30);
}
</script>
</body>
</html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#div1{
width: 100px;
height: 100px;
background: red;
position:absolute;
top: 50px;
left:0;
}
#div2{
width: 100px;
height: 100px;
background: red;
position:absolute;
top: 250px;
left:600px;
}
</style>
</head>
<body>
<input id="bt1" type="button" value="开始向右">
<input id="bt2" type="button" value="开始向左">
<div id="div1"></div>
<script type="text/javascript">
var oBt2=document.getElementById("bt2");
var oDiv1Timerid=null;
var oBt=document.getElementById("bt1");
oBt.onclick= function () {
var oDiv=document.getElementById("div1");
clearInterval(oDiv1Timerid); //1. 启动定时器之前,需要清理之前的定时器。
oDiv1Timerid=setInterval(function(){//启动定时器,并记录返回值(定时器的标识)
//做运动的部分 和 清理定时器的部分, 放在 if else 里面
if(oDiv.offsetLeft>600){
//清理定时器的 条件 。要注意边界判断!
clearInterval(oDiv1Timerid);
}
else{
oDiv.style.left=oDiv.offsetLeft+9+"px";
}
},30);
}
oBt2.onclick= function () {
var oDiv=document.getElementById("div1");
clearInterval(oDiv1Timerid);
oDiv1Timerid=setInterval(function(){
if(oDiv.offsetLeft<0){
clearInterval(oDiv1Timerid);
}
else{
oDiv.style.left=oDiv.offsetLeft-9+"px";
}
},30);
}
</script>
</body>
</html>
0 0
- js运动原理2
- js运动原理
- js运动(2)----缓冲运动
- JS运动----(2)
- JS 运动框架2 链式运动
- js运动-链式运动
- js运动-同时运动
- js运动-弹性运动
- js运动-碰撞运动
- js运动-运动缓冲
- js运动框架完美运动框架2--这个才有效
- js简单运动框架--2 多属性运动
- js缓冲运动、弹性运动、碰撞运动
- js运动-多物体运动
- js运动-完美运动框架
- js运动--同时运动框架
- js运动技术--缓冲运动
- js动画2 多物体运动
- 如何正确使用开源项目?
- 排序算法
- 机器学习实践指南(三)—— 算法的调试
- [剑指offer]数组中只出现一次的数字
- 从网络获取数据(2)使用SyncAdapter
- js运动原理2
- 目录操作
- 代理模式
- struts2实现文件的下载
- 如何选择开源项目?
- Linux-NFS服务安装配置
- Retrofit+OKHttp 教你怎么持久化管理Cookie
- Tomcat 的三种(bio、nio、apr) 高级 Connector 运行模式
- 如何根据字数多少获取字符串对应高度