zoukankan      html  css  js  c++  java
  • js-运动框架(时间版)

    周末的时候,在网上看了一篇教程,内容是说jquery的animate运动函数是如何实现的,看完之后自己就抽出了一些时间将之前的运动框架改了一下,改完之后发现效果非常好,而却运动形式也丰富了很多!

    代码如下:

    
    
      1 //obj:运动对象,json:运动属性json格式,times:运动时间(不能大于间隔时间),fx:运动形式,fn:回掉函数
      2 
      3 function  startMove(obj,json,times,fx,fn){
      4       var startTime=now();
      5       var iCur={};
      6       for(var attr in json){
      7           
      8            iCur[attr]=0;
      9            if(attr=="opacity"){
     10                
     11                 iCur[attr]=Math.round(getStyle(obj,attr)*100);   
     12            }else{
     13                
     14                  iCur[attr]=parseInt(getStyle(obj,attr));   
     15            }
     16   
     17       }
     18       clearInterval(obj.timer);
     19       obj.timer=setInterval(function(){
     20       var changeTime=now();
     21        
     22       var scale = 1 -  Math.max(0,startTime  -  changeTime + times)/times ; 
     23 
     24 
     25 
     26       for(var attr in json ){
     27 
     28             var cvalue=parseInt(json[attr])-iCur[attr];
     29             console.log(cvalue);
     30 
     31           var value=Tween[fx](scale*times,iCur[attr],cvalue,times);
     32 
     33               
     34           if(attr=="opacity"){
     35               
     36               obj.style.filter="alpha(opacity="+value+")";
     37               obj.style.opacity=value/100;
     38               
     39           }else{
     40               
     41               obj.style[attr]=value+"px";
     42   
     43          }
     44   
     45      }
     46      if(scale == 1){
     47         clearInterval(obj.timer);
     48         if(fn){
     49             fn.call(obj);
     50         }
     51     }
     52   
     53           
     54       },13);
     55       
     56 //取得当前时间      
     57      function now(){
     58          return (new Date()).getTime();     
     59      } 
     60      
     61 //获得样式值     
     62      function getStyle(obj,sty){
     63           if(obj.currentStyle){
     64              
     65              return obj.currentStyle[sty];  
     66               
     67           }else{
     68               
     69              return window.getComputedStyle(obj,false)[sty];  
     70               
     71           }
     72 
     73     }
     74     
     75 //运动公式     
     76 var Tween = {
     77     
     78     //t : 当前时间   b : 初始值  c : 变化值   d : 总时间
     79     //return : 当前的位置 
     80     
     81     linear: function (t, b, c, d){  //匀速
     82         return c*t/d + b;
     83     },
     84     easeIn: function(t, b, c, d){  //加速曲线
     85         return c*(t/=d)*t + b;
     86     },
     87     easeOut: function(t, b, c, d){  //减速曲线
     88         return -c *(t/=d)*(t-2) + b;
     89     },
     90     easeBoth: function(t, b, c, d){  //加速减速曲线
     91         if ((t/=d/2) < 1) {
     92             return c/2*t*t + b;
     93         }
     94         return -c/2 * ((--t)*(t-2) - 1) + b;
     95     },
     96     easeInStrong: function(t, b, c, d){  //加加速曲线
     97         return c*(t/=d)*t*t*t + b;
     98     },
     99     easeOutStrong: function(t, b, c, d){  //减减速曲线
    100         return -c * ((t=t/d-1)*t*t*t - 1) + b;
    101     },
    102     easeBothStrong: function(t, b, c, d){  //加加速减减速曲线
    103         if ((t/=d/2) < 1) {
    104             return c/2*t*t*t*t + b;
    105         }
    106         return -c/2 * ((t-=2)*t*t*t - 2) + b;
    107     },
    108     elasticIn: function(t, b, c, d, a, p){  //正弦衰减曲线(弹动渐入)
    109         if (t === 0) { 
    110             return b; 
    111         }
    112         if ( (t /= d) == 1 ) {
    113             return b+c; 
    114         }
    115         if (!p) {
    116             p=d*0.3; 
    117         }
    118         if (!a || a < Math.abs(c)) {
    119             a = c; 
    120             var s = p/4;
    121         } else {
    122             var s = p/(2*Math.PI) * Math.asin (c/a);
    123         }
    124         return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    125     },
    126     elasticOut: function(t, b, c, d, a, p){    //正弦增强曲线(弹动渐出)
    127         if (t === 0) {
    128             return b;
    129         }
    130         if ( (t /= d) == 1 ) {
    131             return b+c;
    132         }
    133         if (!p) {
    134             p=d*0.3;
    135         }
    136         if (!a || a < Math.abs(c)) {
    137             a = c;
    138             var s = p / 4;
    139         } else {
    140             var s = p/(2*Math.PI) * Math.asin (c/a);
    141         }
    142         return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
    143     },    
    144     elasticBoth: function(t, b, c, d, a, p){
    145         if (t === 0) {
    146             return b;
    147         }
    148         if ( (t /= d/2) == 2 ) {
    149             return b+c;
    150         }
    151         if (!p) {
    152             p = d*(0.3*1.5);
    153         }
    154         if ( !a || a < Math.abs(c) ) {
    155             a = c; 
    156             var s = p/4;
    157         }
    158         else {
    159             var s = p/(2*Math.PI) * Math.asin (c/a);
    160         }
    161         if (t < 1) {
    162             return - 0.5*(a*Math.pow(2,10*(t-=1)) * 
    163                     Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    164         }
    165         return a*Math.pow(2,-10*(t-=1)) * 
    166                 Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
    167     },
    168     backIn: function(t, b, c, d, s){     //回退加速(回退渐入)
    169         if (typeof s == 'undefined') {
    170            s = 1.70158;
    171         }
    172         return c*(t/=d)*t*((s+1)*t - s) + b;
    173     },
    174     backOut: function(t, b, c, d, s){
    175         if (typeof s == 'undefined') {
    176             s = 3.70158;  //回缩的距离
    177         }
    178         return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
    179     }, 
    180     backBoth: function(t, b, c, d, s){
    181         if (typeof s == 'undefined') {
    182             s = 1.70158; 
    183         }
    184         if ((t /= d/2 ) < 1) {
    185             return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    186         }
    187         return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
    188     },
    189     bounceIn: function(t, b, c, d){    //弹球减振(弹球渐出)
    190         return c - Tween['bounceOut'](d-t, 0, c, d) + b;
    191     },       
    192     bounceOut: function(t, b, c, d){
    193         if ((t/=d) < (1/2.75)) {
    194             return c*(7.5625*t*t) + b;
    195         } else if (t < (2/2.75)) {
    196             return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
    197         } else if (t < (2.5/2.75)) {
    198             return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
    199         }
    200         return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
    201     },      
    202     bounceBoth: function(t, b, c, d){
    203         if (t < d/2) {
    204             return Tween['bounceIn'](t*2, 0, c, d) * 0.5 + b;
    205         }
    206         return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
    207     }
    208 } 
    209 
    210 }
    
    
    
    
    
    一个不敬业的前端攻城狮
  • 相关阅读:
    debounce防抖和throttle节流
    vue 全局路由守卫,系统未登录时自动跳转到登录页面
    vue中使用animate动画库
    nodejs搭建本地静态服务器
    echart4.9 实现map地图
    vue中如何使用echarts
    http状态码返回415问题
    lodash 常用方法整理
    氦图面试题目Boolean search
    Mac 去掉文件属性@
  • 原文地址:https://www.cnblogs.com/chaoming/p/3429134.html
Copyright © 2011-2022 走看看