I'm using Masterslider to power an image carousel.
For desktop, I'd like the script to run layout: 'autofill'
. For mobile, I would like the script to run layout: 'boxed'
.
To achieve this, I am using window .resize()
with Modenizr mq:
( function( $ ) {
$( window ).resize(function() {
if (Modernizr.mq('(max-width: 767px)')) {
var slider = new MasterSlider();
slider.setup('masterslider' , {
width: 1440, // slider standard width
height: 400, // slider standard height
space: 5,
layout: 'boxed',
});
// adds Arrows navigation control to the slider.
slider.control('arrows');
} else {
var slider = new MasterSlider();
slider.setup('masterslider' , {
width: 1440, // slider standard width
height: 400, // slider standard height
space: 5,
layout: 'autofill',
});
// adds Arrows navigation control to the slider.
slider.control('arrows');
}
}).resize();
} )( jQuery ); // End JQuery
This method seems to fire the script multiple times with the following multiple errors in console:
Uncaught TypeError: Cannot read property 'position' of undefined
How should I run the script only once, calling the layout operator on resize?