question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need a way to get the accurate activeIndex in loop mode

See original GitHub issue

I’m using Swiper for a user to set their ‘current active image’, and so hooking into the onSlideChangeEnd() callback.

As you know, when in loop mode, the activeIndex property is offset by the duplicated slides, although what I need is essentially the data-swiper-slide-index attribute of the currently selected slide. I can’t find a way to get the currently selected slide or it’s index.

I looked repeatedly for this value in the API, although I can’t find anything, it seems like it should be there somewhere. The only other way I can think to get this value, would be to query the DOM for the slide with the active class on it, and grab it’s data-swiper-slide-index value, although that’s a pretty messy way to get a simple value.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
navyxiecommented, Apr 27, 2016

in loop mode active index value will be always shifted on a number of looped/duplicated slides. you can get attribute ‘data-swiper-slide-index’ whit function like:

function geSlideDataIndex(swipe){
    var activeIndex = swipe.activeIndex;
    var slidesLen = swipe.slides.length;
    if(swipe.params.loop){
        switch(swipe.activeIndex){
            case 0:
                activeIndex = slidesLen-3;
                break;
            case slidesLen-1:
                activeIndex = 0;
                break;
            default:
                --activeIndex;
        }
    }
    return  activeIndex;
}

usage:

var mySwiper = new Swiper('.swiper-container', {
    direction: 'vertical',
    loop:true,
    onSlideChangeEnd:function(swipe){
        console.log(geSlideDataIndex(swipe))
    }
}); 

@phongmedia

0reactions
lock[bot]commented, Jun 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

iDangero.us Swiper slide count when loop is true
activeIndex property and mySwiper.slides but the problem is that when loop is true they don't give correct index and count. Is there any...
Read more >
swiper get active index | The AI Search Engine You Control
How do I get the previously active index of a slide using Swiper.js? ... nolimits4web/swiperNeed a way to get the accurate activeIndex in...
Read more >
How to Fix iPhone That Is Stuck in Recovery Mode Loop (No ...
This is a short story about how a girl fixed her iPhone that is stuck in a Recovery Mode loop. Free Download for...
Read more >
When class is toggled on `html` change Swiper JS settings
I have a Swiper JS instance which works as expected. ... loop (need to figure out how to do autoplay) and changing the...
Read more >
Swiper API
Now, when we have Swiper's HTML, we need to initialize it using the following ... with loop mode you should tell to Swiper...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found