简体   繁体   English

使用jQuery键入文本动画

[英]Typing text animation using jQuery

hopefully someone can help me with this. 希望有人可以帮助我。 I am kinda new to jQuery, but I'm trying to figure out a way to create an animated typing text effect for an entire div class! 我是jQuery的新手,但我试图找出一种为整个div类创建动画键入文本效果的方法! A long google search has only given me small plugins, although they are great... They just don't do what I need them to do… (or I just don't have the education to set it up)… 漫长的google搜索只给了我一些小插件,尽管它们很棒……他们只是不做我需要他们做的事情……(或者我只是没有受过设置的教育)……

Basically, as soon as the page loads I need to have an entire pre-existing div class inside a block of html (I don't want the text to be provided by the JS) start to 'type' out the text as a human would, keeping all CSS of the internal elements (h1s, h2s, p, spans, etc.) and in the order that they are in the flow of the page. 基本上,页面加载后,我需要在一个html块中包含一个完整的预先存在的div类(我不希望JS提供文本)开始以人工方式“键入”文本将保持内部元素的所有CSS(h1,h2s,p,span等),并保持它们在页面流中的顺序。

This is the file I'm working on http://jsfiddle.net/we9d81jf/ 这是我正在使用的文件http://jsfiddle.net/we9d81jf/

var $el = $('.typing'),
    txt = $el.text(),
    txtLen = txt.length,
    timeOut,
    char = 0;

$el.text('|');

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

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

    }, humanize);

}());

Any help would be greatly appreciated! 任何帮助将不胜感激!

You are almost there, if you want to see the effects of html tags use .html instead of .text() . 如果您想查看html标记的效果,就快到了,使用.html而不是.text() Here is an updated fiddle . 这是更新的小提琴

Why don't use jQuery plugin such as Typed.js . 为什么不使用Typed.js之类的 jQuery插件。 It works perfect, the '<' and '>' won't display. 它工作完美,不会显示“ <”和“>”。 You can also dive into the source code to find out how it work. 您还可以深入研究源代码以了解其工作原理。

This is a demo (The string contains <br> and you can add some tag to test it). 这是一个演示 (字符串包含<br> ,您可以添加一些标签对其进行测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM