0

I have several instances of slider, and I need to pass the same parameters to every one.

I assume that I need array for that, but since I'm newbie in JS, I'm unsure how to do it properly.

Parameters looks for example like this:

spaceBetween: 20,
slidesPerView: 5,
breakpoints: {
  600: {
    slidesPerView: 2
  },
  991: {
    slidesPerView: 3
  },
  1200: {
    slidesPerView: 4
  }
}

and slider instance:

var swiper1 = new Swiper('.swiper1', {
   prevButton: '.b-prev-1',
   nextButton: '.b-next-1',

   //here I need to get the rest of the parameters

});

Thank you :)

1
  • look at $.extend - it allows you to merge objects together, so you can have var commonOpts = {spaceBetween: 20 ...} and then $.extend({}, commonOpts, swiper1Opts) Commented Oct 27, 2016 at 13:21

4 Answers 4

0

Hi you can write like below

 var swiper1 = new Swiper('.swiper1', [
   prevButton: '.b-prev-1',
   nextButton: '.b-next-1',
   //here I need to get the rest of the parameters
]);

Sign up to request clarification or add additional context in comments.

3 Comments

if you to use code snippet certified that works, but, maybe in your answer can be suitable to use code format.
I have used it because i have faced a problem. Problem i have faced it because of '[' its not formatting properly so i have added in to code snippet
Independent of the problem, if doesn't works no makes sense to use it.
0

Try it

function(arrayP){    
   for(var i = 0; i < arrayP.length; i++){
      alert(arrayP[i]);    //no .value here
   }
}

link:-Passing an array as parameter in JavaScript

Comments

0

You can use jQuery's $.extend() :

var extraDetails = {
    spaceBetween: 20,
    slidesPerView: 5,
    breakpoints: {
      600: {
        slidesPerView: 2
      },
      991: {
        slidesPerView: 3
      },
      1200: {
        slidesPerView: 4
      }
    }
};

var swiper1 = new Swiper('.swiper1', $.extend({ 
        prevButton: '.b-prev-1',
        nextButton: '.b-next-1'
    }, 
    extraDetails
));

Comments

0

The most eloquent way of doing this in ES5 is to create a new config object for each slide, but base it off of a common object using $.extend. (Object.assign() is similar in VanillaJS)

var commonConfig = { spaceBetween: 20, slidesPerView: 5, breakPoints: { ... } };

var swiper1 = $.extend(true, {}, commonConfig, {
    prevButton: '.b-prev-1',
    nextButton: '.b-next-1'
});

var swiper2 = $.extend(true, {}, commonConfig, {
    prevButton: '.b-prev-2',
    nextButton: '.b-next-2'
});

It'd be cool to see more code -- I imagine you could loop over your DOM elements and create the config on-the-fly for each one. Something like:

$('.swiper').each(function(idx, el){
    $(this).swiper({
        spaceBetween: 20,
        slidesPerView: 5,
        breakPoints: { ... },
        prevButton: '.b-prev-'+(idx+1),
        nextButton: '.b-next-'+(idx+1)
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.