I am changing image source on click (prev/next buttons), and I would like to fade out image, then change source and fade it in, but I cannot get smooth transition. I tried adding .delay()
in between fading and changing the source but still the src
change happens before fading ends. I tried adding .stop()
but that only stops the currently running animation, and attribute change doesn't count as animation.
var images = ['http://i.imgur.com/U82gG8H.jpg', 'http://i.imgur.com/kVy4G4R.jpg', 'http://i.imgur.com/BtMikrd.jpg'];
var count = 0;
$('.next').on('click', function() {
if (count !== images.length-1) {
count++;
$('.product_image img').fadeTo(300, 0).delay(600).attr('src', '').attr('src', images[count]).fadeTo(300, 1);
}
});
$('.previous').on('click', function() {
if (count !== 0) {
count--;
$('.product_image img').fadeTo(300, 0).delay(400).attr('src', '').attr('src', images[count]).fadeTo(300, 1);
}
});
.navigation {
display: block;
}
.navigation .previous,
.navigation .next {
width: 30px;
height: 30px;
line-height: 30px;
text-align: center;
background: #ddd;
color: #fff;
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.navigation .next {
margin-right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="product_image">
<img src="http://i.imgur.com/U82gG8H.jpg" />
</div>
<div class="navigation">
<div class="previous"><</div>
<div class="next">></div>
</div>
Any help is appreciated.