简体   繁体   English

我如何使这个ghostType jQuery函数做换行符?

[英]How do I make this ghostType jquery function do line breaks?

I'm playing around with a jquery function called ghostType which basically types out text as though it is being typed on screen. 我在玩一个名为ghostType的jQuery函数,该函数基本上将文本键入,就像在屏幕上键入一样。 Its kinda cool, but I can't seem to have it do line breaks. 它有点酷,但我似乎无法进行换行。

The jquery is as follows: jQuery如下所示:

(function( $ ){
  $.fn.ghostType = function() {

    return this.each(function() {

        var $this = $(this);
        var str = $this.text();
        $this.empty().show();
        str = str.split("");
        str.push("_");

        // increase the delay to ghostType slower
        var delay = 100;

        $.each(str, function (i, val) {
            if (val == "^") {
                // Do nothing. This will add to the delay.
            }
            else {
                $this.append('<span>' + val + '</span>');
                $this.children("span").hide().fadeIn(100).delay(delay * i);

            }
        });
        $this.children("span:last").css("textDecoration", "blink");

    });

};
})( jQuery );

From what I can tell this code takes each character in the chosen elements TEXT and puts it into seperate tags, therefor omitting the HTML (ie br's) with the line var str = $this.text(); 据我所知,这段代码将所选元素TEXT中的每个字符放入单独的标签中,因此省略了HTML(即br),其行为var str = $ this.text();

How would you go about making this code include line breaks? 您将如何使该代码包含换行符?

The best I could come up with was by adding an extra 'else if' statement like so: 我能想到的最好的方法是添加一个额外的“ else if”语句,如下所示:

            else if ( val == "*" ) {
                $this.append('<br />');
            }

And therefor * signs would become line breaks... but this damages the functionality where blinking cursor doesn't sit beside each letter as it fades in. otherwise, it works... 因此*符号会变成换行符...但是这会损坏闪烁的光标不会在每个字母旁边出现的功能,否则会起作用。

You can see an example of what I've done at http://jsfiddle.net/JNyQV/ 您可以在http://jsfiddle.net/JNyQV/上看到我所做的示例。

(function( $ ){
$.fn.ghostType = function() {

    return this.each(function() {

        var $this = $(this);
        var str = $this.text();
        $this.empty().show();
        str = str.split("");
        str.push("_");

        // increase the delay to ghostType slower
        var delay = 55;

        $.each(str, function (i, val) {
            if (val == "^") {
                // Do nothing. This will add to the delay.
            }
            else {
                if (val == "*") val = "<br/>";
                $this.append('<span>' + val + '</span>');
                $this.children("span").hide().fadeIn(100).delay(delay * i);

            }
        });
        $this.children("span:last").css("textDecoration", "blink");

    });

};
})( jQuery );

$('#example').ghostType();

Your best bet is to treat the selected element as an element, and not just simply grab its text. 最好的选择是将所选元素视为元素,而不仅仅是获取其文本。

Here is an excerpt from one of my plugins that handles text... 这是我的一个处理文本的插件的摘录...

var findText = function(element, pattern, callback) {
    for (var childi= element.childNodes.length; childi-->0;) {
        var child= element.childNodes[childi];
        if (child.nodeType==1) {
            findText(child, pattern, callback);
        } else if (child.nodeType==3) {
            var matches= [];
            var match;
            while (match= pattern.exec(child.data))
                matches.push(match);
            for (var i= matches.length; i-->0;)
                callback.call(window, child, matches[i]);
        }
    }
}

I originally got this piece of code from an answer here by bobince . 我最初是从bobince的答案中得到这段代码的。

By examining the rest of my plugin , you should be able to pick up how it works. 通过检查插件的其余部分,您应该能够了解它的工作原理。 Basically, it iterates over all text nodes recursively, wrapping each character in a span which is later animated. 基本上,它以递归方式遍历所有文本节点,将每个字符包装在一个span ,然后span动画显示。

This is how I would write the script... just add the entire partial string instead of each individual letter. 这就是我编写脚本的方式...只需添加整个部分字符串而不是每个单独的字母。 The biggest difference is that the last letter doesn't fade in. I didn't think the effect was that noticeable, but it should be possible to just add the last letter and have that fade in if it is a necessity. 最大的区别是最后一个字母不会淡入。我认为效果没有那么明显,但是应该可以添加最后一个字母并在需要时淡入。 Here is a demo . 这是一个演示

(function($) {
    $.fn.ghostType = function() {

        return this.each(function() {

            // increase the delay to ghostType slower
            var delay = 55,

            $this = $(this),
            str = $this.text(),
            i = 0,
            l = str.length,
            t,
            loop = function() {
                if (i <= l) {
                    t = str.substring(0,i++).replace(/\^/g, '').replace(/\*/g, '<br>');
                    $this.html(t + '<span class="blink" >_</span>');
                    setTimeout(loop, delay);
                }
            };

            $this.empty().show();
            loop();
        });

    };
})(jQuery);

$('#example').ghostType();

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

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