我有一个基于shuffleLetters.js扰乱字母的功能。效果很好,但是我似乎无法排除嵌套在我要定位的类中的类。

因此,我想对菜单项中的字符进行加扰。因此,我给他们提供了.shuffle类并按如下所示调用函数:

$(function(){
    var container = $(".shuffle")
    container.shuffleLetters();
});


.shuffleLetters与插件互动的地方。问题在于它正在使嵌套在该菜单项中的所有字符变得混乱,这是我不想要的。

我已经了解了.not方法,但是无法使其正常工作。

这是我正在尝试的:

<li class="shuffle">title
    <li class="no-shuffle">sub-title
    </li>
</li>


然后我就这样写我的js(或每种可能的变体):

$(function(){
    var container = $(".shuffle").not('.no-shuffle')
    container.shuffleLetters();
});


虽然这不起作用。

任何人都可以在这里向我指出正确的方向,使我的大脑耗时数小时。

一如既往,在此先感谢!

编辑:这是shuffleLetters.js

(function($){

    $.fn.shuffleLetters = function(prop){

        var options = $.extend({
            "step"      : 8,            // How many times should the letters be changed
            "fps"       : 25,           // Frames Per Second
            "text"      : "",           // Use this text instead of the contents
            "callback"  : function(){}  // Run once the animation is complete
        },prop)

        return this.each(function(){

            var el = $(this),
                str = "";


            // Preventing parallel animations using a flag;

            if(el.data('animated')){
                return true;
            }

            el.data('animated',true);


            if(options.text) {
                str = options.text.split('');
            }
            else {
                str = el.text().split('');
            }

            // The types array holds the type for each character;
            // Letters holds the positions of non-space characters;

            var types = [],
                letters = [];

            // Looping through all the chars of the string

            for(var i=0;i<str.length;i++){

                var ch = str[i];

                if(ch == " "){
                    types[i] = "space";
                    continue;
                }
                else if(/[a-z]/.test(ch)){
                    types[i] = "lowerLetter";
                }
                else if(/[A-Z]/.test(ch)){
                    types[i] = "upperLetter";
                }
                else {
                    types[i] = "symbol";
                }

                letters.push(i);
            }

            el.html("");

            // Self executing named function expression:

            (function shuffle(start){

                // This code is run options.fps times per second
                // and updates the contents of the page element

                var i,
                    len = letters.length,
                    strCopy = str.slice(0); // Fresh copy of the string

                if(start>len){

                    // The animation is complete. Updating the
                    // flag and triggering the callback;

                    el.data('animated',false);
                    options.callback(el);
                    return;
                }

                // All the work gets done here
                for(i=Math.max(start,0); i < len; i++){

                    // The start argument and options.step limit
                    // the characters we will be working on at once

                    if( i < start+options.step){
                        // Generate a random character at thsi position
                        strCopy[letters[i]] = randomChar(types[letters[i]]);
                    }
                    else {
                        strCopy[letters[i]] = "";
                    }
                }

                el.text(strCopy.join(""));

                setTimeout(function(){

                    shuffle(start+1);

                },1000/options.fps);

            })(-options.step);


        });
    };

    function randomChar(type){
        var pool = "";

        if (type == "lowerLetter"){
            pool = "abcdefghijklmnopqrstuvwxyz0123456789";
        }
        else if (type == "upperLetter"){
            pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        else if (type == "symbol"){
            pool = ",.?/\\(^)![]{}*&^%$#'\"";
        }

        var arr = pool.split('');
        return arr[Math.floor(Math.random()*arr.length)];
    }

})(jQuery);

最佳答案

尝试在.shuffle元素内的li span中包装文本节点“ title”



(function($){

    $.fn.shuffleLetters = function(prop){

        var options = $.extend({
            "step"      : 8,            // How many times should the letters be changed
            "fps"       : 25,           // Frames Per Second
            "text"      : "",           // Use this text instead of the contents
            "callback"  : function(){}  // Run once the animation is complete
        },prop)

        return this.each(function(){

            var el = $(this),
                str = "";


            // Preventing parallel animations using a flag;

            if(el.data('animated')){
                return true;
            }

            el.data('animated',true);


            if(options.text) {
                str = options.text.split('');
            }
            else {
                str = el.text().split('');
            }

            // The types array holds the type for each character;
            // Letters holds the positions of non-space characters;

            var types = [],
                letters = [];

            // Looping through all the chars of the string

            for(var i=0;i<str.length;i++){

                var ch = str[i];

                if(ch == " "){
                    types[i] = "space";
                    continue;
                }
                else if(/[a-z]/.test(ch)){
                    types[i] = "lowerLetter";
                }
                else if(/[A-Z]/.test(ch)){
                    types[i] = "upperLetter";
                }
                else {
                    types[i] = "symbol";
                }

                letters.push(i);
            }

            el.html("");

            // Self executing named function expression:

            (function shuffle(start){

                // This code is run options.fps times per second
                // and updates the contents of the page element

                var i,
                    len = letters.length,
                    strCopy = str.slice(0); // Fresh copy of the string

                if(start>len){

                    // The animation is complete. Updating the
                    // flag and triggering the callback;

                    el.data('animated',false);
                    options.callback(el);
                    return;
                }

                // All the work gets done here
                for(i=Math.max(start,0); i < len; i++){

                    // The start argument and options.step limit
                    // the characters we will be working on at once

                    if( i < start+options.step){
                        // Generate a random character at thsi position
                        strCopy[letters[i]] = randomChar(types[letters[i]]);
                    }
                    else {
                        strCopy[letters[i]] = "";
                    }
                }

                el.text(strCopy.join(""));

                setTimeout(function(){

                    shuffle(start+1);

                },1000/options.fps);

            })(-options.step);


        });
    };

    function randomChar(type){
        var pool = "";

        if (type == "lowerLetter"){
            pool = "abcdefghijklmnopqrstuvwxyz0123456789";
        }
        else if (type == "upperLetter"){
            pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        else if (type == "symbol"){
            pool = ",.?/\\(^)![]{}*&^%$#'\"";
        }

        var arr = pool.split('');
        return arr[Math.floor(Math.random()*arr.length)];
    }

})(jQuery);

$(function() {
    var container = $(".shuffle span:first")
    container.shuffleLetters();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<ul>
<li class="shuffle"><span>title</span>
  <ul>
    <li class="no-shuffle">sub-title
    </li>
    </ul>
</li>
</ul>

09-13 10:08