Created
February 15, 2019 06:03
-
-
Save sukhikh18/b488d36873154e9f863ad048ffa93e6e to your computer and use it in GitHub Desktop.
Own slick initialize method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************* Slick **********************************/ | |
window.slickSlider = function(target, props) { | |
var _defaults = { | |
autoplay: true, | |
autoplaySpeed: 4000, | |
dots: true, | |
infinite: false, | |
slidesToShow: 4, | |
slidesToScroll: 4, | |
responsive: [ | |
{ | |
breakpoint: 576, | |
settings: {} | |
}, | |
] | |
}; | |
try { | |
if( !props ) props = {}; | |
this.props = Object.assign(_defaults, props); | |
} catch(e) { | |
console.log('Init props is demaged.'); | |
this.props = _defaults; | |
} | |
this.$slider = $( target ); | |
this.isInit = false; | |
} | |
window.slickSlider.prototype = { | |
init: function( minWidth ) { | |
if( !this.$slider.length ) return false; | |
try { | |
if( !this.isInit ) { | |
if( undefined !== this.$slider.slick ) { | |
this.$slider.slick( this.props ); | |
this.isInit = this.$slider.hasClass('slick-initialized'); | |
} | |
else { | |
console.error('Slick library is not available!'); | |
} | |
} | |
} catch(e) { | |
console.error(e); | |
} | |
}, | |
responsive: function( minWidth ) { | |
var self = this; | |
if( !minWidth ) minWidth = 992; | |
if( !this.$slider.length ) return false; | |
$(window).on('load resize', function(e) { | |
if( minWidth < $(window).width() ) { | |
if( self.isInit ) { | |
self.$slider.slick('unslick'); | |
self.isInit = false; | |
} | |
} | |
else { | |
self.init(); | |
} | |
}); | |
} | |
}; | |
var slick = new slickSlider('.slider', {slidesToShow: 3, slidesToScroll: 1, responsive: [{ | |
breakpoint: 576, | |
settings: { | |
slidesToShow: 1 | |
} | |
}]}); | |
slick.responsive(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment