Linear (a formula for uniform motion)

function linear(t,b,c,d){
         returnt/d*c+b; // T is the elapsed time; d is the total time; c is the total distance; b is the initial position of the element.Copy the code
<! DOCTYPE html> <html lang="en">
<head>
    <meta charset="UTF-8"</title> <style> HTML,body,div{margin: 0; padding: 0; }#box{
            width: 100px;
            height: 100px;
            background: brown;
            border-radius: 50%;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0.4;
        }
    </style>
</head>
<body>
<div id="box"></div>
<script src="utils.js"></script>
<script>
    var oBox=document.getElementById('box'); ; (function() {function linear(t,b,c,d){
            return t/d*c+b;
        }

        function animate(curEle,target,duration,callback){
            var begin={};
            var distance={};
            for(var key inBegin [key]=utils.css(curEle,key); distance[key]=target[key]-begin[key]; console.log(target[key]); console.log(distance[key]) } var time=0; curEle.timer=setInterval(()=>{ time+=17; // The elapsed timeif(time>=duration){utils. CSS (curEle,target); clearInterval(curEle.timer); typeof callback==='function'? callback():null;return;
                }

                for(var key intarget){ var val=linear(time,begin[key],distance[key],duration ); //time is the elapsed time, utils.css(curEle,key,val); 17)}}},function change() {
            oBox.style.width=500;
            oBox.style.height=700;
            oBox.style.background='yellowGreen';
        }
        animate(oBox,{width:1000,height:1000,opacity:1,left:2500,top:2500},3000,change)
    })();
</script>
</body>
</html>
Copy the code

utils.js

var utils = (function () {
    function offset(curEle) {
        var  l = curEle.offsetLeft;
        var  t = curEle.offsetTop;
        var p = curEle.offsetParent;
        while(p.nodeName ! = ="BODY"){
            l+=p.offsetLeft +p.clientLeft;
            t+=p.offsetTop+p.clientTop;
            p = p.offsetParent;
        }
        return {
            left:l,top:t
        }
    };
    function getCss(curEle,attr) {
        var  val;
        if("getComputedStyle" inWindow){// Check whether getComputedStyle is supported; val = getComputedStyle(curEle)[attr]; }else{ val = curEle.currentStyle[attr]; } / / to unit var reg = / ^ (width | height | margin | padding | left | top | | | right bottom fontZise | opacity) $/; // Checks whether the current attribute has a unitif(reg.test(attr)){// Check whether it is null;if(!isNaN(parseFloat(val))){
                val = parseFloat(val);
            }
        }
        returnval; } / /setCss: Each execution sets an attribute style for the element;function setCss(curEle,attr,val) {
        var  reg = /^(width|height|top|left|right|bottom|padding|margin)$/;
        if(reg.test(attr)){
            if(typeof val==="number"){
                val = val + "px"; } } curEle.style[attr]=val; // Set inline styles; }function setGroupCss(curEle,obj) {// iterate obj; Call-wrappedsetCss, which sets a single style for an element;for(var key in obj){
            setCss(curEle,key,obj[key])
        }
    }
    functioncss(... Arg) {// In the parentheses of the function definition... Is the residual operator; Put all the arguments into an array; //if(arg.length===3){
            // [oBox,"height", 300]setCss(... arg); }else if(arg.length===2){
            if(toString.call(arg[1])==="[object Object]") {setGroupCss(... arg) }else{
                returngetCss(... arg) } } }functionWin (attr,val) {// If it is two arguments, then it must be set; If it is an argument, get;if(val===undefined){
            return document.documentElement[attr] || document.body[attr];
        }
        document.documentElement[attr] = val;
        document.body[attr] = val;


    }
    return {
        offset:offset,
        getCss:getCss,
        setCss:setCss,
        setGroupCss:setGroupCss, css:css, win:win } })(); /* utils= {offset:function offset(curEle) {
        var  l = curEle.offsetLeft;
        var  t = curEle.offsetTop;
        var p = curEle.offsetParent;
        while(p.nodeName ! = ="BODY"){
            l+=p.offsetLeft +p.clientLeft;
            t+=p.offsetTop+p.clientTop;
            p = p.offsetParent;
        }
        return {
            left:l,top:t
        }
    }
}*/

