setInterval图片缓冲(js)
2017-11-13 23:14
225 查看
css:
html:
js:
#div{ position: absolute; left:800px; top:30px; background: yellowgreen; width:100px; height:100px; } #target{ width:1px; height:300px; background: black; position: absolute; left:300px; top:0; }
html:
<!--终点线--> <div id="target"></div> <!--终点线--> <div id="div"></div> <input type="button" value="点击运动" id="btn">
js:
var btn=document.getElementById("btn"); btn.onclick=function () { startMove(300) } var timer=null; var startMove=function (oTarget) { clearInterval(timer); var odiv=document.getElementById("div"); timer=setInterval(function () { var ispeed=(oTarget-odiv.offsetLeft)/8; ispeed>0?ispeed=Math.ceil(ispeed):ispeed=Math.floor(ispeed);//准确到达终点的位置 - 三目运算 // if(ispeed>0){ //准确到达终点的位置 // ispeed=Math.ceil(ispeed) // }else{ // ispeed=Math.floor(ispeed) // } if(odiv.offsetLeft<=oTarget){ clearInterval(timer); }else{ odiv.style.left=odiv.offsetLeft+ispeed+"px"; } },30) }
相关文章推荐
- js图片预加载---for与onload事件,图片数据模型到界面的单项绑定,使用interval模拟循环操作
- JS黑魔法之this, setTimeout/setInterval, arguments
- 缓冲加载图片的 jQuery 插件 lazyload.js 使用方法详解
- 缓冲加载图片的 jQuery 插件 lazyload.js 使用方法详解
- JS setInterval()、 setInterval()与 clearInterval() 方法的使用
- JQuery缓冲加载图片插件lazyload.js的使用方法
- 缓冲加载图片的 jQuery 插件 lazyload.js 使用方法详解
- js 多线程 setTimeout setInterval
- js setInterval不能访问外网
- 漂亮的js tab图片轮换效果代码(可自定义的幻灯片和图片缓冲切换)
- js setTimeout and setInterval
- setInterval.js
- JS中图片缓冲loading技术的实例代码
- js中setInterval/clearInterval与setTimeout/clearTimeout
- js之setInterval与setTimeout函数传参方法
- 缓冲加载图片的jQuery插件lazyload.js 使用方法详解
- js setInterval() 用法示例
- 缓冲加载图片的jQuery插件lazyload.js 真正省资源用法
- JS setInterval() 与 clearInterval() 方法的使用
- JQuery缓冲加载图片插件lazyload.js的使用方法