简体   繁体   English

使用jQuery对数组中的元素进行动画处理

[英]Animate in elements from array using jQuery

I am working on a jQuery plugin that hides all the elements within a container and then shows them over a set interval using either fadeIn() or by jquery's animate function. 我正在使用一个jQuery插件,该插件将容器中的所有元素都隐藏起来,然后使用fadeIn()或jquery的animate函数在设定的间隔内显示它们。

So far I've managed to get all the elements into an array and I can print out the html in an alert if i do 到目前为止,我已经设法将所有元素放入数组,如果可以,我可以在警报中打印出html。

$(children).each(function (index, val) {
     alert(this);
});

However, when i try to add the elements as html again to the document i get no luck. 但是,当我尝试再次将元素作为html添加到文档中时,我没有运气。

I've tried 我试过了

$(container).append(this);

and

$(container).appendChild(this);

but still no luck. 但仍然没有运气。

What i need ideally, is to be able to fadeIn() each element again and also add a css class to each element over a set interval. 我理想地需要的是能够再次淡入每个元素,并在设定的间隔内向每个元素添加一个CSS类。

(function($) {

$.fn.myPlugin = function (options) {

    // Set default options

    var defaults = {
        rate : '1000',
    }

    var options = $.extend(defaults, options);


    // Hide all elements within parent container
    $(this).children().hide();


    var container = $(this).selector;

    // Store children in loader variable
    var loader = $(this).children();


    // Put all elements into an array

    var children = [];

    $(loader).each(function(idx) {
        children.push(this.outerHTML); 
    });


    // Iterate over array and fadeIn elements;

    $(children).each(function (index, val) {


    });


};

})(jQuery);

Somethink like this?: http://jsfiddle.net/zKpp2/1/ 像这样吗?: http : //jsfiddle.net/zKpp2/1/

(function ($) {
    $.fn.myPlugin = function (options) {
        // Set default options
        var defaults = $.extend({
            rate: 1000,
        }, options);

        // Hide all elements within parent container
        $(this).children().hide();
        var container = $(this).selector;

        // Store children in loader variable
        var loader = $(this).children(),
            length = loader.length;

        (function fadeLoop(index){
            if (index < length)
                loader.eq(index).fadeIn(defaults.rate, function(){
                    $(this).addClass('foo'); // add class when done animating.
                    fadeLoop(index + 1);
                });
        })(0);
    };
})(jQuery);

However, I would recommend something a bit more flexible: http://jsfiddle.net/zKpp2/3/ 但是,我建议您使用一些更灵活的方法: http : //jsfiddle.net/zKpp2/3/

(function ($) {
    $.fn.myPlugin = function (options) {
        // Set default options
        var def = $.extend({
            rate: 1000,
            onStepStart: $.noop,
            onStepFinish: $.noop,
            onFinish: $.noop
        }, options);

        // Hide all elements within parent container
        $(this).children().hide();
        var container = this;

        // Store children in loader variable
        var loader = $(this).children(),
            length = loader.length;

        (function fadeLoop(index) {
            if (index < length) {
                def.onStepStart.apply(
                loader.eq(index).fadeIn(def.rate, function () {
                    def.onStepFinish.apply(this);
                    fadeLoop(index + 1);
                }).get());
            } else def.onFinish.apply(container.get());
        })(0);

        return container;
    };
})(jQuery);

Which you could use like this to accomplish the same thing you want (as well as many other things): 您可以像这样使用它来完成您想要的同一件事(以及许多其他事情):

$("#loader").myPlugin({
    rate: 1000,
    onStepStart: function(){
        $(this).addClass("foo");  // "this" just started fading in
    },
    onStepFinish: function(){
        $(this).removeClass("foo").addClass("bar");  // "this" finished fading in
    },
    onFinish: function(){
        $(this).css("background", "green");  // "this" is the original selector.
    }
}).css("background", "red");  // chains properly

Edit — The second version of the plugin doesn't verify that def.onStepStart etc are actually functions, so it will break if you set them to something other than a function. 编辑 -插件的第二个版本不验证def.onStepStart等实际上是函数,因此如果将它们设置为函数以外的其他函数,则它将中断。

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

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