Attempting to create my own slider using jQuery, here's a working example:
$(document).ready(function(){
$(".carousel-indicators li").click(function(){
$(".carousel-indicators li").css("background-color", "transparent");
$(this).css("background-color", "yellow");
});
var active1, prev1, next1;
function rotateimages(){
active1 = $(".item.active");
if(active1.prev().length == 0) {
prev1 = $(".item").last();
prev1.removeClass("mytrans");
prev1.addClass("prev");
}
else {
prev1 = active1.prev();
prev1.removeClass("mytrans");
prev1.addClass("prev");
}
if(active1.next().length == 0) {
next1 = $(".item").first();
next1.removeClass("mytrans");
next1.addClass("next");
//alert();
}
else {
next1 = active1.next();
next1.removeClass("mytrans");
next1.addClass("next");
//alert();
}
active1.removeClass("active").addClass("prev mytrans");
next1.removeClass("next").addClass("mytrans active");
prev1.removeClass("prev mytrans");
}
setInterval(rotateimages, 3000);
});
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
width: 70%;
margin: auto;
}
html, body {
width: 100%;
height: 100%;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
list-style-type: none;
}
.container {
width: 768px;
margin: auto;
padding: 0 15px;
height: 100%;
}
#myCarousel {
width: 100%;
background-color: lightgray;
position: relative;
height: 100%;
}
.carousel-indicators {
position: absolute;
bottom: 20px;
left: 45%;
}
.carousel-indicators li {
list-style-type: circle;
display: inline-block;
width: 10px;
height: 10px;
border: 1px solid red;
border-radius: 10px;
cursor: pointer;
}
.mytrans {
transition: all 1s;
}
.carousel-inner .item {
position: absolute;
display: none;
}
.carousel-inner .item.prev {
left: -100%;
top: 0;
display: block;
}
.carousel-inner .item.next {
left: 100%;
top: 0;
display: block;
}
.carousel-inner .item.active {
left: 0%;
top: 0;
display: block;
}
.carousel-inner .left {
position: absolute;
left: 10%;
top: 50%;
}
.carousel-inner .right {
position: absolute;
right: 10%;
top: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<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>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="http://madaboutwords.com/wp-content/uploads/2010/05/one.png" alt="Chania" width="460" height="345">
<div class="carousel-caption">
<h3>Chania</h3>
<p>The atmosphere in Chania has a touch of Florence and Venice.</p>
</div>
</div>
<div class="item">
<img src="http://digimind.com/blog/wp-content/uploads/2012/02/number2c.png" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>
<div class="item">
<img src="http://www.hotel-r.net/im/hotel/gb/number-three-22.png" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>
<div class="item">
<img src="http://www.hotel-r.net/im/hotel/gb/number-four-10.gif" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>
</div>
<!-- Left and right controls -->
<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>
</div>
The issue appears to be related to the fast addition and removal of the next
class. Here's the problematic snippet:
else {
next1 = active1.next();
next1.removeClass("mytrans");
next1.addClass("next");
//alert();
}
active1.removeClass("active").addClass("prev mytrans");
next1.removeClass("next").addClass("mytrans active");
prev1.removeClass("prev mytrans");
Question: How can I ensure that the styles of the next
class are applied before it is removed by the removeClass function?