<title>无标题文档</title>
<style>
#div1{width:200px;height:200px; background:red; position:absolute; left:0px; top:50px;}
</style>
<script>
var timer=null;
function startMove()
{
var oDiv=document.getElementById('div1');
clearInterval(timer); //为避免同时开启多个定时器
timer=setInterval(function (){
var speed=10;
if(oDiv.offsetLeft>300)
{
clearInterval(timer);
}
else //if...else避免在到达终点位置后按开始按钮再次执行else里面的语句而使物体运动
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
</script>
</head> <body>
<input id="btn1" type="button" value="开始运动" onclick="startMove()"/>
<div id="div1">
</div>
</body>

运动框架及应用

运动框架

1.在开始运动时,关闭已有定时器

2.把运动和停止隔开(if/else)

运动框架实例分享

1.”分享到“侧边栏

通过目标点,计算速度值

<title>无标题文档</title>
<style>
#div1{width:150px; height:200px; background:green; position:absolute; left:-150px;}
#div1 span{position:absolute; width:20px; height:60px; line-height:20px; background:blue; right:-20px;top:70px;}
</style>
<script>
window.onload=function ()
{
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function ()
{
startMove();
}
oDiv.onmouseout=function ()
{
startMove2();
}
};
var timer=null; function startMove()
{
var oDiv=document.getElementById('div1'); clearInterval(timer);
timer=setInterval(function (){
if(oDiv.offsetLeft==0)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+10+'px';
}
},30);
}
function startMove2()
{
var oDiv=document.getElementById('div1'); clearInterval(timer);
timer=setInterval(function (){
if(oDiv.offsetLeft==-150)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft-10+'px';
}
},30);
}
</script>
</head> <body>
<div id="div1">
<span>分享到</span>
</div>
</body>

改进1

window.onload=function ()
{
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function ()
{
startMove(10,0);
}
oDiv.onmouseout=function ()
{
startMove(-10,-150);
}
};
var timer=null; function startMove(speed,iTarget)
{
var oDiv=document.getElementById('div1'); clearInterval(timer);
timer=setInterval(function (){
if(oDiv.offsetLeft==iTarget)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
} </script>

改进2

window.onload=function ()
{
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function ()
{
startMove(0);
}
oDiv.onmouseout=function ()
{
startMove(-150);
}
};
var timer=null; function startMove(iTarget)
{
var oDiv=document.getElementById('div1'); clearInterval(timer);
timer=setInterval(function (){
var speed=0; if(oDiv.offsetLeft>iTarget)
{
speed=-10;
}
else
{
speed=10;
} if(oDiv.offsetLeft==iTarget)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
} </script>

JS-运动基础(一)的更多相关文章

  1. 第八节 JS运动基础

    运动基础 让Div运动起来 速度——物体运动的快慢 运动中的Bug 不会停止 速度取某些值会无法停止 到达位置后再点击还会运动 重复点击速度加快 匀速运动(速度不变) 运动框架及应用: 运动框架: 在 ...

  2. 学习blus老师js(6)--js运动基础

    运动基础 一.匀速运动 运动框架 在开始运动时,关闭已有定时器 把运动和停止隔开(if/else) <!DOCTYPE HTML> <html> <head> &l ...

  3. JS运动基础(一)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. js运动基础2(运动的封装)

    简单运动的封装 先从最简单的封装开始,慢慢的使其更丰富,更实用. 还是上一篇博文的代码,在此不作细说. 需求:点击按钮,让元素匀速运动. <!DOCTYPE html> <html ...

  5. JS运动基础(四) 碰撞运动

    碰撞运动撞到目标点,速度反转无重力的漂浮Div速度反转滚动条闪烁的问题过界后直接拉回来 加入重力反转速度的同时,减小速度纵向碰撞,横向速度也减小横向速度小数问题(负数) <!DOCTYPE HT ...

  6. JS运动基础(三) 弹性运动

    加减速运动速度不断增加或减少速度减小到负值,会向反方向运动 弹性运动在目标点左边,加速:在目标点右边,减速根据距离,计算加速度 带摩擦力的弹性运动弹性运动+摩擦力 弹性:速度 += (目标点 - 当前 ...

  7. JS运动基础(二) 摩擦运动、缓冲运动

    摩擦运动: 逐渐变慢,最后停止 缓冲运动: 与摩擦力的区别:可以精确的停到指定目标点距离越远速度越大速度由距离决定速度=(目标值-当前值)/缩放系数Bug:速度取整值取整: iSpeed = iSpe ...

  8. JS运动基础

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. Javascript 运动基础 01

    JS运动基础  运动基础   让Div运动起来 速度——物体运动的快慢 运动中的Bug 不会停止 速度取某些值会无法停止 到达位置后再点击还会运动 重复点击速度加快   匀速运动 速度不变 <s ...

  10. JS学习-基础运动

    多物体运动 多个物体用同一个函数时,函数里定义的定时器应该要每个物体对应一个定时器名称,不然会导致未完成运动就被关闭了,因为定时器名称一样,而开启定时器前会清除一下. obj.timer 多值同时运动 ...

随机推荐

  1. HDU 1258 Sum It Up(DFS)

    题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...

  2. LeetCode OJ 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. js事件冒泡和捕捉

    (1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...

  4. html5实现滚动文字

    <div class="custom-notice"> <i class="icon-notice"></i> <ma ...

  5. Centos6.6升级python版本

    centos原生python为2.6.6,可以通过下面的命令查看 #python -V Python 注:在安装新版本前,请先安装zlib\openssl组件,如果你确认你用不到这个,也可以不装 需要 ...

  6. 理解 bashrc 和 profile(转)

    转自:https://wido.me/sunteya/understand-bashrc-and-profile/ 在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 p ...

  7. 东芝MIPI解码TC358746AXBG和OV6211使用及配置

    Camera Sensor常见的接口类型: 1.有并口信号(D0~D7.PCLK.HSYNC.VSYNC),一般的处理器有DCMI接口,如ST32F207x系列,直接相连就可以使用. 2.MIPI接口 ...

  8. DOM操作-遍历HTML文档内容

    基础:   JS nodeType返回类型:http://blog.csdn.net/qyf_5445/article/details/9232907 代码: <!DOCTYPE html> ...

  9. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  10. Canvas 数学、物理、动画学习笔记一

    Canvas 第五章 数学.物理和运动学习笔记让人映像深刻的运动,需要我们不只是简单的知道如何移动对象,还需要知道怎么按用户期望看到的方式去移动它们.这些需要基于数学知识的基本算法和物理学作用.基于点 ...