我需要创建一个滑块的几个实例.每个滑块都有两个参数:元素ID和包含不同选项的对象.所有滑块的选项都相同,除了一个选项(以下示例中的速度)
var swiper1 = new Swiper ('#swiper1', {
width: 200,
distance: 10,
slides: 5,
preventClick: true
// ... many others options
speed: 300
});
var swiper2 = new Swiper ('#swiper2', {
width: 200,
distance: 10,
slides: 5,
preventClick: true
// ... many others options
speed: 500
});
// ... many others instances of Swiper
Run Code Online (Sandbox Code Playgroud)
因此,为了减少代码的长度并避免多次复制/粘贴,我可以这样做:
var options = {
width: 200,
distance: 10,
slides: 5,
preventClick: true
// ... many others options
speed: 500
}
var swiper1 = new Swiper ('#swiper1', options)
var swiper2 = new Swiper ('#swiper2', options)
var swiper3 = new Swiper ('#swiper3', options)
// ...
Run Code Online (Sandbox Code Playgroud)
但正如我所提到的,只有一个参数因实例而异,所以我需要做类似的事情:
var options = {
width: 200,
distance: 10,
slides: 5,
preventClick: true
// ... many others options
}
var swiper1 = new Swiper ('#swiper1', {options, speed: 500})
var swiper2 = new Swiper ('#swiper2', {options, speed: 700})
var swiper3 = new Swiper ('#swiper3', {options, speed: 300})
// ...
Run Code Online (Sandbox Code Playgroud)
但我不知道这样做的正确方法是什么
您可以使用...创建包含options附加键的新对象speed:
new Swiper ('#swiper1', {...options, speed: 500})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |