闭包
多方向动画
- [x] 多方向动画基础
指定运动路线
123456var totalLeft = targetLeft - beginLeft;var totalTop = targetTop - beginTop;//time,begin,change,durationfunction linear(t, b, c, d) {return b + t / d * c;}每个方向的值实时改变
1234var curLeft = linear(time, beginLeft, totalLeft, duration);var curTop = linear(time, beginTop, totalTop, duration);utils.setCss(box, 'top',curTop);utils.setCss(box, 'left',curLeft);
源码
- [x] 自制简单多方向动画库animation.js
http://tool.css-js.com/ (压缩Uglify)
123456789101112//方法的调用//animate("谁动","往哪里动","多久动完");// curEle:操作元素// targetObj:动画目标位置--->存储每个方向的目标位置{left:xxx,top:xxx,……}// duration:运行时间animate(box, {width: 100,height: 150,left: 100,top: 200,opacity: 0.2},1000);[ ] 根据传递目标对象,获取对应
123456789//根据targetObj获取起始值beginObj和totalObj(taget里的方向和begin、total的方向对应)var beginObj = {},changeObj = {};for (var key in targetObj) { //key就是对象里的方向if (targetObj.hasOwnProperty(key)) {beginObj[key] = utils.css(curEle, key);changeObj[key] = targetObj[key] - beginObj[key];}}[ ] 实现多方向的运动动画
12345678910111213141516curEle.animateTimer = window.setInterval(function() {time += 5;// 到达目标结束动画,(批量)当前元素的样式=目标样式if (time >= duration) {utils.css(curEle, targetObj);window.clearInterval(curEle.animateTimer);}// 没到达目标,每个位置设置for (var key in targetObj) { //key就是对象里的方向if (targetObj.hasOwnProperty(key)) {var curPosObj = Linears.Linear(time, beginObj[key], changeObj[key], duration);utils.css(curEle, key, curPosObj);}}}, 10);}