问题
I'm using the awesome Slick slide on a project.
The problem is: I have a carousel with 5 divs inside. 4 images and a video.
On mobile (< 768px) I want the slider to show only the images, not the video. I tried to hide the video div by css but that's not working.
Any ideas on how to do this? Anyone had this need before?
回答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 and slickUnfilter 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="//placehold.it/900x300/c69/f9c/?text=1" alt="">
</div>
<div class="hide-on-mobile">
<img src="//placehold.it/900x300/9c6/cf9/?text=2" alt="">
</div>
<div class="hide-on-mobile">
<img src="//placehold.it/900x300/69c/9cf/?text=3" alt="">
</div>
<div>
<img src="//placehold.it/900x300/c33/f66/?text=4" alt="">
</div>
<div class="hide-on-mobile">
<img src="//placehold.it/900x300/099/3cc/?text=5" alt="">
</div>
<div>
<img src="//placehold.it/900x300/f93/fc6/?text=6" alt="">
</div>
</div>
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.0/slick/slick.min.css">
<link rel="stylesheet" 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>
回答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.
来源:https://stackoverflow.com/questions/32661011/how-to-remove-slick-slide-on-mobile