仅当第一张幻灯片处于活动状态时才使按钮处于活动状态 - Swiper 滑块
Make button active only when first slide is active - Swiper slider
我需要一个仅在第一张幻灯片处于活动状态时才处于活动状态的按钮。我试过这段代码,但它不能正常工作——按钮总是被禁用。怎么了?
if ($("#aslide").attr("swiper-slide-active")) {
//
} else {
$("#acceptbtn").attr("disabled", "disabled");
}
单向:
使用Swiper API事件slideChange
(当当前活动的幻灯片被改变时会触发事件)-AND-
realIndex
(考虑到循环模式下的重复幻灯片, 当前活动幻灯片的索引号 )。
https://swiperjs.com/api/
/* swiper events */
swiper.on("slideChange", function() {
console.log("slide changed - current slide is: " + this.realIndex )
if(this.realIndex == 0){
console.log("first slide do something")
}
else{
console.log("not first slide do somehing")
}
});
disabled
按钮相关问题:
- Disable button in jQuery
基本片段
var swiper = new Swiper('.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
scrollbar: {
el: '.swiper-scrollbar',
draggable: true,
}
});
let button = document.getElementById("button")
/* swiper events */
swiper.on("slideChange", function() {
console.log("slide changed - current slide is: " + this.realIndex )
if(this.realIndex == 0){
console.log("first slide do something")
button.disabled = false;
}
else{
console.log("not first slide do somehing")
button.disabled = true;
}
});
html,
body {
position: relative;
height: 100%;
}
body {
background: #eee;
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: flex;
justify-content: center;
align-items: center;
}
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
<button id="button">Button</button>
<!-- Slider main container -->
<div class="swiper-container">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</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>
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
测试
$('#btn_asd2231').attr('disabled', true);
似乎对我有用,当我尝试禁用或启用按钮时。可能是代码中其他地方的错误。
我需要一个仅在第一张幻灯片处于活动状态时才处于活动状态的按钮。我试过这段代码,但它不能正常工作——按钮总是被禁用。怎么了?
if ($("#aslide").attr("swiper-slide-active")) {
//
} else {
$("#acceptbtn").attr("disabled", "disabled");
}
单向:
使用Swiper API事件slideChange
(当当前活动的幻灯片被改变时会触发事件)-AND-
realIndex
(考虑到循环模式下的重复幻灯片, 当前活动幻灯片的索引号 )。
https://swiperjs.com/api/
/* swiper events */
swiper.on("slideChange", function() {
console.log("slide changed - current slide is: " + this.realIndex )
if(this.realIndex == 0){
console.log("first slide do something")
}
else{
console.log("not first slide do somehing")
}
});
disabled
按钮相关问题:
- Disable button in jQuery
基本片段
var swiper = new Swiper('.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
scrollbar: {
el: '.swiper-scrollbar',
draggable: true,
}
});
let button = document.getElementById("button")
/* swiper events */
swiper.on("slideChange", function() {
console.log("slide changed - current slide is: " + this.realIndex )
if(this.realIndex == 0){
console.log("first slide do something")
button.disabled = false;
}
else{
console.log("not first slide do somehing")
button.disabled = true;
}
});
html,
body {
position: relative;
height: 100%;
}
body {
background: #eee;
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: flex;
justify-content: center;
align-items: center;
}
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
<button id="button">Button</button>
<!-- Slider main container -->
<div class="swiper-container">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</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>
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
测试
$('#btn_asd2231').attr('disabled', true);
似乎对我有用,当我尝试禁用或启用按钮时。可能是代码中其他地方的错误。