Jquery - Animate innerHTML possible?

后端 未结 3 1050
暖寄归人
暖寄归人 2021-01-21 23:56

I\'m trying to have a function that does setTimeout, then changes the innerHTML:



        
3条回答
  •  没有蜡笔的小新
    2021-01-22 00:44

    Line-by-line is a bit tricky, but possible.

    var ps = document.getElementById("text").children;
    var i = 0;
    var $p = $(ps[i]);
    
    setTimeout(function newline(){
    $p.css("height", function(index, h){
        h = parseInt(h);
        h += parseInt($p.css("line-height"));
        console.log(h,  ps[i].scrollHeight);
        if (h > ps[i].scrollHeight)
            $p = $(ps[++i]);
        return h;
    }); 
    if (i < ps.length)
        setTimeout(newline, 200);
    }, 200);​
    

    I'd suggest to use a typewriter effect, which is also very likable: http://jsfiddle.net/pZb8W/1/

    var ps = document.getElementById("text").children;
    var i = 0;
    var $p, text;
    var speed = 20;
    
    setTimeout(function newchar(){
    if (!text) {
        $p = $(ps[i++]); 
        text = $p.text();
        $p.empty().show();
    }
    $p.append(document.createTextNode(text.charAt(0)));
    text = text.slice(1);
    if (text.length || i < ps.length)
        setTimeout(newchar, Math.random()*speed+speed);
    }, 3*speed);​
    

提交回复
热议问题