找回密码
 立即注册
首页 业界区 业界 Js动画(一)基础

Js动画(一)基础

第璋胁 2025-5-29 19:45:57
  在再谈js拖拽(二)仿iGoogle自定义首页模块拖拽的最后,我说了接下来要写Js动画,转瞬拖到了今天,呵呵。这篇主要讲动画的基础,就是几个最基本的特效,即:移动,渐变和尺寸变化。接下来写个梦幻西游版逍遥生角色行走的动画,然后再适时的写些动画有关的例子,争取把这个系列写好。
  我们玩魔兽世界的时候可以通过ctrl+r来查看当前的帧数,当帧数很小时,会觉得很卡,帧数很高则很流畅。所谓帧数就是1秒内显示图片的数量。当这么多帧图片连起来显示,就形成了动画。
  Js中实现动画都是靠setInterval或者setTimeout来实现。setInterval自身就能不断循环来执行计算从而显示新的帧,setTimeout是间隔一段时间后仅执行一次,他需要配合函数循环实现,很多人偏爱setTimeout来实现。本文采用setInterval实现。
  据说,普通人眼能看到1/24秒,就是说1秒至少24帧,每次移位间隔需要小于1000/24=41.7毫秒,也就说setInterval要每隔至少40毫秒执行一次,一般地,我们采用10毫秒,当然间隔时间越短,客户端执行计算次数就越多,如果你code计算量大则可以适当调长些。
                                                              ~_~ ---->        
        
        
1.gif
   

  首先贴上封装好的动画类Animation。  
