JS实现打字效果

最近参加了CSDN论坛举办的214情人节活动,就写个了页面,


效果见:http://withiter.com/cross.jsp

这里我把代码共享出来,供大家参考:

    <html>  
    <head>  
    <title>I just wanna stay</title>  
    <style type="text/css">  
    a {  
        text-decoration: none  
    }  
    </style>  
    </head>  
    <body>  
        <div id="newsticker">  
            <span id="tickertitle"></span><span style="color:green;" id="typewriter"  
                href="javascript:void(0);"></span>  
        </div>  
    </body>  
    </html>  
    <script type="text/javascript">  
        var current = 0;  
        var x = 0;  
        var speed = 100;  
        var speed2 = 10000;  
        function initArray(n) {  
            this.length = n;  
            for ( var i = 1; i <= n; i++) {  
                this[i] = ' ';  
            }  
        }  
        typ = new initArray(16);  
        typ[0] = "1) Do you have a map? Because I just keep losing in your eyes.<br/><br/>"  
                + "2) Meeting you was fate,and falling in love with you was out of my control.<br/><br/>"  
                + "3) No man or woman is worth your tears and the one who is,won't make your cry.<br/><br/>"  
                + "4) There are two reasons why I wake up in the morning: my alarm clock and you.<br/><br/>"  
                + "5) You are everything to me, and I was so blessed when god sent you here for me<br/><br/>"  
                + "6) In spite of you and me and the silly world going to pieces around us,I love you.<br/><br/>"  
                + "7) If I could rearrange the alphabet,I'd put Y and I together.<br/><br/>"  
                + "8) It's not being in love that makes me happy, but is being in loving with you.<br/><br/>"  
                + "9) There are 4 steps to happiness: 1 you 2 me 3 our hearts 4 together<br/><br/>"  
                + "10) Love you so I don`t wanna go to sleep, for reality is better than a dream.<br/><br/>"  
                + "2014/2/14  Cross, Follow me at SinaWeibo <a target='_blank' href='http://weibo.com/343642038'>http://weibo.com/343642038</a><br/><br/>";  
                  
        function typewrite() {  
            var m = typ[current];  
            document.getElementById("typewriter").innerHTML = m.substring(0, x++)  
                    + "_";  
      
            if (x == m.length + 1) {  
                x = 0;  
                current++;  
                if (current > typ.length - 1) {  
                    current = 0;  
                }  
            } else {  
                setTimeout("typewrite()", speed);  
            }  
        }  
        typewrite();  
    </script>  

原文地址:https://www.cnblogs.com/ful1021/p/4804436.html