How To Remove Slick Slide On Mobile?
Solution 1:
Look at the «Filtering» demo at the Slick's documentation. Let's use it to solve the issue.
Set breakpoints that you need, using the
responsive
option.Catch the
breakpoint
event and read theactiveBreakpoint
property of the carousel. Notabene: when the screen width is greater than the last of the breakpoints, this property returnsnull
.Call
slickFilter
andslickUnfilter
methods:slickFilter
Argumentfilter
: selector or function
Filters slides using jQuery.filter
syntaxslickUnfilter
Removes applied filterBeware of an infinite loop: these methods cause a re-initialization of the slider, during which the
breakpoint
event again occurs (unless the breakpoint isnull
).Call these methods during initialization too. Don’t forget to define the
init
handler before the initialization.
Check the result: https://codepen.io/glebkema/pen/NaGGzv
Slides with the .hide-on-mobile
class become hidden when the screen width is 700px
or less.
var breakpointMobile = 700,
isChanging = false,
isFiltered = false;
$('#breakpointMobile').text( breakpointMobile );
$('#myCarousel').on('init breakpoint', function(event, slick){ /** 2. and 5. **/
if ( ! isChanging ) { /** 4. **/
$('#breakpointValue').text( String(slick.activeBreakpoint) );
isChanging = true;
if ( slick.activeBreakpoint && slick.activeBreakpoint <= breakpointMobile) {
if ( ! isFiltered ) {
slick.slickFilter(':not(.hide-on-mobile)'); /** 3. **/
isFiltered = true;
}
} else {
if ( isFiltered ) {
slick.slickUnfilter();
isFiltered = false;
}
}
isChanging = false;
}
}).slick({
autoplay: true,
dots: true,
responsive: [ /** 1. **/
{ breakpoint: 500 },
{ breakpoint: 700 },
{ breakpoint: 900 }
]
});
body { /** Decorations **/
font-family: 'Open Sans', sans-serif;
}
.my-carousel img {
width: 100%;
}
.my-carousel .slick-next {
right: 15px;
}
.my-carousel .slick-prev {
left: 15px;
z-index: 1;
}
<h3>Slides 2, 3 and 5 become hidden when the screen width is <span id="breakpointMobile"></span>px or less</h3>
<p>Active breakpoint is <b id="breakpointValue"></b>. Try to resize the workspace.</p>
<div id="myCarousel" class="my-carousel">
<div>
<img src="https://via.placeholder.com/900x300/c69/f9c/?text=1" alt="">
</div>
<div class="hide-on-mobile">
<img src="https://via.placeholder.com/900x300/9c6/cf9/?text=2" alt="">
</div>
<div class="hide-on-mobile">
<img src="https://via.placeholder.com/900x300/69c/9cf/?text=3" alt="">
</div>
<div>
<img src="https://via.placeholder.com/900x300/c33/f66/?text=4" alt="">
</div>
<div class="hide-on-mobile">
<img src="https://via.placeholder.com/900x300/099/3cc/?text=5" alt="">
</div>
<div>
<img src="https://via.placeholder.com/900x300/f93/fc6/?text=6" alt="">
</div>
</div>
<link href="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.0/slick/slick.min.css">
<link href="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.0/slick/slick-theme.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.0/slick/slick.min.js"></script>
Solution 2:
Slick has the method slickRemove (you have to scroll down a bit):
Remove slide by index. If removeBefore is set true, remove slide preceding index, or the first slide if no index is specified. If removeBefore is set to false, remove the slide following index, or the last slide if no index is set.
I would just call this method and remove the slide with the video if your current screen size is lower than 768px.
Solution 3:
You can unslick at a given breakpoint now by adding:
settings: "unslick"
instead of a settings object
To remove slick slider on mobile view add above code snippet to responsive breakpoint like this
$('.responsive').slick({
dots: true,
infinite: false,
speed: 300,
slidesToShow: 4,
slidesToScroll: 4,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true
}
},
{
breakpoint: 600,
settings: "unslick"
}
// You can unslick at a given breakpoint now by adding:
// settings: "unslick"
// instead of a settings object
]
});
Post a Comment for "How To Remove Slick Slide On Mobile?"