我检查了react-slick 的源代码并找到了下面显示的代码部分;
if (this.props.responsive) {
var breakpoints = this.props.responsive.map(breakpt => breakpt.breakpoint);
breakpoints.sort((x, y) => x - y);
breakpoints.forEach((breakpoint, index) => {
var bQuery;
if (index === 0) {
bQuery = json2mq({minWidth: 0, maxWidth: breakpoint});
} else {
bQuery = json2mq({minWidth: breakpoints[index-1], maxWidth: breakpoint});
}
this.media(bQuery, () => {
this.setState({breakpoint: breakpoint});
});
});
// Register media query for full screen. Need to support resize from small to large
var query = json2mq({minWidth: breakpoints.slice(-1)[0]});
this.media(query, () => {
this.setState({breakpoint: null});
});
}
我从这段代码中了解到的是react-slick实际上同时使用了这两种方法,min-width and max-width
但是在对给定的断点进行排序之后,第一个总是会得到一个min-width: 0
.
所以这是你可以做的事情。它有点hack,但我认为它可以完成工作。
const settings = {
dots: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
responsive: [
{
breakpoint: 768,
},
{
breakpoint: 10000, // a unrealistically big number to cover up greatest screen resolution
settings: 'unslick'
}
]
};
这段代码所做的是,它创建了 2 个媒体条目,
@media only screen and (min-width: 0px) and (max-width: 768px) { ... } // empty
@media only screen and (min-width: 768px) and (max-width: 10000px) { ... } // disables slick
希望能帮助到你。