Skip to content

Instantly share code, notes, and snippets.

@elgsantos
Last active November 5, 2016 19:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elgsantos/5ff76405f52d33715ffa to your computer and use it in GitHub Desktop.
Save elgsantos/5ff76405f52d33715ffa to your computer and use it in GitHub Desktop.
A Pen by Eduardo Gomes.
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bootstrap 3 Carousel</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<body>
<div id="myCarousel" class="carousel slide myCarousel" data-interval="false" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item">
<!--put img inside itens like this-->
<!--<img src="" alt="...">-->
<div class="container">
<div class="carousel-caption">
<h3>First slide label</h3>
<p>It's Responsive and Pauses on hover</p>
</div>
</div>
</div>
<div class="item">
<div class="container">
<div class="carousel-caption">
<h3>Second slide label</h3>
<p>If you use controls, progress bar will change too.</p>
</div>
</div>
</div>
<div class="item">
<div class="container">
<div class="carousel-caption">
<h3>Third slide label</h3>
<p>Bug fixed</p>
</div>
</div>
</div>
</div>
<hr class="transition-timer-carousel-progress-bar" />
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<center>
<h2>Bootstrap 3 Carousel With Progress Bar</h2>
<h3>A pen by elgsantos</h3>
<h4>See on <a href=http://codepen.io/elgsantos/pen/doknj/>Codepen</a> or <a href=https://gist.github.com/elgsantos/5ff76405f52d33715ffa/>Github</a></h4>
</center>
</body>
</html>
$(document).ready(function(){
var percent = 0,
interval = 30,//it takes about 6s, interval=20 takes about 4s
$bar = $('.transition-timer-carousel-progress-bar'),
$crsl = $('#myCarousel');
$('.carousel-indicators li, .carousel-control').click(function (){$bar.css({width:0.5+'%'});});
/*line above just for showing when controls are clicked the bar goes to 0.5% to make more friendly,
if you want when clicked set bar empty, change on width:0.5 to width:0*/
$crsl.carousel({//initialize
interval: false,
pause: true
}).on('slide.bs.carousel', function (){percent = 0;});//This event fires immediately when the bootstrap slide instance method is invoked.
function progressBarCarousel() {
$bar.css({width:percent+'%'});
percent = percent +0.5;
if (percent>=100) {
percent=0;
$crsl.carousel('next');
}
}
var barInterval = setInterval(progressBarCarousel, interval);//set interval to progressBarCarousel function
if (!(/Mobi/.test(navigator.userAgent))) {//tests if it isn't mobile
$crsl.hover(function(){
clearInterval(barInterval);
},
function(){
barInterval = setInterval(progressBarCarousel, interval);
}
);
}
});
.carousel-inner .item{
background: #a7cfdf; /* Old browsers */
background: -moz-linear-gradient(top, #a7cfdf 0%, #23538a 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, #a7cfdf 0%,#23538a 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, #a7cfdf 0%,#23538a 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a7cfdf', endColorstr='#23538a',GradientType=0 ); /* IE6-9 */
text-align: center;
height: 200px;/*change as your needs*/
}
.control-buttons{
text-align:center;
}
.transition-timer-carousel-progress-bar {
height: 3px;
background-color: #5cb85c;/*progress bar color*/
width: 0;
margin: 0 0 0 0;
border: none;
z-index: 11;
position: relative;
}
.carousel-control{
z-index: 11;
}
@satrya-sabeni
Copy link

Everytime it loops the bar will go 10% then starts from to 0% to 100% again, is there a way to make that go away? Either way it's nice!

@JohJohan
Copy link

Hello @satrya-sabeni, i found a fix when the bar starts at 10% and then goes to 0% you should change:

if (percent>100) {
    percent=0;
    crsl.carousel('next');
}  

To

if (percent>100) {
    percent=-10;
    crsl.carousel('next');
}  

And .on('slid.bs.carousel', function () {percent=-10;});

to .{percent=-10;})

@elgsantos
Copy link
Author

Hello @JohJohan and @satrya-sabeni, I fixed it. Thanks for feedback!!
Also look it working on Codepen

@novakim
Copy link

novakim commented Jul 2, 2016

Hello, how can i stop the progress bar from pausing on hover?

@sonamtsutsatsang
Copy link

Hi,
Very nice. Small problem. The progress bar does not seem to work well on Internet Explorer. The progress bar starts from the middle and not from the left. Any chance of looking into it. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment