38、JavaScript的运动----分享到侧边栏
2016-06-20 16:13
393 查看
1、“分享到”侧边栏
1.1 传三个参数的函数(运动对象的id,速度,目标点)
1.2 传两个参数的函数(运动对象的id,目标点)
– 通过目标点,计算速度值
1.1 传三个参数的函数(运动对象的id,速度,目标点)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #div1 { width: 100px; height: 200px; background: #CCC; position: absolute; /*用于隐藏*/ left: -100px; } #div1 span { width: 20px; height: 60px; line-height: 20px; text-align: center; left: 100px; top: 70px; background: yellow; position: absolute; } </style> <script type="text/javascript"> window.onload = function() { var oDiv = document.getElementById('div1'); oDiv.onmouseover = function() { startMove('div1', 10, 0); } oDiv.onmouseout = function() { startMove('div1', -10, -100); } } var timer = null; /** * 运动框架的实现 * @param {Object} objId 要运动的对象的id * @param {Object} iSpeed 运动的速度 * @param {Object} iTarget 运动的目标点 */ function startMove(objId, iSpeed, iTarget) { var oDiv = document.getElementById(objId); clearInterval(timer); timer = setInterval(function() { if (oDiv.offsetLeft == iTarget) { clearInterval(timer); } else { oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px'; } }, 30); } </script> </head> <body> <div id="div1"> <span>分享到</span> </div> </body> </html>
1.2 传两个参数的函数(运动对象的id,目标点)
– 通过目标点,计算速度值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #div1 { width: 100px; height: 200px; background: #CCC; position: absolute; left: -100px; } #div1 span { width: 20px; height: 60px; line-height: 20px; text-align: center; left: 100px; top: 70px; background: yellow; position: absolute; } </style> <script type="text/javascript"> window.onload = function() { var oDiv = document.getElementById('div1'); oDiv.onmouseover = function() { startMove('div1', 0); } oDiv.onmouseout = function() { startMove('div1', -100); } } var timer = null; /** * 实现运动的框架 * @param {Object} objID 要运动的对象的id * @param {Object} iTarget 运动的目标点 */ function startMove(objID, iTarget) { var oDiv = document.getElementById(objID); clearInterval(timer); timer = setInterval(function() { var iSpeed = 0; /** * 通过目标点,来计算运动的速度大小 */ if (oDiv.offsetLeft < iTarget) { iSpeed = 10; } else { iSpeed = -10; } if (oDiv.offsetLeft == iTarget) { clearInterval(timer); } else { oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px'; } }, 30); } </script> </head> <body> <div id="div1"> <span>分享到</span> </div> </body> </html>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解
- call/apply/bind 的理解与实例分享