在Jquery中键入文本

Din*_*ivu 6 html javascript css jquery

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

这是我的演示

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>
Run Code Online (Sandbox Code Playgroud)

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);
}());
Run Code Online (Sandbox Code Playgroud)

Has*_*ami 9

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

.writer {
    font:bold 23px arial;
    white-space: pre-line;
}
Run Code Online (Sandbox Code Playgroud)

工作演示

16.6白色空间:"白色空间"属性

pre-line此值指示用户代理折叠空白序列.在保留的换行符处打破行,并根据需要填充行框.

值得注意的是,IE8 +支持pre-line值.

  • 我也很喜欢你的回答 (2认同)