javascript
  • jquery
  • html
  • jquery-animate
  • innerhtml
  • 2012-04-19 61 views 2 likes 
    2

    我想有一個確實的setTimeout函數,然後改變的innerHTML:jquery - 動畫innerHTML可能嗎?

    <script type="text/javascript"> 
        $(document).ready(function(){ 
         setTimeout(function(){ 
          document.getElementById("middlecolor").innerHTML='new text new text'; 
         }, 1000); 
        }); 
    </script> 
    

    問題:我怎麼會動畫顯示的新文本,即一行行,而不是被寫入的所有立刻?

    感謝您的任何建議!

    +1

    是。不要使用'.innerHTML'。看看這個:http://stackoverflow.com/questions/1520178/jquery-using-append-with-effects – 2012-04-19 20:29:53

    +0

    我會做animate.setTimeout?或者把jquery html()放入animate腳本中? – 2012-04-19 20:31:40

    回答

    3

    行由行是有點棘手,但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);​ 
    

    我建議用打字機的效果,這也是很討人喜歡: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);​ 
    
    4

    嘗試這樣:

    <div id="text"> 
    </div> 
    
    $(document).ready(function() { 
        var interval = setInterval(function() { 
         $('#text').append('<p style="display: none;">new text</p>'); 
         $('#text p:last').fadeIn('slow'); 
        }, 5000); 
    }); 
    

    見的例子here

    如果你想殺死的間隔,可以這樣做:

    clearInterval(interval); 
    

    Greatings。

    +0

    地獄啊!非常感謝,這是偉大的 – 2012-04-19 21:09:15

    +1

    雖然一個問題,,,我怎麼得到它停止!? http://jsfiddle.net/dankpiff/7Jve8/4/ – 2012-04-19 21:09:44

    +0

    你需要殺死間隔,做這樣的事情:clearInterval(interval)...「間隔指針」存儲在interval變量中。 – 2012-04-19 21:53:18

    1

    下面是會後,其他動畫文本,一個多行的函數:

    <script type="text/javascript"> 
    $(document).ready(function(){ 
    
    function animateAddText(id, text, delta) { 
        var lines = text.split("\n"); 
        var lineCntr = 0; 
        var target = $("#" + id); 
    
        function addLine() { 
         if (lineCntr < lines.length) { 
          var nextLine = ""; 
          if (lineCntr != 0) { 
           nextLine = "<br>"; 
          } 
          nextLine += lines[lineCntr++]; 
          $("<span>" + nextLine + "</span>").hide().appendTo(target).fadeIn(1000); 
          setTimeout(addLine, delta); 
         } 
        } 
        addLine(); 
    } 
    
    var multilineText = "First line\nSecond Line\nThird Line\nFourth Line\nFifth Line"; 
    animateAddText("middlecolor", multilineText, 1000); 
    
    }); 
    </script> 
    

    和工作演示:http://jsfiddle.net/jfriend00/Gcg5T/

    +0

    謝謝,我很難讓這個工作,雖然:http://jsfiddle.net/dankpiff/2AtwM/ – 2012-04-19 21:07:56

    +0

    獲取最新版本,你可以看到它在我提供的jsFiddle鏈接工作。在我解決了幾個問題之前,您選擇了一個版本。 – jfriend00 2012-04-19 21:10:18

    相關問題