一个朋友急着用,所以写了这个效果,代码还有改进得地方,大家可以尝试去修改以使更简洁,然后帖出来大家一起分享
body{margin:0px;font:"宋体" 12px; background-color:#000; color:#fff}
var typer={
container : null,
speed : 100 ,
innerhtml : null ,
curpos : 0,
interval1 : null,
interval2 : null,
init : function(obj,speed){
typer.container = obj;
if(speed)typer.speed = speed;
typer.start();
},
start : function(){
typer.innerhtml = clearspace(typer.container.innerhtml).tolowercase();
typer.container.innerhtml = "";
typer.interval1 = setinterval("typer.type()",typer.speed);
},
type : function(){
if(typer.curpos=typer.curpos+4){
if(typer.innerhtml.substring(typer.curpos,typer.curpos+4)=="
") {
tempstr="
";
typer.curpos = typer.curpos+4;
}else if(typer.innerhtml.substring(typer.curpos,typer.curpos+4)=="
"){
tempstr="
";
typer.curpos = typer.curpos+4;
}else{
if(typer.innerhtml.length>=typer.curpos+3 && typer.innerhtml.substring(typer.curpos,typer.curpos+3)=="
"){
tempstr="
";
typer.curpos = typer.curpos+3;
}else{
tempstr=typer.innerhtml.substring(typer.curpos,typer.curpos+1);
typer.curpos++;
}
}
}else if(typer.innerhtml.length>=typer.curpos+3){
if(typer.innerhtml.substring(typer.curpos,typer.curpos+3)=="
"){
tempstr="
";
typer.curpos = typer.curpos+3;
}else{
tempstr=typer.innerhtml.substring(typer.curpos,typer.curpos+1);
typer.curpos++;
}
}else{
tempstr=typer.innerhtml.substring(typer.curpos,typer.curpos+1);
typer.curpos++;
}
typer.container.innerhtml = typer.container.innerhtml.substring(0,typer.container.innerhtml.length-1)+tempstr;
}
}else{
clearinterval(typer.interval1);
typer.interval2 = setinterval("typer.last()",typer.speed);
}
},
last : function(){
if(typer.container.innerhtml.substring(typer.container.innerhtml.length-1)!="_")
typer.container.innerhtml +="_";
else
typer.container.innerhtml = typer.container.innerhtml.substring(0,typer.container.innerhtml.length-1);
}
}
function clearspace(str){
if(str!=""){
str = str.replace(/^\s*/g,"").replace(/\s*$/g,"");
}
return str;
}
姓名: 冷风
email: cityvoice@hotmail.com
籍贯: 湖南
爱好:打球,旅游,玩星际,唱歌
[ctrl+a 全选 注:如需引入外部js需刷新才能执行]