【发布时间】:2020-12-08 15:47:37
【问题描述】:
我想在视图中显示第二张幻灯片的一部分。大约 20-30%。 我已将 slidesPerView 设置为 2.1(在移动设备上!)以显示下一张幻灯片的一部分。 但是,当滚动幻灯片时,它不会捕捉到容器的开头,这会导致在滚动滑动条时部分幻灯片不在视图中。
我尝试将幻灯片的高度设置为 70%,但这会产生同样的问题。我也一直在摆弄 slidesOffsetAfter 参数,但没有运气。
- 幻灯片放映快照以显示整个图像的容器的开始
- 下一张幻灯片应该部分可见
查看项目当前状态:live dev environment
您可以在桌面上的水平滑动器和垂直滑动器的平板电脑视图中看到问题。
CSS
<!-- swiper CSS -->
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
<style>
@media screen and (max-width: 1079px){
.swiper-section {
justify-content: flex-start;
margin-top: 194px;
}
}
@media screen and (max-width: 1079px){
.main-navigation {
left: 45px;
top: 45px;
}
}
@media screen and (max-width: 1079px){
.sub-navigation {
right: 45px;
top: 45px;
}
}
@media screen and (max-width: 1079px){
.swiper-container {
height: 965px;
}
}
.swiper-slide {
padding-bottom: 45px;
}
@media screen and (max-width: 1079px) {
.swiper-slide {
padding-bottom: 0px;
}
}
.swiper-slide:nth-child(even) {
height: 75%;
width: 45vw !important;
}
.swiper-slide:nth-child(odd) {
height: 100%;
width: 55vw !important;
}
@media screen and (max-width: 1079px) {
.swiper-div {
margin: 0px 45px 0px 45px;
}
}
@media screen and (max-width: 1079px) {
.swiper-slide:nth-child(even) {
height: 100%!important;
width: 100% !important;
}
}
@media screen and (max-width: 1079px) {
.swiper-slide:nth-child(odd) {
height: 100%!important;
width: 100% !important;
}
}
.swiper-slide>img {
object-fit: cover;
width: 100%;
height: 100%;
}
.swiper-button-prev,
.swiper-button-next {
display: none;
}
.swiper-container-horizontal>.swiper-pagination-progressbar {
width: 175px;
margin-left: 65px;
border-radius: 50px;
}
.swiper-pagination-progressbar {
margin-top: 733px;
background: #bababa;
}
.swiper-pagination-progressbar .swiper-pagination-progressbar-fill {
background: #888888;
}
.swiper-container-horizontal>.swiper-pagination-progressbar {
height: 12px;
}
.swiper-container-vertical>.swiper-pagination-progressbar {
display: none;
}
</style>
JS
<!-- swiper JS -->
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
<!-- Swiper - Extra Step - add arrows and pagination html markup by code (Append) -->
<script>
var swiperNodes = "";
var pagination = '<div class=swiper-pagination></div>';
var next_prev_buttons = '<div class="swiper-button-prev"></div><div class="swiper-button-next"></div>';
var scrollbar = '<div class="swiper-scrollbar"></div>';
var swiperNodes = swiperNodes.concat(pagination, next_prev_buttons);
/* loop throw all swipers on the page */
$('.swiper-container').each(function( index ) {
$( this ).append(swiperNodes);
});
</script>
<!-- swiper JS Initialize -->
<script>
var mySwiper = new Swiper ('.swiper-container', {
// Optional parameters
slidesPerView: 1,
spaceBetween: 30,
freeMode: false,
loop: true,
centeredSlides: false,
// Enable lazy loading
lazy: true,
mousewheel: {
invert: true,
},
keyboard: {
enabled: true,
onlyInViewport: false,
},
scrollbar: {
el: '.swiper-scrollbar',
draggable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
pagination: {
el: '.swiper-pagination',
type: 'progressbar',
},
keyboard: {
enabled: true,
},
breakpoints: {
0: { /* when window >=0px - webflow mobile landscape/portriat */
slidesPerView: 1,
spaceBetween: 10,
slidesOffsetBefore: 0,
direction: 'vertical',
},
767: { /* when window >= 767px - webflow tablet */
slidesPerView: 2.1,
spaceBetween: 30,
slidesOffsetBefore: 0,
direction: 'vertical',
},
1279: { /* when window >= 988px - webflow desktop */
slidesPerView: 2,
spaceBetween: 20,
slidesOffsetBefore: 0,
}
},
/* uncomment if you want autoplay slider
autoplay: {
delay: 3000,
},
*/
/* uncomment if you want scrollbar
scrollbar: {
el: '.swiper-scrollbar',
hide: true,
},
*/
})
</script>
【问题讨论】:
-
按照这个例子(例如将宽度设置为自动并滑动到 80% - 不要使用 2.1):swiperjs.com/demos/#auto_slides_per_view_carousel_mode
标签: javascript swiper swiper.js