2Guy's Blog

闪光字效果

Author:
by: sliuqin
Date:
6. October 2009 13:10
Tagged in:
Comments:
0

十一长假都没有写JavaScript,正好看到愚人码头的闪光字效果示例,我也做一个,以免工作时手生。

DEMO

关键代码:

JS
start: function(){
    var self = this;
	//遍历所有el
    for (var i = 0, item; item = this.els[i++];) {
        var j = 0;
		//将item放在闭包中。
        this.times.push(setInterval((function(item){
            return function(){
                item.style.color = self.color[j++];
                if (j == self.color.length) {
                    j = 0;
                }
            }
        })(item), self.offset))
    }
}

有一个点比较有意思的是,将color定义在prototype中,如果有必要在构造函数中覆盖,这样不仅在实例化时性能会比较好,同样也达到设置默认color的效果。

Add comment