How to create jQuery fadeIn fadeOut Animation
Html:
<ul>
<li class="client-testimonial"><img src="demo1.png"></li>
<li class="client-testimonial"><img src="demo2.png"></li>
<li class="client-testimonial"><img src="demo3.png"></li>
<li class="client-testimonial"><img src="demo4.png"></li>
</ul>
jQuery:
jQuery(document).ready(function(){
if(jQuery('.client-testimonial').length > 0){
var showImg = jQuery(".client-testimonial-img");
var quoteIndex = -1;
function showFadeInFadeOut() {
++quoteIndex;
showImg.eq(quoteIndex % showImg.length)
.fadeIn(2000)
.delay(2000)
.fadeOut(2000, showFadeInFadeOut);
}
showFadeInFadeOut();
}
});
Html:
<ul>
<li class="client-testimonial"><img src="demo1.png"></li>
<li class="client-testimonial"><img src="demo2.png"></li>
<li class="client-testimonial"><img src="demo3.png"></li>
<li class="client-testimonial"><img src="demo4.png"></li>
</ul>
jQuery:
jQuery(document).ready(function(){
if(jQuery('.client-testimonial').length > 0){
var showImg = jQuery(".client-testimonial-img");
var quoteIndex = -1;
function showFadeInFadeOut() {
++quoteIndex;
showImg.eq(quoteIndex % showImg.length)
.fadeIn(2000)
.delay(2000)
.fadeOut(2000, showFadeInFadeOut);
}
showFadeInFadeOut();
}
});

The fade-in/fade-out example is a good demonstration of using a recursive callback to cycle through testimonial images with controlled animation and delay. The use of fadeIn(), delay(), and fadeOut() makes the sequence easy to follow.
ReplyDeleteFor developers learning Javascript Online Course, the quoteIndex % showImg.length expression is particularly useful because it allows the animation to loop back to the first image after reaching the end of the collection.
The document.ready() wrapper also ensures that the animation logic runs after the page has loaded, while checking whether the testimonial elements exist before starting the sequence. This provides a practical example for anyone exploring Javascript Online Training.
ReplyDelete