在我正在编写的网站中,我有一个带有“.swiper-slide”类的 div。然后,当我完成表单时,将生成几个带有“.swiper-slide”类的 div。问题是 swiper 将我的第一张幻灯片识别为最后一张,并使用“.swiper-slide-next”禁用按钮。有没有办法防止 swiper 禁用它?.
我写了这段代码并且它有效。我想知道是否存在更干净的方法来做到这一点。
if(swiper.isEnd){
$("#prosegui").removeClass("swiper-button-disabled");
$("#prosegui").removeClass("swiper-button-lock");
$("#prosegui").removeAttr("disabled");
$("#prosegui").removeAttr("tabindex");
$("#prosegui").removeAttr("aria-disabled");
$("#prosegui").removeAttr("aria-controls");
}
其中“#prosegui”是下一张幻灯片按钮。
谢谢大家。
这个例子正在运行。最后你到底想做什么?
在最后一张幻灯片中,我的代码将为您提供一个 console.log 并识别出已到达最后一张幻灯片。
如果您喜欢使用代码,请参阅我为您制作的小提琴:https://jsfiddle.net/bogatyr77/rux9jekt/2/
const swiper = new Swiper('.swiper', {
// Optional parameters
direction: 'horizontal',
loop: true,
// If we need pagination
pagination: {
el: '.swiper-pagination',
},
// Navigation arrows
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// And if we need scrollbar
scrollbar: {
el: '.swiper-scrollbar',
}
});
swiper.on('reachEnd', function(){
console.log("reach to End");
});
.swiper {
width: 600px;
height: 300px;
}
<script src="https://unpkg.com/swiper@7/swiper-bundle.min.js"></script>
<link href="https://unpkg.com/swiper@7/swiper-bundle.min.css" rel="stylesheet"/>
<!-- Slider main container -->
<div class="swiper">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide"><img src="https://via.placeholder.com/600">
</div>
<div class="swiper-slide"><img src="https://via.placeholder.com/600"></div>
<div class="swiper-slide"><img src="https://via.placeholder.com/600"></div>
...
</div>
<!-- If we need pagination -->
<div class="swiper-pagination"></div>
<!-- If we need navigation buttons -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<!-- If we need scrollbar -->
<div class="swiper-scrollbar"></div>
</div>
当我第一次按下一个按钮时,我也遇到问题,所以为了避免按钮被禁用,我像这样使用slideNextTransitionEnd
在视野中
<div class="swiper-button-next" id="slide-next-button"></div>
然后在脚本中
const swiper = new Swiper('#my-swipper-slide', {
direction: 'horizontal',
pagination: {
el:'.swiper-pagination',
},
navigation: {
nextEl:'.swiper-button-next',
prevEl:'.swiper-button-prev',
},
allowSlidePrev: true,
allowSlideNext: true,
loop:true
});
swiper.on('slideNextTransitionEnd',function(){
if($("#slide-next-button").hasClass("swiper-button-disabled")){
$("#slide-next-button").removeClass("swiper-button-disabled");
$("#slide-next-button").attr("aria-disabled", "false");
}
})