Swiper 滑块无法按预期使用循环工作:true 和 centeredSlides:false
Swiper slider not working as expected with loop: true and centeredSlides: false
当 loop 设置为 true 且 centeredSlides 设置为 false 时,Swiper 滑块无法正常工作。
单击缩略图或导航箭头时,主滑块不显示活动缩略图,我搜索但没有找到任何解决方案,如果有人知道如何解决它,我将不胜感激必须删除 none 当前选项。
这是JSFiddle
var galleryTop = new Swiper('.gallery-top', {
slidesPerView: 1,
loop: true,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
var galleryThumbs = new Swiper('.gallery-thumbs', {
direction: 'vertical',
slidesPerView: 4,
slideToClickedSlide: true,
spaceBetween: 10,
loop: true,
});
galleryTop.controller.control = galleryThumbs;
galleryThumbs.controller.control = galleryTop;
body {
padding: 20px;
}
.gallery-thumbs {
float: left;
width: calc(16% - 20px);
height: 280px;
margin-right: 20px;
}
.gallery-thumbs .swiper-slide {
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
cursor: pointer;
}
.gallery-thumbs .swiper-slide-active {
border: 1px solid grey;
}
.gallery-thumbs .swiper-slide:hover {
border: 1px solid grey;
}
.gallery-top {
float: left;
width: 84%;
height: 280px;
}
.gallery-top .swiper-slide {
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
border: 1px solid grey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/js/swiper.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css" rel="stylesheet"/>
<div class="swiper-container gallery-thumbs">
<div class="swiper-wrapper">
<div class="swiper-slide">1</div>
<div class="swiper-slide">2</div>
<div class="swiper-slide">3</div>
<div class="swiper-slide">4</div>
<div class="swiper-slide">5</div>
<div class="swiper-slide">6</div>
</div>
</div>
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide">1</div>
<div class="swiper-slide">2</div>
<div class="swiper-slide">3</div>
<div class="swiper-slide">4</div>
<div class="swiper-slide">5</div>
<div class="swiper-slide">6</div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
你可以这样做
HTML
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/1)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/2)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/3)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/4)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/5)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/6)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/7)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/8)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/9)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/10)"></div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next swiper-button-white"></div>
<div class="swiper-button-prev swiper-button-white"></div>
</div>
<div class="swiper-container gallery-thumbs">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/1)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/2)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/3)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/4)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/5)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/6)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/7)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/8)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/9)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/10)"></div>
</div>
</div>
CSS
html, body {
position: relative;
height: 100%;
}
body {
background: #000;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.swiper-container {
width: 100%;
height: 300px;
margin-left: auto;
margin-right: auto;
}
.swiper-slide {
background-size: cover;
background-position: center;
}
.gallery-top {
height: 80%;
width: 70%;
margin: 0;
margin-left: auto;
}
.gallery-thumbs {
//height: 20%;
box-sizing: border-box;
padding: 10px 0;
}
.gallery-thumbs.swiper-container {
padding: 10px;
margin: 0px;
}
.gallery-thumbs .swiper-slide {
height: 30%;
opacity: 0.4;
}
.gallery-thumbs .swiper-slide-active {
opacity: 1;
border: 2px solid #ffa303;
}
.gallery-top{
float:right;
width:80%;
}
.gallery-thumbs{
float:left;
width:20%;
height:80%;
}
JS
var galleryTop = new Swiper(".gallery-top", {
nextButton: ".swiper-button-next",
prevButton: ".swiper-button-prev",
spaceBetween: 10,
loop:true,
loopedSlides: 50
});
var galleryThumbs = new Swiper(".gallery-thumbs", {
spaceBetween: 10,
slidesPerView: "auto",
touchRatio: 0.2,
loop:true,
slideToClickedSlide: true,
loopedSlides: 50,
direction:'vertical'
});
galleryTop.params.control = galleryThumbs;
galleryThumbs.params.control = galleryTop;
试一试。
尝试删除 spaceBetween: 10,
并将其更改为包含在幻灯片中 padding: 0 10px;
这将有助于 swiper 进行幻灯片偏移计算。
当 loop 设置为 true 且 centeredSlides 设置为 false 时,Swiper 滑块无法正常工作。
单击缩略图或导航箭头时,主滑块不显示活动缩略图,我搜索但没有找到任何解决方案,如果有人知道如何解决它,我将不胜感激必须删除 none 当前选项。
这是JSFiddle
var galleryTop = new Swiper('.gallery-top', {
slidesPerView: 1,
loop: true,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
var galleryThumbs = new Swiper('.gallery-thumbs', {
direction: 'vertical',
slidesPerView: 4,
slideToClickedSlide: true,
spaceBetween: 10,
loop: true,
});
galleryTop.controller.control = galleryThumbs;
galleryThumbs.controller.control = galleryTop;
body {
padding: 20px;
}
.gallery-thumbs {
float: left;
width: calc(16% - 20px);
height: 280px;
margin-right: 20px;
}
.gallery-thumbs .swiper-slide {
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
cursor: pointer;
}
.gallery-thumbs .swiper-slide-active {
border: 1px solid grey;
}
.gallery-thumbs .swiper-slide:hover {
border: 1px solid grey;
}
.gallery-top {
float: left;
width: 84%;
height: 280px;
}
.gallery-top .swiper-slide {
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
border: 1px solid grey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/js/swiper.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css" rel="stylesheet"/>
<div class="swiper-container gallery-thumbs">
<div class="swiper-wrapper">
<div class="swiper-slide">1</div>
<div class="swiper-slide">2</div>
<div class="swiper-slide">3</div>
<div class="swiper-slide">4</div>
<div class="swiper-slide">5</div>
<div class="swiper-slide">6</div>
</div>
</div>
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide">1</div>
<div class="swiper-slide">2</div>
<div class="swiper-slide">3</div>
<div class="swiper-slide">4</div>
<div class="swiper-slide">5</div>
<div class="swiper-slide">6</div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
你可以这样做
HTML
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/1)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/2)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/3)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/4)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/5)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/6)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/7)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/8)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/9)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/10)"></div>
</div>
<!-- Add Arrows -->
<div class="swiper-button-next swiper-button-white"></div>
<div class="swiper-button-prev swiper-button-white"></div>
</div>
<div class="swiper-container gallery-thumbs">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/1)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/2)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/3)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/4)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/5)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/6)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/7)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/8)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/9)"></div>
<div class="swiper-slide" style="background-image:url(http://lorempixel.com/1200/1200/nature/10)"></div>
</div>
</div>
CSS
html, body {
position: relative;
height: 100%;
}
body {
background: #000;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.swiper-container {
width: 100%;
height: 300px;
margin-left: auto;
margin-right: auto;
}
.swiper-slide {
background-size: cover;
background-position: center;
}
.gallery-top {
height: 80%;
width: 70%;
margin: 0;
margin-left: auto;
}
.gallery-thumbs {
//height: 20%;
box-sizing: border-box;
padding: 10px 0;
}
.gallery-thumbs.swiper-container {
padding: 10px;
margin: 0px;
}
.gallery-thumbs .swiper-slide {
height: 30%;
opacity: 0.4;
}
.gallery-thumbs .swiper-slide-active {
opacity: 1;
border: 2px solid #ffa303;
}
.gallery-top{
float:right;
width:80%;
}
.gallery-thumbs{
float:left;
width:20%;
height:80%;
}
JS
var galleryTop = new Swiper(".gallery-top", {
nextButton: ".swiper-button-next",
prevButton: ".swiper-button-prev",
spaceBetween: 10,
loop:true,
loopedSlides: 50
});
var galleryThumbs = new Swiper(".gallery-thumbs", {
spaceBetween: 10,
slidesPerView: "auto",
touchRatio: 0.2,
loop:true,
slideToClickedSlide: true,
loopedSlides: 50,
direction:'vertical'
});
galleryTop.params.control = galleryThumbs;
galleryThumbs.params.control = galleryTop;
试一试。
尝试删除 spaceBetween: 10,
并将其更改为包含在幻灯片中 padding: 0 10px;
这将有助于 swiper 进行幻灯片偏移计算。