zoukankan      html  css  js  c++  java
  • 简单动画实现代码

    动画的实现原型

    <div id="div1" style="100px;height:50px;background:red;cursor:pointer;color:#fff;text-align:center;line-height:50px;">点击</div>
    
    <script type="text/javascript">
    //dom
    var oDiv = document.getElementById('div1');
    //调用
    oDiv.onclick = function() {
        $('#div1').run({
            'width': '500'
        }).run({
            'width': '300'
        }).run({
            'width': '1000'
        });
    };
    </script>

    JS

    (function($) {
    
        window.$ = $;
    
    })(function() {
    
        var rquickExpr = /^(?:#([w-]*))$/;
    
        function aQuery(selector) {
            return new aQuery.fn.init(selector);
        }
    
        /**
         * 动画
         * @return {[type]} [description]
         */
        var animation = function(){
            var self    = {};
            var Queue   = []; //动画队列
            var fireing = false //动画锁
            var first   = true;//通过add接口触发
    
            var getStyle = function(obj, attr) {
                return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, false)[attr];
            }
    
            var makeAnim = function(element, options, func) {
                var width = options.width
                    //包装了具体的执行算法
                    //css3
                    //setTimeout
                element.style.webkitTransitionDuration = '2000ms';
                element.style.webkitTransform = 'translate3d(' + width + 'px,0,0)';
    
                //监听动画完结
                element.addEventListener('webkitTransitionEnd', function() {
                    func()
                });
            }
    
            var _fire = function() {
                //加入动画正在触发
                if (!fireing) {
                    var onceRun = Queue.shift();
                    if (onceRun) {
                        fireing = true;
                        //next
                        onceRun(function() {
                            fireing = false;
                            _fire();
                        });
                    } else {
                        fireing = true;
                    }
                }
            }
    
            return self = {
                //增加队列
                add: function(element,options) {
                    Queue.push(function(func) {
                        makeAnim(element, options, func);
                    });
    
                    //如果有一个队列立刻触发动画
                    if(first && Queue.length ){
                        first = false;
                        self.fire();
                    }
                },
                //触发
                fire: function() {
                    _fire();
                }
            }
        }();
    
    
        aQuery.fn = aQuery.prototype = {
            run:function(options){
                animation.add(this.element,options);
                return this;
            }
        }
    
        var init = aQuery.fn.init = function(selector) {
            var match    = rquickExpr.exec(selector);
            var element  = document.getElementById(match[1])
            this.element = element;
            return this;
        }
    
        init.prototype = aQuery.fn;
    
        return aQuery;
    }());
  • 相关阅读:
    在springmvc中,什么是逻辑视图?什么是物理视图?
    docker2
    docker1
    HTTP Service 中篇
    HTTP Service 上篇
    Centos7 fstab盘符挂载硬盘导致重启系统失败解决办法
    vsphere网络
    LVS的工作原理认识
    数字转换
    对def函数的参数认识
  • 原文地址:https://www.cnblogs.com/aaronjs/p/3858672.html
Copyright © 2011-2022 走看看