Swiper(http://www.idangero.us/swiper)是否提供了一个事件来滑动到具有特定类的幻灯片?有一个事件slideTo(索引),但我需要像slideTo(“。slide-classname”)这样的东西。
这就是我想要做的事情 - 也许可以采用不同的方法:
<script type="text/javascript">
$j(document).ready(function(){
$j("#attribute529").change(function() {
var optionValue = $j(this).val();
var optionValueText = $j.trim($j('#attribute529 :selected').text());
if( ! optionValue ){
galleryTop.slideTo(0);
} else {
galleryTop.slideTo(".product-image_" + optionValueText);
}
});
});
</script>
答案 0 :(得分:0)
我在下面创建了一个解决方案,在这里我创建了一个函数getSlideIndexByClass
,它将通过它的类名返回滑块中元素的索引,然后我就是使用该索引值调用slideTo
函数。
var swiper = new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
paginationClickable: true
});
swiper.slideTo(getSlideIndexByClass('my-class'));
function getSlideIndexByClass(className) {
var index = 0;
$.each($('.swiper-wrapper').children(), function(i, item) {
if ($(item).hasClass(className)) {
index = i;
return false;
}
});
return index;
}
&#13;
html,
body {
position: relative;
height: 100%;
}
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.swiper-container {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
&#13;
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/css/swiper.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/js/swiper.jquery.min.js"></script>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide my-class">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>
&#13;