http://www.css88.com/archives/1910
这是一题知名公司的前端开发面试试题,要求写出闪光字的效果。基本思路是:
1.页面中可能多出使用;
2.选出几种颜色;
3.选择合理的跳动时间。
例如html代码
<div id="comment1">愚人码头</div> <div id="comment2">前端开发</div> <div id="comment3">http://www.css88.com</div>
javascript代码:
function MagicColor(elem,colors,interTime){
this.elements=elem||[];
this.colors=colors||["#CC0000","#CC6D00","#CCCC00","#00CC00","#0000CC","#00CCCC","#CC00CC"];//可选的颜色值
this.indexColors=0;//颜色值索引
this.interTime=interTime;//跳动的间隔时间
}
MagicColor.prototype.timerCall=function(){
var elem=this.elements,cls=this.colors;
var color=this.colors[(this.indexColors++) % this.colors.length];//去除当前颜色
for(var i=0;i<elem .length;i++){
elem[i].style.color=color;
}
};
MagicColor.prototype.reset=function(){
if(this.timer){
clearInterval(this.timer);
delete this.timer;
}
};
MagicColor.prototype.start=function(){
this.reset();
var self=this;
this.timer = setInterval(function(){
self.timerCall();
},self.interTime||100);
}
function $(id){return document.getElementById(id)}
var effect = new MagicColor([$("comment1"),$("comment2"),$("comment3")],["#000000","#CC0000","#C00000","#00CC00","#0000CC","#00CCCC","#CC00CC"],500);
effect.start();
感谢肉块的指教,欢迎大家拍砖。
下接:http://www.css88.com/archives/1921


2 条评论
建议添加demo页面。
才接触前端开发,不知道楼主的这些代码是在什么工具里写的?
一 个引用通告
[...] 上接http://www.css88.com/archives/1910 这里新加了一个extend方法. [...]