javascript
  • jquery
  • html
  • jquery-animate
  • innerhtml
  • 2012-04-19 56 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

    相关问题