插件前面的Y.是基于我自己的插件封装,可以换为var 执行。本插件用于公司网站。仅供大家参考。有更好的实现方式或者问题,请留言。

<div class="phone_av"></div>
Y.setHightlight.set({
    target:Y.$id("phone_av"),
    speed : 40,
    defaultColor : "#FFF4D9"    
});    

 

//高亮某区域
Y.setHightlight = (function(){
    var HeightLight = function(o){
        this.setting = typeof o === 'object' ? o : {};
        this.target = this.setting.target || 'hight';
        this.speed = this.setting.speed || 100;
        this.color = this.setting.defaultColor || "#ffffff";
        this.varColor = true;
        this.colorFix = 17;
        this.colorMaxCount = 4;
        this.colorCount = 0;
        this.colorG = 0;
        this.colorB = 0;
        this.toRGB();
        this.set();
    }
    HeightLight.prototype = {
        set:function(){
            var me = this;
            var colorG = me.colorG;
            var colorB = me.colorB;
            var setBgColor = setInterval(function(){
                if (me.varColor) {
                        colorG += me.colorFix;
                        colorB += me.colorFix;
                        if (colorG >= 255) {
                            me.varColor = false;
                            me.colorCount++;
                        }
                    } else {
                        colorG -= me.colorFix;
                        colorB -= me.colorFix;
                        if (colorG <= 170) {
                            me.varColor = true;
                            me.colorCount++;
                        }
                    }
                    if (me.colorCount >= me.colorMaxCount) {
                        clearInterval(setBgColor);
                        me.target.style.backgroundColor = "rgb(255,255,255)";
                        colorG = me.colorG;
                        colorB = me.colorB;
                    }    
                    me.target.style.backgroundColor = "rgb(255," + colorG + "," + colorB + ")";
            },me.speed);
        },
        toRGB:function(){
            var me = this;
            var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
            var sColor = me.color.toLowerCase();
            if(sColor && reg.test(sColor)){
               if(sColor.length === 4){
                     var sColorNew = "#";
                     for(var i=1; i<4; i+=1){
                          sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));   
                     }
                     sColor = sColorNew;
               }
               //处理六位的颜色值
               var sColorChange = [];
               for(var i=1; i<7; i+=2){
                     sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));     
               }
               me.colorG = sColorChange[1];
               me.colorB = sColorChange[2];
            }    
        }        
    }
    return {
        set : function(o){
            var SetBgColor = new HeightLight(o);
        }    
    }
})();
 posted on 2012-05-23 11:50  刘卿  阅读(409)  评论(0编辑  收藏  举报