js运动框架tween

<!DOCTYPE html>
<html>
<head>
<title>myAnimate</title>
<style>
*{
    margin: 0;
    padding: 0;
    list-style: none;
}
#abc{
    position: absolute;
    top: 50px;
    left: 50px;
    width: 100px;
    height: 100px;
    background-color: #ff4500;
}
</style>
<script src="tween.js"></script>
</head>
<body>
<div id="abc" style="width:200px;"></div>
<script>
window.onload = function(){
    ele = document.getElementById("abc");
    function myAnimate(obj,attrObj,dur,fn){
        var fn = fn?fn:Tween.Quad.easeIn;
        var start = [];
        var changes = [];
        var times = 0;
        for(var i in attrObj){
            start[i]=getStyleNum(obj,i);
            changes[i]=attrObj[i]-start[i];
        }
        obj.time = setInterval(function(){
            var stops = true;
            if(times<dur){
                stops = false;
                for(var i in attrObj){
                    getStyleNum(obj,i,fn(times,start[i],changes[i],dur));
                }
            }
            times+=60;
            if(stops){
                for(var i in attrObj){
                    getStyleNum(obj,i,attrObj[i]);
                }
                clearInterval(obj.time);
                obj.time = null;
            }
        },60);
    }

    //getStyleNum
    function getStyleNum(obj,attr,val){
        if(obj.nodeType!=1){ //if it is not a element
            return;
        }
        var attr = attr.replace(/^\s+|\s+/,"");
        if(arguments.length==2){
            if(attr=="opacity"){
                return 100*parseInt(obj.currentStyle ? (obj.currentStyle[attr]||1) : (getComputedStyle(obj,null)[attr]||1)); //IE or FF
            }
            if(attr=="width"||attr=="height"||attr=="top"||attr=="left"){
                attr = "offset" + attr.replace(attr.charAt(0),attr.charAt(0).toUpperCase());
                return obj[attr];
            }
            return obj.currentStyle ? parseInt(obj.currentStyle[attr]) : (getComputedStyle(obj,null)[attr]);
        }
        if(arguments.length==3){
            switch(attr){
                case "width":
                case "height":
                case "top":
                case "left":
                obj.style[attr]=val+"px";
                break;
                case "opacity":
                obj.style.filter="alpha(opacity="+val+")";
                obj.style[attr]=val/100;
                break;
                default:
                obj.style[attr]=val;
            }
        }
    }

    myAnimate(ele,{width:400,height:300,opacity:20},600,Tween.Back.easeOut);
}
</script>
</body>
</html>

tween.js

  //animation caculate
            /*
            Linear:uniform
            Quadratic:2 square
            Cubic:3 square
            Quartic:4 square
            Quintic:5 square
            Sinusoidal:sine curve
            Exponential:index curve
            Circular:circular curve
            Elastic:index reduce sine curve
            Back:over range 3 square
            Bounce:index reduce rebounce

            each effect has three function:
            easeIn:from 0 accelerate add
            easeOut:reduce to 0
            easeInOut:accelerate add then reduce

            the function has four parameters
                t--- current time
                b--- beginning value
                c--- change in value
                d--- duration
          */

 Tween = {
    Linear: function(t,b,c,d){ return c*t/d + b; },
    Quad: {
        easeIn: function(t,b,c,d){
            return c*(t/=d)*t + b;
        },
        easeOut: function(t,b,c,d){
            return -c *(t/=d)*(t-2) + b;
        },
        easeInOut: function(t,b,c,d){
            if ((t/=d/2) < 1) return c/2*t*t + b;
            return -c/2 * ((--t)*(t-2) - 1) + b;
        }
    },
    Cubic: {
        easeIn: function(t,b,c,d){
            return c*(t/=d)*t*t + b;
        },
        easeOut: function(t,b,c,d){
            return c*((t=t/d-1)*t*t + 1) + b;
        },
        easeInOut: function(t,b,c,d){
            if ((t/=d/2) < 1) return c/2*t*t*t + b;
            return c/2*((t-=2)*t*t + 2) + b;
        }
    },
    Quart: {
        easeIn: function(t,b,c,d){
            return c*(t/=d)*t*t*t + b;
        },
        easeOut: function(t,b,c,d){
            return -c * ((t=t/d-1)*t*t*t - 1) + b;
        },
        easeInOut: function(t,b,c,d){
            if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
            return -c/2 * ((t-=2)*t*t*t - 2) + b;
        }
    },
    Quint: {
        easeIn: function(t,b,c,d){
            return c*(t/=d)*t*t*t*t + b;
        },
        easeOut: function(t,b,c,d){
            return c*((t=t/d-1)*t*t*t*t + 1) + b;
        },
        easeInOut: function(t,b,c,d){
            if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
            return c/2*((t-=2)*t*t*t*t + 2) + b;
        }
    },
    Sine: {
        easeIn: function(t,b,c,d){
            return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
        },
        easeOut: function(t,b,c,d){
            return c * Math.sin(t/d * (Math.PI/2)) + b;
        },
        easeInOut: function(t,b,c,d){
            return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
        }
    },
    Expo: {
        easeIn: function(t,b,c,d){
            return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
        },
        easeOut: function(t,b,c,d){
            return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
        },
        easeInOut: function(t,b,c,d){
            if (t==0) return b;
            if (t==d) return b+c;
            if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
            return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
        }
    },
    Circ: {
        easeIn: function(t,b,c,d){
            return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
        },
        easeOut: function(t,b,c,d){
            return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
        },
        easeInOut: function(t,b,c,d){
            if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
            return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
        }
    },
    Elastic: {
        easeIn: function(t,b,c,d,a,p){
            if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
            if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
            else var s = p/(2*Math.PI) * Math.asin (c/a);
            return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
        },
        easeOut: function(t,b,c,d,a,p){
            if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
            if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
            else var s = p/(2*Math.PI) * Math.asin (c/a);
            return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);
        },
        easeInOut: function(t,b,c,d,a,p){
            if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
            if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
            else var s = p/(2*Math.PI) * Math.asin (c/a);
            if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
            return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
        }
    },
    Back: {
        easeIn: function(t,b,c,d,s){
            if (s == undefined) s = 1.70158;
            return c*(t/=d)*t*((s+1)*t - s) + b;
        },
        easeOut: function(t,b,c,d,s){
            if (s == undefined) s = 1.70158;
            return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
        },
        easeInOut: function(t,b,c,d,s){
            if (s == undefined) s = 1.70158;
            if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
            return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
        }
    },
    Bounce: {
        easeIn: function(t,b,c,d){
            return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;
        },
        easeOut: function(t,b,c,d){
            if ((t/=d) < (1/2.75)) {
                return c*(7.5625*t*t) + b;
            } else if (t < (2/2.75)) {
                return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
            } else if (t < (2.5/2.75)) {
                return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
            } else {
                return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
            }
        },
        easeInOut: function(t,b,c,d){
            if (t < d/2) return Tween.Bounce.easeIn(t*2, 0, c, d) * .5 + b;
            else return Tween.Bounce.easeOut(t*2-d, 0, c, d) * .5 + c*.5 + b;
        }
    }
 }
