您好,欢迎访问一九零五行业门户网

怎样使用JS实现缓冲运动

这次给大家带来怎样使用js实现缓冲运动,使用js实现缓冲运动的注意事项有哪些,下面就是实战案例,一起来看一下。
缓冲需要用到数值取整,向上取整:math.ceil()  向下取整math.floor()
移动的速度慢慢减慢的效果,移动速度=(终点位置 - 当前位置) / 一个数
<!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=utf-8" /> <title>js缓冲运动</title> <style> #p{   width:150px;   height:150px;   background:#0c6;   position:absolute;   left:0;   top:50px; } #p2{   background:#000;   height:600px;   position:absolute;   left:500px;   width:2px; } </style> </head> <script> var speed; var time; window.onload = function(){   var btn = document.getelementbyid('btn');   btn.onclick = function(){     speed = 0;     move(500);   };   btn2.onclick = function(){     speed = 0;     move(0);   }; }; function move(e){   var p = document.getelementbyid('p');   clearinterval(time);   time = setinterval(function(){     //改变位置,如果向左则e==500, 向上取整, 否则向右,向下取整,速度=(终点位置 - 当前位置)/一个数     e==500 ? speed = math.ceil((e-(p.offsetleft))/30):speed = math.floor((e-(p.offsetleft))/30)     if (e <= p.style.left){//达到,关闭定时器 clearinterval(time); } else { p.style.left = p.offsetleft+speed+'px'; } },30); }; </script> <body> <input type="button" value="向右运动" id="btn" /> <input type="button" value="向左运动" id="btn2" /> <p id = "p"> </p> <p id = "p2"> </p> </body> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
怎样实现vue项目中使用vux
不用js实现菜单打开关闭
以上就是怎样使用js实现缓冲运动的详细内容。
其它类似信息

推荐信息