如何在 Swiper React 中设置响应式断点
当我们不使用断点时,它就能提供正确的结果。
由 Mux 赞助的 DEV 全球展示挑战赛:展示你的项目!
有人在 YouTube 上问我如何在Swiper 的新 React 版本中使用断点,所以这里就来解释一下。
首先,您需要在 jsx 中设置断点:
<Swiper
breakpoints={{
// when window width is >= 640px
640: {
width: 640,
slidesPerView: 1,
},
// when window width is >= 768px
768: {
width: 768,
slidesPerView: 2,
},
}}
>
{slides}
</Swiper>
然后,在 CSS 中添加自定义媒体查询:
.swiper-container {
width: 480px;
}
@media screen and (min-width: 640px) {
.swiper-container {
width: 640px;
}
}
@media screen and (min-width: 768px) {
.swiper-container {
width: 768px;
}
}
希望这对您有所帮助,别忘了查看我在 GitHub 上的演示代码。如有任何疑问,请在此留言。祝您编程愉快!
Twitter
https://www.twitter.com/timo_ernst
YouTube频道:
http://www.timoernst.tv
