js一段文本,文字一个字一个字出现,像打字一样

实现方式1

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        window.οnlοad=function(){
            var story = document.getElementById('word');
            var s = document.getElementById('show');
            var i = 0;
            timer=setInterval(function(){
                s.innerHTML=story.innerHTML.substring(0,i);
                i++;
                if(s.innerHTML==story.innerHTML){
                    clearInterval(timer);
                }
            },200);
        }
    </script>
</head>
<body>
    <p id="word" style="display:none;">大家好,我是东,来自南方。</p>
    <p id="show"></p>
</body>
</html>

实现方式2

<!DOCTYPE html>
<html lang="zh">

	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
		<link rel="stylesheet" type="text/css" href="zxx.lib.css"/>
		<style>body{  
    line-height:1.4;  
    color:#333;  
    font-family:arial;  
    font-size: 12px;  
}  
input,textarea,select{  
    font-size:100%;      
    font-family:inherit;  
}  
body,h1,h2,h3,h4,h5,h6,p,ul,ol,form{  
    margin:0;  
}  
h4,h5,h6{  
    font-size:1em;  
}  
ul,ol{  
    padding-left:0;   
    list-style-type:none;  
}  
img{border:0;}  
		</style>
	</head>

	<body>
		<pre id="aa"></pre>
<div style="display:none" id="w">大家好,我是东,来自南方。
</div>
	</body>
	<script>
		var index=0;
var word=document.getElementById("w").innerHTML;
function type(){
    document.getElementById("aa").innerText = word.substring(0,index++);
}
setInterval(type, 250);
	</script>
	<script src="jquery.min.js"></script>
</html>


版权声明:本文为dongsdh原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。