34

Twitter Bootstrap Version: 2.0.3

Example HTML code:

<!DOCTYPE html> <html dir="ltr" lang="en-US" xmlns:og="http://opengraphprotocol.org/schema/"> <head> <link rel="stylesheet" type="text/css" media="all" href="reddlec/style.css" /> <script type="text/javascript"> $(document).ready(function() { $('.carousel').each(function(){ $(this).carousel({ pause: true, interval: false }); }); });​ </script> <script src="http://twitter.github.com/bootstrap/assets/js/jquery.js"></script> <script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-transition.js"></script> <script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-carousel.js"></script> </head> <body> <div id="myCarousel" class="carousel slide"> <!-- Carousel items --> <div class="carousel-inner"> <div class="active item">…</div> <div class="item">…</div> <div class="item">…</div> </div> <!-- Carousel nav --> <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a> </div> </body> </html> 

CSS: Provided with bootstrap.css

Problem: As you can see, I've enabled paused mode, and disabled cycling. But when I click the carousel's left or right controls, the carousel starts cycling at the default interval (5 seconds per cycle) on mouseleave.

How do I forcefully disable cycling? I want the images to be cycled manually by the user.

You can test the problem live on my test site here.

11 Answers 11

42

You might want to try removing the "pause" attribute. It's not necessary if you are not automatically cycling through the images. My assumption (and I'm going to look at the bootstrap code to verify) is that when the "mouseleave" event fires, the cycling resumes -- even if it was turned off in the first place. When it resumes, it uses the default speed.

Here is a solution:

$(function() { $('.carousel').each(function(){ $(this).carousel({ interval: false }); }); });​ 
Sign up to request clarification or add additional context in comments.

7 Comments

Your other answer worked like a charm, I didn't realize what I was missing there. This one though isn't working as intented, despite removing pause: true, like you said.
Hmmm. So I set up a fiddle here: jsfiddle.net/aB7c6 , and it seems to be working as expected. Even after clicking and leaving the controls multiple times, auto-cycling does not resume. Is there something else in your code that may be affecting this?
You can check mine live here.
Ah ha - there is an invisible illegal character in your $(document). ready... javascript, just after the very last semi-colon. Some copy-paste cruft, or whatever. Kill that, then try it again.
I see nothing, no space, no character nothing. I even hand-typed out all that JavaScript code. The problem's still there. I've pasted my document on pastebin. It would be great if you can actually mark it in a screenshot or let me know what it actually is or looks like. Thanks.
|
39

To disable cycling, one working solution is to add data-interval="false" to the carousel container:

<div id="myCarousel" class="carousel slide" data-interval="false"> <div class="carousel-inner"> <div class="active item">toto</div> <div class="item">tata</div> <div class="item">tutu</div> </div> <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a> </div> 

2 Comments

Just to highlight to anyone else the NB part of this is data-interval="false" and it works on BS 2.3.2, no JS needed.
If you're testing this in-browser, note this data attribute needs to be set on page load.
29

I just came up with a solution for this issue. None of the above worked for me, but it did get me to look at the bootstrap code. I believe the reason for this bug is in the bootstrap-carousel.js file in the defaults object:

 $.fn.carousel.defaults = { interval: 5000 , pause: 'hover' } 

After the carousel slides, it ends up reverting to these default values. If you want the carousel to not slide and only be controlled by the user, you can change the defaults object to have an interval of false. Hope this helps.

 $.fn.carousel.defaults = { interval: false , pause: 'hover' } 

Comments

10
<div id="carousel-example-generic" class="carousel slide" data-wrap="false"> 

http://getbootstrap.com/javascript/#carousel

1 Comment

data-wrap="false", data-interval="false", data-pause="true", etc.. that's the way I was looking for
9

I'm not sure why but the main solution didn't work for me either. Weird.

To fix my problem I did the following code.

$('.carousel').carousel({interval: false}); $(document).on('mouseleave', '.carousel', function() { $(this).carousel('pause'); }); 

See the documentation for carouselpause.

Comments

3

Apart from what @dgabriel suggests, do make sure that the JavaScript call that initializes Bootstrap carousel is AFTER the <script> references to jQuery, bootstrap-transition.js, and bootstrap-carousel.js, pretty much like this:

<script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/jquery.js"></script> <script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/bootstrap-transition.js"></script> <script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/bootstrap-carousel.js"></script> <script type="text/javascript"> $(document).ready(function() { $('.gallery-carousel').each(function(){ $(this).carousel({ interval: false }); }); }); </script> 

This makes sure that the carousel works as intended (errors like Uncaught ReferenceError: $ is not defined (anonymous function) for example.

Comments

1

I tried everything, but nothing worked. I solved the issue in changing files bootstrap.min.js & bootstrap.js. You have to looking for "carousel.defaults" with Ctrl+F in these files and replace what it contains by:

interval:false 

Comments

1

I think @dgabriel's answer should work, because :

bootstrap carousel's pause() method doesn't treat its argument the way you think it does. pause(true) doesn't mean "yes, pause it", nor does pause(false) mean "no, don't pause it".

it can be seen in the source code,

Carousel.prototype.pause = function (e) { e || (this.paused = true) ... this.interval = clearInterval(this.interval) return this } 

github link here

as can be seen, if e is true, this.paused = true won't execute. so when event "mouseleave" fires, cycle() method still see "paused == false" and setInterval is executed again, so your carousel won't stay still.

// mouseleave event fires using cycle() with no arguments .on('mouseleave', $.proxy(this.cycle, this)) // so when cycle() is called, this.paused == false. Carousel.prototype.cycle = function (e) { e || (this.paused = false) this.interval && clearInterval(this.interval) // set interval == false to prevent setInterval this.options.interval && !this.paused && (this.interval = setInterval($.proxy(this.next, this), this.options.interval)) return this } 

to pause, use .pause() and interval=false to prevent "mouseleave" event to recycle. bootstrap itself uses it this way, it can be seen here

Comments

1

Actually, changing the defaults in bootstrap-carousel.js as follows helped me.

Carousel.DEFAULTS = { interval: false, pause: 'hover', wrap: false } 

Comments

0

I have some problem too doing Stop bootstrap Carousel from auto playing. I checked bootstrap.js file and I have found codes related to carousel then I remove the another js. In new js file I make copy from all codes and then I change Carousel variable to Carousel_n(because it's possible to change it to anything you want). In last line of codes, the most important thing to do is:

 { $('[data-ride="carousel"]').each(function () } 

I change carousel to carousel_n.

for html I only changed:

<div id="Carousel" class="carousel slide" data-ride="carousel"> 

to

<div id="Carousel" class="carousel slide" data-ride="carousel_n"> 

and ofcourse it really worked.

Comments

-1
$('your-carousel').carousel({ pause: true, interval: false }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.