javascript运动框架多物体运动---1
2015-08-04 15:23
746 查看
<head> <meta charset="UTF-8"> <title>test</title> <style> * { margin: 0; padding: 0; } textarea { position: fixed; right: 0; top: 50px; } div { margin-top: 10px; width: 100px; height: 30px; background: red; border: 1px solid green; cursor: pointer; } </style> </head> <body style="height:3000px;"> <textarea name="" id="" cols="30" rows="10"></textarea> <div>div1</div> <div>div2</div> <div>div3</div> <script src="test.js"></script> <script> window.onload = function() { var oDiv1 = document.getElementsByTagName('div'); for (var i = 0; i < oDiv1.length; i++) { // 给每个元素对象设置一个定时器 // 用来结局定时器公用的问题 oDiv1.timer = null; oDiv1[i].onmouseover = function() { move(this, 400); }; oDiv1[i].onmouseout = function() { move(this, 100); }; } } </script> </body> /** * [getStyle 获取计算出来的样式] * @param {[type]} obj [元素对象] * @param {[type]} attr [属性名] * @return {[type]} [对应属性名的值] */ function getStyle(obj, attr) { if (obj.currentStyle) { // IE return obj.currentStyle[attr]; } else { // 其他 return getComputedStyle(obj, false)[attr]; } } function move(obj, target) { // 多物体运动,解决公用定时器的问题 // 不仅仅是定时器,多物体运动所有东西都不能公用 clearInterval(obj.timer); obj.timer = setInterval(function() { var width = parseInt(getStyle(obj, 'width')); var speed = (target - width) / 8; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (width == target) { clearInterval(obj.timer); } else { obj.style.width = width + speed + 'px'; } document.getElementsByTagName('textarea')[0].value += parseInt(getStyle(obj, 'width')) + '\n'; }, 30); }
相关文章推荐
- Emberjs路由
- JS实现转动随机数抽奖的特效代码
- js中window的属性
- JavaScript实现跨浏览器的添加及删除事件绑定函数实例
- js解码函数 escape,encodeURI,encodeURIComponent比较
- javascript超链接的变换
- js中&& 和 ||
- JavaScript实现LI列表数据绑定的方法
- JSON Compression algorithms
- javascript运动框架---3--匀速运动
- JSP三秒自动跳转到另一页
- JSONKIT isa错误的解决办法
- 用js编写的简单的计算器代码程序
- javascript运动框架---2--缓冲运动
- 从jsTree演示代码中提取的在线文件查看
- 关于SSH整合JSON
- Json数据处理方法
- 05-js 操作select和option
- JavaScript 字符串转换数字
- 禁止右键js