1

I am trying to make a feature on a swiper slide so when the second slide on the slider has the class name "swiper-slide-active" it alerts the user they are on the second slide

Here is the codepen to my problem https://codepen.io/mrsalami/pen/rEWNzN

$(document).ready(function() {
  if ($("div.swiper-wrapper > div.swiper-slide.first-child").hasClass("landscape")) {
    $(".swiper-wrapper").addClass('landscape-slider');
  }
  if( $('div.swiper-wrapper > div.swiper-slide.second-child').hasClass('swiper-slide-active')) {
    alert('active');
}
});
yunzen
  • 32,854
  • 11
  • 73
  • 106
Jafar Salami
  • 321
  • 4
  • 18

2 Answers2

3

you need to call your alert code on slide event of swiper not on ready fucntion

e.g

swiper.on('slideChange', function () {

  if( $('div.swiper-wrapper > div.swiper-slide.second-child').hasClass('swiper-slide- 
  active')) {

          alert('active');
     }

  });

see here for more events

Satish Patil
  • 438
  • 5
  • 15
1

What you want to achieve is probably: If the .second-child is active, then alert

So you need something alongside this

// Event will be triggered after animation to other slide (next or previous).
// @see https://idangero.us/swiper/api/#events
swiper.on('slideChangeTransitionEnd', function() {  
  // check, if active slide has a class of 'second-child'
  if (this.slides[this.realIndex].classList.contains("second-child")) {
    alert("active");
  }
})

or

swiper.on('slideChangeTransitionEnd', function() {  
  if (this.realIndex == 1) { // second slide, because of zero indexed counting
    alert("active");
  }
})

No jQuery involved here

yunzen
  • 32,854
  • 11
  • 73
  • 106