在Jquery中键入文本

时间:2014-03-21 09:17:30

标签: javascript jquery html css

伙计们,我想把我的打字文本改为两行。

这是 Demo

Plz检查此代码

HTML

  <div id="container">

    <div class="writer">Hi! This is a test text. 
        <br><br>
        Can any one         
        Help me in this ?.</div>  

</div>

JAVA SCRIPT

var txt = $('.writer').text();
var timeOut;
var txtLen = txt.length;
var char = 0;
$('.writer').text('|');
(function typeIt() {   
    var humanize = Math.round(Math.random() * (200 - 30)) + 30;
    timeOut = setTimeout(function() {
        char++;
        var type = txt.substring(0, char);
        $('.writer').text(type + '|');
        typeIt();

        if (char == txtLen) {
            $('.writer').text($('.writer').text().slice(0, -1)); // remove the '|'
            clearTimeout(timeOut);
        }

    }, humanize);
}());

1 个答案:

答案 0 :(得分:9)

您可以向.writer添加white-space: pre-line;声明,以便打破这些行,如下所示:

.writer {
    font:bold 23px arial;
    white-space: pre-line;
}

<强> WORKING DEMO

  

<强> 16.6 White space: the 'white-space' property

     

pre-line此值指示用户代理折叠空白序列。行   在保留的换行符中被破坏,并且必要时填写   线盒。

值得注意的是pre-line价值is supported in IE8+