codeing or artist ?
记得大学第一节编程课,教授说,"如果一件事儿有对错,那么是科学。如果有美丑好坏,那么是艺术。" 一个能顺利运行还能让人阅读时体验思维美妙的代码,就是艺术和科学的结合。能运行的程序并不是好程序,能当作文章来读的才是。在我看来代码是一种特殊的文体,程序猿其实会写诗。

不啰嗦上代码:

WN:(function(){
            var el = $('<fakeelement>'),
                transition="transition",
                transitionEnd,
                animEvent={'start':null,'iteration':null,'end':null},
                vendorPrefix;
                
            transition = transition.charAt(0).toUpperCase() + transition.substr(1);
            
            vendorPrefix=(function(){//现在的opera也是webkit
                var  i=0, vendor=["Moz", "Webkit", "Khtml", "O", "ms"];
                while (i < vendor.length) {
                    if (typeof el.css(vendor[i] + transition) === "string"){
                        return vendor[i];
                    }
                    i++;
                }
                return false;
            })();
            
            transitionEnd=(function(){
                var transEndEventNames = {
                    WebkitTransition : 'webkitTransitionEnd',
                    MozTransition    : 'transitionend',
                    OTransition      : 'oTransitionEnd otransitionend',
                    transition       : 'transitionend'
                }
                for(var name in transEndEventNames){
                    if(typeof el.css(name) === "string"){
                        return transEndEventNames[name];
                    }
                }
            })();
            
            animEvent.end=(function(){
                var animEndEventNames = {
                    WebkitAnimation : 'webkitAnimationEnd',
                    animation      : 'animationend'
                }
                for(var name in animEndEventNames){
                    if(typeof el.css(name) === "string"){
                        return animEndEventNames[name];
                    }
                }
            })();
            
            animEvent.iteration=(function(){
                var animIterationEventNames = {
                    WebkitAnimation : 'webkitAnimationIteration',
                    animation      : 'animationiteration'
                }
                for(var name in animIterationEventNames){
                    if(typeof el.css(name) === "string"){
                        return animIterationEventNames[name];
                    }
                }
            })();
            
            animEvent.start=(function(){
                var animStartEventNames = {
                    WebkitAnimation : 'webkitAnimationStart',
                    animation      : 'animationstart'
                }
                for(var name in animStartEventNames){
                    if(typeof el.css(name) === "string"){
                        return animStartEventNames[name];
                    }
                }
            })();
            
            return {
                called:false,
                addTranEvent:function(elem,fn,duration){
                    var self = this;
                    var fncallback = function(){
                        if(!self.called){
                            fn();
                            self.called = true;
                        }
                    };
                    function hand(){    
                        elem.on(transitionEnd,function(){
                            //elem.unbind(transitionEnd,arguments.callee);
                            fncallback();
                        });
                    }
                    setTimeout(hand,duration);
                },
                addAnimEvent:function(elem,name,fn){
                    elem.on(animEvent[name],fn);
                },            
                removeAnimEvent:function(elem,name,fn){
                    elem.unbind(animEvent[name],fn);
                },
                setStyleAttribute:function(elem,val){
                    if(Object.prototype.toString.call(val) === "[object Object]"){
                        for(var name in val){
                            if(/^transition|animation|transform/.test(name)){
                                var styleName=name.charAt(0).toUpperCase() + name.substr(1);
                                elem.css(vendorPrefix+styleName,val[name]);
                            }else{
                                elem.css(name,val[name]);
                            }
                        }
                    }
                }
            };
        })(),

 

怎么调用不用我说了吧,看返回的4个方法:

addTranEvent,addAnimEvent,removeAnimEvent,setStyleAttribute

posted on 2016-08-29 16:32  codeing-or-artist-??  阅读(365)  评论(0编辑  收藏  举报