ronnyrr
ronnyrr

Reputation: 1551

jQuery call plugin with own init function

I started trying to create my own plugin using jQuery and the prototype function. My code looks like the following:

var Slider = function(element, controls, interval) {
    this.element = element;
    this.controls = controls;
    this.interval = interval || 5000;

    this.init();
};

Slider.prototype = {
    init: function() {
        if (this.controls)
            this.setControls();

        this.element.hide();
        this.slides[0].show();
        this.fadeIt();
    }
}

And inside prototype are more functions. Currently I'm creating a slider by calling the following:

new Slider($(".slide"), $(".slider").find(".controls").find("i"), 6000);

But how I want to do is is something like this:

Slider.init({
   element: $(".slide"),
   controls: $(".slider").find(".controls").find("i"),
   interval: 6000,
});

I tried searching for a solutions but I only could find how to make something like this:

$(".slider").Slider();

Anyone who can help me out or knows what to search for? Thanks instead!

Upvotes: 1

Views: 5438

Answers (1)

Sonny
Sonny

Reputation: 8326

I was looking up "jQuery Plugin init" when I found your question. I also found this great article with a jQuery Plugin boilerplate that include an init() function.

Upvotes: 1

Related Questions