V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
Howcouleyoubeso

有关 js 的打字机效果问题

  •  1
     
  •   Howcouleyoubeso · Mar 21, 2016 · 2322 views
    This topic created in 3689 days ago, the information mentioned may be changed or developed.
    我是刚学 js 的新手,想在网页上实现打字机效果,但是却没成功,请各位帮忙看一下,谢谢。
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <div id="autotype">This is a test</div>
    <script type="text/javascript">
    $.fn.autotype = function() {
    var $tt = $(this);
    var str = $tt.html();
    var index = 0;
    $(this).html('');
    var timer = setInterval(function() {
    var current = str.substr(index, 1);

    if (current == '<')
    index = str.indexOf('>', index) + 1;
    else
    index++;

    $tt.html(str.substring(0, index) + (index & 1 ? '_' : ''));

    if (index >= str.length)
    clearInterval(timer);
    }, 55);
    };

    $("#autotype").autotype();

    </script>
    </body>
    </html>
    2 replies    2016-03-23 10:57:43 +08:00
    wyc829
        1
    wyc829  
       Mar 23, 2016   ❤️ 1
    有效果啊,就是要加上 jquery 就是了.
    Howcouleyoubeso
        2
    Howcouleyoubeso  
    OP
       Mar 23, 2016
    @wyc829 请问怎么加?$("#autotype").autotype(); 我不是已经调用函数了吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5918 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 01:57 · PVG 09:57 · LAX 18:57 · JFK 21:57
    ♥ Do have faith in what you're doing.