Copy the code

animate.js

(functionVar zhufengEffect = {var zhufengEffect = {var zhufengEffect = {function (t, b, c, d) {
              returnt/d * c + b; }, // index decay Bounce: {easeIn:function(t, b, c, d) {
                return c - zhufengEffect.Bounce.easeOut(d - t, 0, c, d) + b;
            },
            easeOut: function(t, b, c, d) {
                if((t /= d) < (1/2.75)) {returnC * (7.5625 * t * t) + b; }else if(t < (2/2.75)) {returnC * (7.5625 * (t - = (1.5/2.75)) * t + 75) + b; }else if(t < (2.5/2.75)) {returnC * (7.5625 * (t - = (2.25/2.75)) * t + 9375) + b; }else {
                    returnC * (7.5625 * (t - = (2.625/2.75)) * t + 984375) + b; } }, easeInOut:function(t, b, c, d) {
                if (t < d / 2) {
                    return zhufengEffect.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
                }
                returnzhufengEffect.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + 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;
                }
                returnc / 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;
                }
                returnc / 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;
                returnc / 2 * (-Math.pow(2, -10 * --t) + 2) + b; }}, // easeIn: {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;
                }
                returnc / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + 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) {
                    returnC / 2 * (t * t * (((s * = (1.525)) + 1) * t -s)) + b; }returnC / 2 * (t (t = 2) * * (((s * = (1.525)) + 1) * t + s) + 2) + b; }}, // exponential decay sine 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; var s; ! a || a < Math.abs(c) ? (a = c, s = p / 4) : 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; var s; ! a || a < Math.abs(c) ? (a = c, s = p / 4) : 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); var s; ! a || a < Math.abs(c) ? (a = c, s = p / 4) : 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;
                returna * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b; }}}; /** ** @param curEle Currently executing animation element * @param target target value object {left:500,top:300} * @param duration Total transition time 2000ms */ / effect Pass a number // pass an array ['Quad'.'easeInOut']
    functionMove (curEle, target, duration,effect, callBack) {var tempEffect = zhufengeffect.linear; // determine effect if it is a numberif(typeof effect === "number") {
            switch (effect) {
                case 0:
                    tempEffect = zhufengEffect.Linear;
                    break;
                case 1:
                    tempEffect = zhufengEffect.Quad.easeInOut;
                    break;
                case 2:
                    tempEffect = zhufengEffect.Bounce.easeIn;
                    break;
                case 3:
                    tempEffect = zhufengEffect.Cubic.easeInOut;
                    break; }}else if(effect instanceof Array) {// If an Array is specified ['Quad'.'easeInOut']
            tempEffect = effect.length === 2? zhufengEffect[effect[0]][effect[1]]:zhufengEffect[effect[0]];
        } else if(typeof effect === "function") { callBack = effect; } // Clear the last animation curele.zftimer before executing this animation? clearInterval(curEle.zfTimer) : null; var begin = {}; Var change = {}; // Store the state change value of the property corresponding to the element executing the animationfor(var key inTarget) {// target Target position of the objectif(target.hasOwnProperty(key)) {
                // begin {'left': 'left initial value '} begin[key] =Number(utils.css(curEle, key)); Obj = obj; obj = obj; obj = obj;'left': 'left change value '} console.log(begin[key]); // Change [key] =target[key] -begin [key]// The value of the key attribute to change, and put it in the change object console.log(change[key]); } } // console.log(begin); // console.log(change); var time = null; // Record the current time curele.zftimer =setInterval(function () {
            time += 10;
            if(time >= duration) {// Target: {left: 500, top: 300} utils. CSS (curEle,target); // Make sure it is the target state clearInterval(curele.zftimer); // End animation typeof callBack ==="function"? callBack.call(curEle) : null;
                return; Zfeffect. Linear how many times zfeffect. Linear executes and passes in the parameters of the corresponding attributes to calculate the current state // The first key is left and the second key is left topfor(var key in target) {
               ifVar curPos = tempEffect(time,begin[key],change[key], duration); CurPos utils.css(curEle, key, curPos); }}}, 10); } // Put the function into the global zfAnimate window. ZfAnimate = move; }) ();Copy the code