Junaid Qadir Shekhanzai
Junaid Qadir Shekhanzai

Reputation: 1423

How to attach "slide" & "slid" events to the Bootstrap toolkit's carousel?

Here's a fiddle with a working Bootstrap carousel. http://jsfiddle.net/jeykeu/5TDff/

And here's the official documentation which tells nothing about event usage. http://twitter.github.com/bootstrap/javascript.html#carousel

I thought this would work but no:

     $('#carousel').bind('slide',function(){
            alert("Slide Event");
    });

Upvotes: 22

Views: 52469

Answers (3)

Thumper
Thumper

Reputation: 9

Old post I know, but I wanted to show both slide and slid using the .on function.

So, my 2 cents... JSFiddle

$('#myCarousel').on('slide.bs.carousel', function (e) {
    $( '.carousel' ).removeClass('color-start');
    $( '.carousel' ).addClass('color-finish');
    $('#bind').html('Sliding!');
});

$('#myCarousel').on('slid.bs.carousel', function (e) {
    $( '.carousel' ).removeClass('color-finish');
    $( '.carousel' ).addClass('color-start');
    $('#bind').html('slid again!');
});

The 'addClass & removeClass' are for the fun of showing something is happening and when. Also, you could use this along with animate.css to add/remove the 'animated' class to elements .on(slide,).

$('#bind').html('Sliding!')

The above is I don't like 'alerts' and showing console.log can be a pain on smaller screens.

Upvotes: 0

Prashanth
Prashanth

Reputation: 954

For Bootstrap 3 implementations :

Before event

$('#myCarousel').bind('slide.bs.carousel', function (e) {
       console.log('before');
});

After event

$('#myCarousel').bind('slid.bs.carousel', function (e) {
       console.log('after');
});

Upvotes: 8

Gary.S
Gary.S

Reputation: 7131

Based on your fiddle #carousel is wrong. It should be #myCarousel.

Updated Example:

$('#myCarousel').carousel({
    interval: 2000
});

// Could be slid or slide (slide happens before animation, slid happens after)
$('#myCarousel').on('slid', function() {
    alert("Slide Event");
});​

http://jsfiddle.net/infiniteloops/wPF3n/

With Bootstrap 3

http://jsfiddle.net/infiniteloops/wPF3n/252/

Upvotes: 48

Related Questions