Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only use slick.js on mobile screen size

I only want the slick.js plugin to be active when on a mobile screen size, let's say under 450px for example. I want this to happen on either page load or browser resize. Slick.js does work properly if I load the page or resize to the correct width but not if I resize the browser to greater than 450px. Can this be done?

   $(document).ready(function(){
    $(window).resize(function(){
      if($(window).width() < 450) {
        $('.round-card-slick').slick({
        });
      } else {
        $('.round-card-slick').unslick();
      }
    });
  }); 
like image 635
user1406737 Avatar asked Sep 23 '14 20:09

user1406737


2 Answers

btw now you can do this directly in the slick responsive settings object

mobileFirst: true, 
responsive: [
   {
      breakpoint: 480,
      settings: "unslick"
   }
]
like image 50
James Daly Avatar answered Oct 21 '22 17:10

James Daly


The problem is unslick keeps firing on resize even after it has been unslicked, which in turn, breaks it. A work around that my coworker came up with goes like this check..

var target = $('.slick-mobile-gallery');
if(target.hasClass('slick-initialized'))
  target.unslick();

Update:

The solution is built-in feature in slick plugin & @James Daly's answer is the true answer to this question.

like image 34
user1406737 Avatar answered Oct 21 '22 17:10

user1406737