js缓冲运动、弹性运动、碰撞运动
来源:互联网 发布:centos 7 xfce4 中文 编辑:程序博客网 时间:2024/06/12 00:57
适用于图片轮播,物体运动等。
缓冲运动
function startMove(obj, json, fnEnd) //json :{width:300,height:400}{ clearInterval(obj.timer); obj.timer=setInterval(function (){ var bStop=true; //假设:所有值都已经到了 for(var attr in json) { var cur=0; //取得当前值 if(attr=='opacity') { cur=Math.round(parseFloat(getStyle(obj, attr))*100); } else { cur=parseInt(getStyle(obj, attr)); } var speed=(json[attr]-cur)/6; //速度 speed=speed>0?Math.ceil(speed):Math.floor(speed); if(cur!=json[attr]) bStop=false; if(attr=='opacity') { obj.style.filter='alpha(opacity:'+(cur+speed)+')'; obj.style.opacity=(cur+speed)/100; } else { obj.style[attr]=cur+speed+'px'; } } if(bStop) //停止条件 { clearInterval(obj.timer); fnEnd && fnEnd(); } }, 30);}
弹性运动
与缓冲运动的小区别:
1、公式 :
速度 += (目标点 - 当前值)/系数 (如6,7,8)
速度 *= 摩擦力 (如0.7,0.75)
2、停止条件 :
速度绝对小 && 距离绝对小
function startMove(obj,json,fn){ clearInterval(obj.timer); var iSpeed = {}; for(var attr in json){ iSpeed[attr] = 0; } obj.timer = setInterval(function(){ var bBtn = true; for(var attr in json){ var iCur = 0; if(attr == 'opacity'){ iCur = Math.round(getStyle(obj,attr)*100); } else{ iCur = parseInt(getStyle(obj,attr)); } iSpeed[attr] += (json[attr] - iCur)/6; iSpeed[attr] *= 0.75; if( Math.abs(iSpeed[attr])>1 || Math.abs(json[attr] - iCur)>1 ){ bBtn = false; } var value = iCur + iSpeed[attr]; if(value < 0 && (attr == 'width'||attr == 'height')){ value = 0; } if(attr == 'opacity'){ obj.style.filter = 'alpha(opacity='+ value +')'; obj.style.opacity = value/100; } else{ obj.style[attr] = value + 'px'; } } if(bBtn){ clearInterval(obj.timer); for(var attr in json){ iSpeed[attr] = 0; if(attr == 'opacity'){ obj.style.filter = 'alpha(opacity='+ json[attr] +')'; obj.style.opacity = json[attr]/100; } else{ obj.style[attr] = json[attr] + 'px'; } } if(fn){ fn.call(obj); } } },30);}
碰撞运动
function startMove(obj,iTarget) { clearInterval(obj.timer); var iSpeed = 0; obj.timer = setInterval(function() { iSpeed+=3; var iCur = parseInt(getStyle(obj,'height')); var val = iCur + iSpeed; if (val >= iTarget) { obj.style.height = iTarget + 'px'; iSpeed*=-1; iSpeed*=0.65; }else { obj.style.height = val + 'px'; } // console.log(val+'/'+iTarget) if (Math.floor(Math.abs(iSpeed)) <= 1 && Math.floor(Math.abs(val-iTarget)) <= 1 ) { clearInterval(obj.timer); }; console.log(1) },30) }
获取样式函数
function getStyle(obj,attr) { return obj.currentStyle ? obj.currentStyle(attr) : getComputedStyle(obj,false)[attr] }
0 1
- js缓冲运动、弹性运动、碰撞运动
- js动画之匀速运动、缓冲运动、弹性运动、碰撞运动、属性变化动画
- 原生Js完美运动框架(缓冲运动和弹性运动)
- js运动-弹性运动
- js运动-碰撞运动
- 缓冲运动与弹性运动
- js运动-运动缓冲
- js运动(2)----缓冲运动
- js运动技术--缓冲运动
- 碰撞运动js
- JS的缓冲运动
- JS缓冲运动
- js缓冲运动框架
- JS缓冲运动
- js缓冲运动
- js缓冲运动
- 关于js缓冲运动
- JS缓冲运动函数
- Git操作-分支管理(一)
- ZOJ题目分类
- 黑马程序员————面向对象之封装
- hiho一下 第四十四周 题目1 : 博弈游戏·Nim游戏
- HDU-2054-A == B?(Java大数BigDecimal)
- js缓冲运动、弹性运动、碰撞运动
- 5.19作业
- 基于用户的协同过滤推荐算法原理和实现
- 关于LTE系统中的逻辑信道、传输信道以及物理信道
- 剪裁图片【Ruby on Rails】
- web开发学习笔记(5):java.lang.OutOfMemoryError: Java heap space解决方法
- 类加载器及其委托机制的深入分析
- hadoop 多数据源连接之DataJoin
- hdu 5222