Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include from and to indexes in Carousel slide event #2404

Closed
wants to merge 2 commits into from
Closed

Include from and to indexes in Carousel slide event #2404

wants to merge 2 commits into from

Conversation

jimmydo
Copy link

@jimmydo jimmydo commented Mar 4, 2012

The slide event callback will now receive the indexes of the items that the carousel is transitioning from and to as the second and third arguments, respectively.

Example:

someCarousel.bind('slide', function (event, fromIndex, toIndex) {
...
});

I chose to pass the indexes instead of the actual DOM elements, to be consistent with the .carousel(number) function, which uses a zero-based index to the desired carousel item.

This should also address issue #2240.

@ibrent
Copy link

ibrent commented Mar 18, 2012

Is there a way to retrieve the current slide number - the position of the one which is being displayed?
Something like $('#myCarousel').currentSlide;

Thx !

@fat
Copy link
Member

fat commented Mar 20, 2012

@Librent there's no internal state for slide position in the carousel - however

$('#myCarousel .active').index('#myCarousel .item')

would be the current active slide number.

@fat
Copy link
Member

fat commented Mar 20, 2012

Not crazy about adding extra response args with these events. We've been trying to keep events very close to native event expectations - only using real event property names - and only returning a single event argument. (this is why we've used keycode and relatedTarget in other places.)

@fat fat closed this Mar 20, 2012
@ibrent
Copy link

ibrent commented Mar 20, 2012

Perfect. This will work great. Thank you.

On Mar 19, 2012, at 10:01 PM, Jacob Thornton reply@reply.github.com wrote:

@Librent there's no internal state for slide position in the carousel - however

$('#myCarousel .active').index('#myCarousel .item')` 

would be the current active slide number.


Reply to this email directly or view it on GitHub:
#2404 (comment)

@cambridgemike
Copy link

Is there a guarantee that fetching $("#myCarousel .active") inside the "slid" event, will indeed return the slide that was just.. slid to?

For instance
<div id="myCarousel"> <div class="item" id="A"></div> <div class="item" id="B"></div> </div>

 $("#myCarousel").carousel()
 $("#myCarousel").bind("slid", function(){
    alert( $("#myCarousel .active").attr('id') )
  })

Will alert "B" after the transition from A->B?

@dardub
Copy link

dardub commented Jan 22, 2013

The example given:

$('#myCarousel .active').index('#myCarousel .item')

Will only return the from slide index. How do we return the to slide index?

@howdu
Copy link

howdu commented Apr 1, 2013

This works for me $(".active", e.target).index()

I was able to make the slider remember the last visible slide, then show the next slide on next load

$(".carousel").on('slid', function(e){  
    localStorage.setItem('last_slide', 
        // join active slide index with carousel ID 
        $(".active", e.target).index() + "#" + e.target.id
    );
});

var lastSlide = localStorage.getItem('last_slide');    
if (lastSlide) {
    lastSlide = lastSlide.split("#");
    $("#"+lastSlide[1]).carousel(parseInt(lastSlide[0], 10)+1);
}

@furf
Copy link

furf commented Aug 8, 2013

As of 3.0.0-rc1, the following works to access active element, next element, from index, to index, and slide direction. Feel free to decrease specificity of your selectors as needed.

carousel.on('slide.bs.carousel', function (event) {

  var active = $(event.target).find('.carousel-inner > .item.active');
  var from = active.index();
  var next = $(event.relatedTarget);
  var to = next.index();
  var direction = event.direction;

});

@andrezimpel
Copy link

$('.gallery-carousel').data("bs.carousel").getActiveIndex() works for me in 3.0.2 :)

@charl-kruger
Copy link

furf's example works best for me, thanks

@twbs twbs locked and limited conversation to collaborators Aug 7, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants