jQuery插件接受数组作为选项

jQuery plugin accept array as an option

本文关键字:选项 数组 插件 jQuery      更新时间:2023-09-26

我正在创建我的第一个jQuery插件。

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

这是我的默认选项。我想接受几个断点。类似Slick旋转木马的响应:选项。

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

当我尝试这样做时,我只得到最后一个断点信息(那个宽度为1500的断点)。

如何正确地声明默认数组以接受用户想要的任意多个值数组?

如果断点是一个数组,则应将其格式化为:

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

我正在创建我的第一个jQuery插件。

var默认值={断点:{宽度:1920,目标:",地点:"之后"}}这是我的默认选项。我想接受几个断点。类似Slick旋转木马的响应:选项。

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

使用断点数组将是更好的解决方案,而不是提供单个对象

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