Search code examples
javascriptjquerytransformcarouselslick.js

Switch vertical carousel (desktop) to horizontal carousel in mobile


They know a one plugin that let me change/transform a vertical (desktop view) carousel to a horizontal carousel on mobile devices?

Now I'm working with slick plugin but I can't get the result that I need, this is my JS code:

          $('.demo-slider).slick({
                      vertical: true;
                    });
           var windowWidth = $(window).width();
              if(windowWidth < 768){
                  $('.demo-slider').slick('unslick',function(){
                   $('.demo-slider).slick({
                      vertical: false;
                    });
                });
           }

Solution

  • I think where you are going wrong is you're initializing slick() twice (you also forgot closing ' on your jQuery selectors). I would switch the faux media query to be out of slick()'s initialization, like this:

    // 1- Get window width 
    var windowWidth = $(window).width();
    
    // 2- For all devices under or at 768px, use horizontal orientation
    if(windowWidth <= 768) {
      $('.demo-slider').slick({
        vertical: false,
      });
    } 
    // 3- For all devices over 768px, use vertical orientation
    else {
      $('.demo-slider').slick({
        vertical: true,
      });
    }
    

    *note, I've never used slickslider, just looking at your JS.