JavaScript 缓冲动画代码样例
2017-08-18 11:45
393 查看
原生 JavaScript 缓冲动画代码样例:
<!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>缓冲动画</title> <style type="text/css"> html,body{margin:0;padding:0;} div{margin:0;padding:0;} .odiv{width:200px; height:200px; background:#f00; position:relative; left:-200px; top:100px;} .sdiv{width:20px; height:60px; background:#00f; position:absolute; top:70px; right:-20px;} </style> <script language="javascript"> window.onload = function(){ var odiv = document.getElementById('odiv'); odiv.onmouseover = function(){ startMover(0); } odiv.onmouseout = function(){ startMover(-200); } } var timer = null; function startMover(itarget){//速度和目标值 clearInterval(timer);//执行当前动画同时清除之前的动画 var odiv = document.getElementById('odiv'); timer = setInterval(function(){ var speed = (itarget-odiv.offsetLeft)/10;//缓冲动画的速度参数变化值 //如果速度是大于0,说明是向右走,那么就向上取整 speed = speed>0?Math.ceil(speed):Math.floor(speed); //Math.floor();向下取整 if(odiv.offsetLeft == itarget){ clearInterval(timer); } else{ //clientLeft 返回对象的offsetLeft属性值和到当前窗口左边的真实值之间的距离 odiv.style.left = odiv.offsetLeft+speed+'px'; } },30); } </script> </head> <body> <div id="odiv" class="odiv"> <div id="sdiv" class="sdiv"> </div> </div> </body> </html>
相关文章推荐
- javascript匀速动画和缓冲动画
- JavaScript实现鼠标放上动画加载大图的代码
- 用JavaScript实现三级动画菜单代码
- javascript 缓冲效果 实现代码
- JavaScript 动画之缓冲运动
- JavaScript动画缓冲算法总结 转
- javascript动画对象支持加速、减速、缓入、缓出的实现代码
- 基于JavaScript实现回到页面顶部动画代码
- javascript 通用loading动画效果实例代码
- javascript 小型动画组件与实现代码
- JavaScript原生折叠扩展收缩菜单带缓冲动画
- 一些自学的html/javascript代码(setTimeout, alert, 定格动画)
- javascript动画效果之多物体缓冲运动
- 40行JavaScript代码实现的3D旋转魔方动画效果
- JavaScript实现带缓冲效果的随屏滚动漂浮广告代码
- javascript 缓冲效果 实现代码
- javascript 缓冲效果实现代码 推荐
- javascript匀速动画和缓冲动画