简体   繁体   中英

jQuery plugin accept array as an option

I'm creating my first jQuery plugin.

var defaults = {
    breakpoint: {
        width: 1920,
        target: "",
        place: "after"
    }
}

Here is my default options. I would like to accept several breakpoints. Something like Slick carousel responsive: option.

$( "a" ).move({
    breakpoint: {
        width: 1800,
        target: "p",
        place: "after"
    },
    breakpoint: {
        width: 1500,
        target: "p",
        place: "after"
    }
});

When I try to do this, I get only last one breakpoint information(that one with 1500 width).

How can I correctly declare my defaults array to accept as many arrays of values as user wants?

use array of breakpoints would be a better solution instead of giving single object

$( "a" ).move({
    breakpoints: [{
        width: 1800,
        target: "p",
        place: "after"
    }, {
        width: 1500,
        target: "p",
        place: "after"
    }]
});

if breakpoints is an array it should be formatted as:

var defaults = {
    breakpoints: [
        {
            width: 1920,
            target: "",
            place: "after"
        }
    ]
}

and

I'm creating my first jQuery plugin.

var defaults = { breakpoints: { width: 1920, target: "", place: "after" } } Here is my default options. I would like to accept several breakpoints. Something like Slick carousel responsive: option.

$( "a" ).move({
    breakpoint: [
        {
            width: 1800,
            target: "p",
            place: "after"
        },
        {
            width: 1500,
            target: "p",
            place: "after"
        }
    ]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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