创建一个可以作为 swiper JS 参数传入的对象

Creating an object that could be passed in as parameters for swiper JS

我正在尝试根据用户输入创建一个滑动器实例,例如:

用户输入字符串:

swiper-direction-horizontal swiper-effect-slide swiper-speed-300

或类似的东西。

然后使用 javascript/jquery 我可以获取该字符串并将其转换为变量并使用它来创建滑动器:

var swiperParams = {direction: horizontal, effect: slide, speed: 300}
var mySwiper = new Swiper('.swiper-container', swiperParams);

最好的方法是什么? (性能很重要)

找到方法了,不过还是谢谢了。 这是一个代码片段https://jsfiddle.net/z5pgarnq/10/,如果有人有更好的解决方案,我将不胜感激。

var input = 'swiper-direction-horizontal swiper-speed-300 swiper-loop-true';
var array = input.split(' ');
var params = {};
var value = '';
for (i = array.length - 1; i >= 0; i--) {
  if (!array[i].indexOf('swiper')) {
    value = array[i].split('-')[2];
    if ($.isNumeric(value)) {
      value = parseInt(value, 10);
    } else if (value === 'true') {
      value = (value === 'true');
    }
    params[array[i].split('-')[1]] = value;
  }
}
console.log(params);
//console:
//Object { loop: true, speed: 300, direction: "horizontal" }