我是初学者编码员。有没有人知道如何将缩略图图像链接到滑动器,以便单击缩略图将滑动器容器移动到相应的幻灯片?谢谢你的帮助!
示例:http://markdarren.com/F13/test.html
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="images/jacket2.png"></div>
<div class="swiper-slide"><img src="images/jacket3.png"></div>
<div class="swiper-slide"><img src="images/jacket4.png"></div>
<div class="swiper-slide"><img src="images/jacket5.png"></div>
</div>
</div>
<div class="thumb">
<div><a href="" title="onClick goto jacket 2"><img src="images/jacket2.png"></a></div>
<div><a href="" title="onClick goto jacket 3"><img src="images/jacket3.png"></a></div>
<div><a href="" title="onClick goto jacket 4"><img src="images/jacket4.png"></a></div>
<div><a href="" title="onClick goto jacket 5"><img src="images/jacket5.png"></a></div>
</div>
<script src="js/scroller/js/jquery-1.10.1.min.js"></script>
<script src="js/scroller/js/idangerous.swiper-2.0.min.js"></script>
<script>
var mySwiper = new Swiper('.swiper-container',{
pagination: '.pagination',
paginationClickable: true
})
</script>
答案 0 :(得分:8)
将脚本代码中的所有内容更改为:
$(document).ready(function() {
var mySwiper = new Swiper('.swiper-container',{
pagination: '.pagination',
paginationClickable: true
});
$(".thumb").on('click', 'div', function(){
mySwiper.swipeTo($(this).index(), 500);
});
});
我添加的位没有经过测试,但是应该意味着点击你的.thumbs位中的一个div(我忽略了你的标签)将刷到该div的索引(例如是div 1,div 2等。
答案 1 :(得分:0)
小修正:) - &gt;它实际上是“slideTo”而非“swipeTo”
$(document).ready(function() {
var mySwiper = new Swiper('.swiper-container',{
pagination: '.pagination',
paginationClickable: true
});
$(".thumb").on('click', 'div', function(){
mySwiper.slideTo($(this).index(), 500);
});
});
答案 2 :(得分:0)
我使用Swiper API编写了一个小插件,可以添加自定义缩略图并将它们链接到Swiper实例。
<强> JavaScript的:强>
$(document).ready(function(){
var mySwiper = new Swiper ('.swiper-container', {
direction: 'horizontal',
})
swiperThumbs(mySwiper, {
element: 'swiper-thumbnails',
activeClass: 'is-active'
});
});
<强> HTML 强>
<div class="swiper-thumbnails">
<button type="button">any html content</button>
<button type="button">any html content</button>
<button type="button">any html content</button>
</div>
答案 3 :(得分:-1)
这是解决此问题的方法
var galleryTop = new Swiper('.gallery-top', {
spaceBetween: 10,
loop:true,
loopedSlides: 5, //looped slides should be the same
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
var galleryThumbs = new Swiper('.gallery-thumbs', {
spaceBetween: 10,
slidesPerView: 4,
touchRatio: 0.2,
loop: true,
loopedSlides: 5, //looped slides should be the same
slideToClickedSlide: true,
});
galleryTop.controller.control = galleryThumbs;
galleryThumbs.controller.control = galleryTop;
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: 100%;
}
.gallery-thumbs {
height: 20%;
box-sizing: border-box;
padding: 10px 0;
}
.gallery-thumbs .swiper-slide {
height: 100%;
opacity: 0.4;
}
.gallery-thumbs .swiper-slide-active {
opacity: 1;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/css/swiper.min.css" rel="stylesheet"/>
<div class="swiper-container gallery-top">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></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(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
<div class="swiper-slide" style="background-image:url(https://picsum.photos/1200/1200/?random)"></div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/js/swiper.min.js"></script>