时间: 2024-10-10 09:05:42

js运动框架tween的相关文章

带无缝滚动的轮播图(含JS运动框架)

今天学习了一下轮播图的写作,想到前一阵学过的无缝滚动得思想,所以就把轮播与滚动结合了一下.不过我的代码的神逻辑我自己都不敢恭维,在没网没参照的情况下,只能硬着头皮往下写,希望跟大家共勉吧. js运动框架的代码如下: 1 //获取样式 2 function getStyle(obj,attr){ 3 if(obj.currentStyle){ 4 return obj.currentStyle[attr]; 5 }else{ 6 return getComputedStyle(obj,false)

js运动框架逐渐递进版

运动,其实就是在一段时间内改变left.right.width.height.opactiy的值,到达目的地之后停止. 现在按照以下步骤来进行我们的运动框架的封装: 匀速运动. 缓冲运动. 多物体运动. 任意值变化. 链式运动. 同时运动 (第一部分):匀速运动 运动基础 思考:如何让div动起来?如下: 设置元素为绝对定位,只有绝对定位后,left,top等值才生效. 定时器的使用(动态改变值),这里使用setInterval()每隔指定的时间执行代码. 计时器setInterval(函数,交

js 运动框架及实例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>运动框架</title> &l

js运动框架之掉落的扑克牌(重心、弹起效果)

玩过电脑自带纸牌游戏的同志们应该都知道,游戏过关后扑克牌会依次从上空掉落,落下后又弹起,直至"滚出"屏幕. 效果如图:    这个案例的具体效果就是:点击开始运动,纸牌会从右上角掉落下来,之后弹起,运动的速度会逐渐减慢,直到越出屏幕后,全部纸牌消失,在右上角会重新出现一张纸牌,继续动作,一再往复. 具体代码如下: 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta ch

js运动框架完成块的宽高透明度及颜色的渐变

了解了运动框架完成块元素的宽高和透明度的变化的原理,我想着写一个颜色的变化来练习一下,不想写了很长时间才写出来,跟各位分享一下. 颜色的变化是通过三元素渐变的方式完成的,通过构造json,使当前的颜色与目标颜色进行对比,实现渐变的过程. 代码如下: 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>

JS运动框架

本人也是菜鸟一枚,从网上搜索了很久,封装备注了一套运动框架,加强大家对此的理解,希望多多提议. getId:function(elemId){ return document.getElementById(elemId); }, getStyle:function(obj,attr){ return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj, false)[attr]; }, startMove:function(o

带无缝滚动的轮播图(含JS运动框架)-简洁版

昨天写的神逻辑,今天终于解决,经过大家的商讨,终于研究出来一套简单的代码!!! js代码如下: 1 <script> 2 window.onload = function() { 3 var oWrap = document.getElementById("wrap"); 4 var oBox = document.getElementById("box"); 5 var aBox = oBox.getElementsByTagName("li

js 运动框架

/** * 使用offsetLeft,需注意margin的使用 * offsetLeft = margin-left + left; * offsetWidth= padding + border + width; * * @author Lonve */ function getStyle(elem,attr){ var result = null; result = elem.currentStyle?elem.currentStyle[attr]:getComputedStyle(elem

JS运动学习笔记 -- 任意值的运动框架(高/宽度,背景颜色,文本内容,透明度等)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>任意值的运动框架</title> <style> div { float: left; width: 200px; height: 200px; margin: 20px; background-color: yellow; border: 1p