0

I have in my page two sliders which share the same options:

var options1 = {
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    itemWidth: 310,
    itemMargin: 5
}

var options2 = {
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false
}

And then I have something like this:

$('#carousel').flexslider(options1);
$('#slider').flexslider(options2);

$('#carousel2').flexslider(options1);
$('#slider2').flexslider(options2);

The problem is I need to add to the end of the options one option that is different for each one, which is this: asNavFor: '#slider' for options1 and sync: "#carousel" for options two. I tried this but it didn't work:

$('#carousel').flexslider(options1, {asNavFor: '#slider'});
$('#slider').flexslider(options2, {sync: "#carousel"});

$('#carousel2').flexslider(options1, {asNavFor: '#slider2'});
$('#slider2').flexslider(options2, {sync: "#carousel2"});

I want to avoid repeating the options because they're the same for every slider. Thanks in advance!

1
  • You could look into Object.assign Commented Jun 19, 2016 at 21:00

3 Answers 3

1

You can use Object.assign to crete a new object with your options as a template:

$('#carousel').flexslider(Object.assign({}, options1, {asNavFor: '#slider'}));
$('#slider').flexslider(Object.assign({}, options2, {sync: "#carousel"}));

$('#carousel2').flexslider(Object.assign({}, options1, {asNavFor: '#slider2'}));
$('#slider2').flexslider(Object.assign({}, options2, {sync: "#carousel2"}));

If you need to support old browsers you can use jQuery's $.extend or a polyfill instead.

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

2 Comments

Thanks for the answering, but I'll use a foor loop because there may be more than 2 galleries and I can avoid this problem!
Not sure how that plugin works but perhaps you could use a common class instead of unique ids.
1

You could to add the attributes to the object before passing it to the flexslider() :

options1['asNavFor'] = '#slider';
options2['sync'] = '#carousel';

$('#carousel').flexslider(options1);
$('#slider').flexslider(options2);

Then override it with new values before second call :

options1['asNavFor'] = '#slider2';
options2['sync'] = '#carousel2';

$('#carousel2').flexslider(options1);
$('#slider2').flexslider(options2);

You could use for loop like :

var nbr_elements = 3;

for(var i=1;i<nbr_elements;i++)
{
    if(i==1){
        options1['asNavFor'] = '#slider';
        options2['sync'] = '#carousel';

        $('#carousel').flexslider(options1);
        $('#slider').flexslider(options2);
    }else{
        options1['asNavFor'] = '#slider'+i;
        options2['sync'] = '#carousel'+i;

        $('#carousel'+i).flexslider(options1);
        $('#slider'+i).flexslider(options2);
    }
}

Hope this helps.

2 Comments

Thanks, this worked, I figured anyways that I can make a for loop and avoid this problem completely (:
You're weclome, how much carousel/slider you have ? check my update.
0

I'd recommend using jQuery's extend to dynamically create extended options like this:

options1Nav1 = $.extend({}, options1, {asNavFor: '#slider'});
options2Sync1 = $.extend({}, options2, {sync: '#carousel'});

options1Nav2 = $.extend({}, options1, {asNavFor: '#slider2'});
options2Sync2 = $.extend({}, options2, {sync: '#carousel2'});

and then calling

$('#carousel').flexslider(options1Nav1);
$('#slider').flexslider(options2Sync1);

$('#carousel2').flexslider(options1Nav2);
$('#slider2').flexslider(options2Sync2);

Of course you can pass the dynamically extended options directly as well.

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.