本文实例讲述了js实现的打字机效果。分享给大家供大家参考,具体如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- always force latest ie rendering engine (even in intranet) & chrome frame
remove this if you use the .htaccess -->
<meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1">
<title>js打字机效果</title>
<meta name="description" content="">
<meta name="author" content="administrator">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<!-- replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<style type = "text/css">
#main {
width: 80%;
height: 750px;
margin: auto;
padding: 10px;
background: #cfe1ca;
border: 10px outset #f9c6aa;
line-height: 30px;
color: #9f3c61;
font-size: 18px;
}
p {
text-indent: 30px;
}
</style>
<script type = "text/javascript">
var typewriter = {
msg: function(msg){
return msg;
},
len: function(){
return this.msg.length;
},
seq: 0,
speed: 150,//打字时间(ms)
type: function(){
var _this = this;
document.getelementbyid("main").innerhtml = _this.msg.substring(0, _this.seq);
if (_this.seq == _this.len()) {
_this.seq = 0;
cleartimeout(t);
}
else {
_this.seq++;
var t = settimeout(function(){_this.type()}, this.speed);
}
}
}
window.onload = function(){
alert("welcome to http://www.jb51.net")
var msg = "js打字机效果 ,原理很简单:每次多获取一个待打出的字符串的值,输出,覆盖原来输出的内容即可";
function getmsg(){
return msg;
}
typewriter.msg = getmsg(msg);
typewriter.type();
}
</script>
</head>
<body>
<div id = "main"> </div>
</body>
</html>
希望本文所述对大家javascript程序设计有所帮助。
更多js实现的打字机效果完整实例。