aquaism
aquaism

Reputation: 43

making functions in Jquery

I`m making some functions with Jquery. but I got problem.

function fader(elm,delaytime) {
    elm.each(function(index){
        $(this).css('visibility','visible').delay(delaytime*index);
        $(this).fadeIn(1500);
    });
}
fader($('.subImgs').children(),200);

It was good, and I wanted more.

$.fn.fader2 = function(elm,delaytime) {
    elm.each(function(index){
        $(this).css('visibility','visible').delay(delaytime*index);
        $(this).fadeIn(1500);
    });
}

$('.subImgs').children().fader2(200);

It doesn't work. I know something was different but I couldn't find it. anyone can help me?

Upvotes: 2

Views: 99

Answers (2)

SpYk3HH
SpYk3HH

Reputation: 22570

You might also try:

(function($) {
    if (!$.myFader) {
        $.extend({
            myFader: function(elm, delaytime) {
                return elm.each(function(index){
                    $(this).css('visibility','visible').delay(delaytime*index).fadeIn(1500);
                });
            }
        });
        $.fn.extend({
            myFader: function(delaytime) {
                return $.myFader($(this), delaytime);
            }
        });
    }
})(jQuery);

This should allow both

$.myFader($('.subImgs').children(),200);

and

$('.subImgs').children().myFader(200);

Upvotes: 1

m90
m90

Reputation: 11822

Your function should probably read:

$.fn.fader2 = function(delaytime) {
    return this.each(function(index){
        $(this).css('visibility','visible').delay(delaytime*index).fadeIn(1500);
    });
}

Working fiddle (slighty different code for better visibility): http://jsfiddle.net/2HMWF/

See the link from the comment above for great information on that topic: http://docs.jquery.com/Plugins/Authoring

Upvotes: 2

Related Questions