Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the index of the current slide, when the slide is changed?

I have the following

mySwiper = new Swiper('.my-swiper', {
    direction: 'vertical',
    loop: true,
});
mySwiper.on('slideChange', function () {
    console.log('*** mySwiper.realIndex', mySwiper.realIndex);
});

But realIndex always returns the previous index. So if I'm on the first slide (0) and go to the next, realIndex is 0 when it should be 1. When I go back to the first slide realIndex is 1 when it should be 0. So it seems the event fires before the index is updated. I've tried other events but haven't had any luck. Is there an event that fires after the slide is changed so that I can capture the current slide index?

like image 674
Bafsky Avatar asked May 09 '18 02:05

Bafsky


People also ask

What is the difference between slideid and slideindex?

Unlike the SlideID property, the SlideIndex property of a Slide object can change when you add slides to the presentation or rearrange the slides in the presentation.

How to use findbyslideid method (PowerPoint)?

Slides.FindBySlideID method (PowerPoint) 1 Parameters. Specifies the ID number of the slide you want to return. ... 2 Return value 3 Remarks. Unlike the SlideIndex property, the SlideID property of a Slide object won't change when you add slides to the presentation or rearrange the slides in the presentation. 4 Example. ... 5 See also. ...

How to get a specific Slide Object from a slide collection?

Therefore, using the FindBySlideID method with the slide's ID number can be a more reliable way to return a specific Slide object from a Slides collection than using the Item method with the slide's index number. This example displays the index number of the currently displayed slide in slide show window one.

How do I find the ID of a slide in PowerPoint?

Slides.FindBySlideID method (PowerPoint) Returns a Slide object that represents the slide with the specified slide ID number. Each slide is automatically assigned a unique slide ID number when it is created. Use the SlideID property to return a slide's ID number. Syntax. expression.


2 Answers

You should use transitionEnd event as Event will be fired after the transition.

see a demo below

var mySwiper = new Swiper('.swiper-container', {
  direction: 'vertical',
  loop: true,
});
mySwiper.on('transitionEnd', function() {
  console.log('*** mySwiper.realIndex', mySwiper.realIndex);
});
.swiper-container {
  width: 100%;
  height: 100%;
}

.as-console-wrapper {
  z-index: 999999 !important;
}

.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;
}
<!-- Link Swiper's CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.6/css/swiper.min.css">
<!-- Swiper -->
<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">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>
</div>

<!-- Swiper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.6/js/swiper.min.js"></script>

Note: If you are not able to see the console.log statement output in the snippet, use F12 to see the index of the slide.

like image 196
Muhammad Omer Aslam Avatar answered Sep 20 '22 22:09

Muhammad Omer Aslam


mySwiper.on('slideChange', function (e) {
    console.log('*** mySwiper.activeIndex', mySwiper.activeIndex);
});
like image 29
Muhammad Faizan Qureshi Avatar answered Sep 18 '22 22:09

Muhammad Faizan Qureshi