2.gif
3.gif
Animation
  1. Animation = {<br>            timer: 10,<br>            SetOpacity: function(obj, n) {<br>                if (document.all) {<br>                    obj.filters.alpha.opacity = n;<br>                }<br>                else {<br>                    obj.style.opacity = n / 100;<br>                }<br>            },<br>            fade: function(obj, target, count, Func) {<br>                obj = this.getItself(obj);<br>                var currentCount = 0;<br>                count = Math.abs(count) || 1;<br>                target = target < 0 ? 0 : (target > 100) ? 100 : target;<br>                var init = document.all ? obj.filters.alpha.opacity : window.getComputedStyle(obj, null).opacity * 100;<br>                Func = Func || Tween.Linear;<br>                var opr = this;<br>                var flag = setInterval(function() {<br>                    if (currentCount > count) {<br>                        clearInterval(flag);<br>                    }<br>                    else {<br>                        currentCount++;<br>                        var tmp = Func(init, target, currentCount, count);<br>                        opr.SetOpacity(obj, tmp);<br>                        //清除小数点的误差<br>                        if (Math.abs(tmp - target) < 1) {<br>                            opr.SetOpacity(obj, target);<br>                        }<br>                    }<br>                }<br>                , this.timer);<br>            },<br>            resize: function(obj, targetPos, count, Func) {<br>                obj = this.getItself(obj);<br>                var currentCount = 0;<br>                count = Math.abs(count) || 1;<br>                var initPos = { x: obj.offsetWidth, y: obj.offsetHeight }<br>                Func = Func || Tween.Linear;<br>                targetPos = { x: targetPos.x < 0 ? 0 : targetPos.x, y: targetPos.y < 0 ? 0 : targetPos.y }<br>                var flag = setInterval(function() {<br>                    if (currentCount > count) {<br>                        clearInterval(flag);<br>                    }<br>                    else {<br>                        currentCount++;<br>                        var tmpX = Func(initPos.x, targetPos.x, currentCount, count);<br>                        var tmpY = Func(initPos.y, targetPos.y, currentCount, count);<br>                        //width值不能小于0,但是算法返回值有可能出现负值<br>                        try {<br>                            obj.style.width = tmpX + "px";<br>                            obj.style.height = tmpY + "px";<br>                        }<br>                        catch (e) {<br>                        }<br>                        //清除小数点的误差<br>                        if (Math.abs(tmpX - targetPos.x) < 1) {<br>                            obj.style.width = targetPos.x + "px";<br>                        }<br>                        if (Math.abs(tmpY - targetPos.y) < 1) {<br>                            obj.style.height = targetPos.y + "px";<br>                        }<br>                    }<br>                }<br>                , this.timer);<br>            },<br>            move: function(obj, targetPos, count, Func) {<br>                obj = this.getItself(obj);<br>                var currentCount = 0;<br>                count = Math.abs(count) || 1;<br>                var elPos = this.getElementPos(obj);<br>                var initPos = { x: elPos.x, y: elPos.y }<br>                Func = Func || Tween.Linear;<br>                var flag = setInterval(function() {<br>                    if (currentCount > count) {<br>                        clearInterval(flag);<br>                    }<br>                    else {<br>                        currentCount++;<br>                        var tmpX = Func(initPos.x, targetPos.x, currentCount, count);<br>                        var tmpY = Func(initPos.y, targetPos.y, currentCount, count);<br>                        obj.style.left = tmpX + "px";<br>                        obj.style.top = tmpY + "px";<br>                        //清除小数点的误差<br>                        if (Math.abs(tmpX - targetPos.x) < 1) {<br>                            obj.style.left = targetPos.x + "px";<br>                        }<br>                        if (Math.abs(tmpY - targetPos.y) < 1) {<br>                            obj.style.top = targetPos.y + "px";<br>                        }<br>                    }<br>                }<br>                , this.timer);<br>            },<br>            getElementPos: function(el) {<br>                el = this.getItself(el);<br>                var _x = 0, _y = 0;<br>                do {<br>                    _x += el.offsetLeft;<br>                    _y += el.offsetTop;<br>                } while (el = el.offsetParent);<br>                return { x: _x, y: _y };<br>            },<br>            getItself: function(id) {<br>                return "string" == typeof id ? document.getElementById(id) : id;<br>            }<br>        }
复制代码
 
  其中fade方法是实现渐变效果的,resize方法是改变对象尺寸大小的,move方法是实现移动效果的。首先看下move方法,其他2个方法实现其实是类似的。
  move方法的调用:Animation.move('divObj',{x:500,y:500},100,Tween.Quart.easeInOut); 第一个参数是移动的对象,第二个参数是移动的目标坐标,第三个参数是一共执行多少次移位,即移动时间内一共的帧数,即执行多少次,第4个参数是可选参数,指定Tween具体算法,若不加,则默认采用Tween.Linear。
  移动问题的关键就是每次移位后,应该将移动对象定位到什么位置,即其left和top是多少。来看一个简单的数学题,当我们知道了一个点的起始位置initPos和终点位置targetPos,要求在count次移动后达到终点,该点为匀速运动,求第currentCount次移动后该点位置在哪里?显然的位置在:(targetPos - initPos)*(currentCount/count)+initPos。这个就是Tween.Linear算法。当这个点不是匀速运动时,就是Tween的其他算法。Tween来自Flash的AS,你可以参考http://www.robertpenner.com/easing/easing_demo.html去查看Tween各种算法的运动效果,也可以参考cloudgamer的一篇文章JavaScript Tween算法及缓动效果。相比下,我将Tween各种算法的传入参数稍改了下,将移动的总距离的运算放到了Tween算法内部,然后最后个参数是作为总共计算次数理解的,而不是持续时间。我本来是想用持续时间运算的,但是发现还是要将持续时间除以10毫秒得到总次数,然后参与运算,总次数还可能非整数,有误差,所以我干脆直接传总次数过来,当然你也可以改成持续时间。那在应用时,究竟移动次数应该写多少,在每次间隔10毫秒进行移动下,次数越多,耗时越长。譬如采用Tween.Linear计算移动位置时,大概是耗时count*10ms的1.5倍,譬如写100,就是100*10*1.5=1.5s左右,这个多出来的0.5倍是Tween.Linear计算花费的时间。贴上改写后的Tween。   
4.gif
5.gif
Tween
  1. /*<br>        t:currentCount 当前执行第t次<br>        b:initPos 初始值<br>        c:targetPos - initPos 发生偏移的距离值<br>        d:count 一共执行d次<br>        效果:http://www.robertpenner.com/easing/easing_demo.html <br>        JavaScript Tween算法及缓动效果 http://www.cnblogs.com/cloudgamer/archive/2009/01/06/tween.html<br>        */<br>        var Tween = {<br>            Linear: function(initPos, targetPos, currentCount, count) {<br>                var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                return c * t / d + b;<br>            },<br>            Quad: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * (t /= d) * t + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return -c * (t /= d) * (t - 2) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if ((t /= d / 2) < 1) return c / 2 * t * t + b;<br>                    return -c / 2 * ((--t) * (t - 2) - 1) + b;<br>                }<br>            },<br>            Cubic: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * (t /= d) * t * t + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * ((t = t / d - 1) * t * t + 1) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;<br>                    return c / 2 * ((t -= 2) * t * t + 2) + b;<br>                }<br>            },<br>            Quart: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * (t /= d) * t * t * t + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return -c * ((t = t / d - 1) * t * t * t - 1) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;<br>                    return -c / 2 * ((t -= 2) * t * t * t - 2) + b;<br>                }<br>            },<br>            Quint: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * (t /= d) * t * t * t * t + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * ((t = t / d - 1) * t * t * t * t + 1) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;<br>                    return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;<br>                }<br>            },<br>            Sine: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * Math.sin(t / d * (Math.PI / 2)) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;<br>                }<br>            },<br>            Expo: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (t == 0) return b;<br>                    if (t == d) return b + c;<br>                    if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;<br>                    return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;<br>                }<br>            },<br>            Circ: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;<br>                    return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;<br>                }<br>            },<br>            Elastic: {<br>                easeIn: function(initPos, targetPos, currentCount, count, a, p) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;<br>                    if (!a || a < Math.abs(c)) { a = c; var s = p / 4; }<br>                    else var s = p / (2 * Math.PI) * Math.asin(c / a);<br>                    return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count, a, p) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;<br>                    if (!a || a < Math.abs(c)) { a = c; var s = p / 4; }<br>                    else var s = p / (2 * Math.PI) * Math.asin(c / a);<br>                    return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count, a, p) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (t == 0) return b; if ((t /= d / 2) == 2) return b + c; if (!p) p = d * (.3 * 1.5);<br>                    if (!a || a < Math.abs(c)) { a = c; var s = p / 4; }<br>                    else var s = p / (2 * Math.PI) * Math.asin(c / a);<br>                    if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;<br>                    return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;<br>                }<br>            },<br>            Back: {<br>                easeIn: function(initPos, targetPos, currentCount, count, s) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (s == undefined) s = 1.70158;<br>                    return c * (t /= d) * t * ((s + 1) * t - s) + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count, s) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (s == undefined) s = 1.70158;<br>                    return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count, s) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (s == undefined) s = 1.70158;<br>                    if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;<br>                    return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;<br>                }<br>            },<br>            Bounce: {<br>                easeIn: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;<br>                },<br>                easeOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if ((t /= d) < (1 / 2.75)) {<br>                        return c * (7.5625 * t * t) + b;<br>                    } else if (t < (2 / 2.75)) {<br>                        return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;<br>                    } else if (t < (2.5 / 2.75)) {<br>                        return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;<br>                    } else {<br>                        return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;<br>                    }<br>                },<br>                easeInOut: function(initPos, targetPos, currentCount, count) {<br>                    var b = initPos, c = targetPos - initPos, t = currentCount, d = count;<br>                    if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;<br>                    else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;<br>                }<br>            }<br>        }
复制代码
 
  相比move方法,fade方法是用Tween算法去计算透明度,而resize方法是用Tween算法去计算width和height,没有大的区别。利用这3个特效,一些简单的动画都能实现了。如果某对象要在移动过程中同时改变大小和设置透明度,只要三个方法连续写下来即可。如:
[code][/code] 
(ps:若无特别声明,此博客所有例子均可在IE/FF/Chrome中运行)
点击下载
----------------华丽丽分隔线下是懒人的快速回复----------------

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册