Easy exchange of fading images

I'm having trouble finding a simple jQuery image exchange. Most of the examples I have found are more complex than I need and do what I don't want.

Purpose: I have 5 images that I want to fade out, embed, etc. I would love to fade / fade from one image to another, but the slide will be fine too. When the page is 1st loading, I want the 1st image to show for 4 seconds ... then fade out to the next image, 4 seconds, then the next, etc. Endless cycle.

Currently my code is a simple image, not very elegant:

document.getElementById("imgMain").src = "images/yurt/sleigh.png";

      

What's the best and easiest way to do this?

+3


source to share


1 answer


Working example at jsFiddle.

Here's the code:

Html

<div class="fadein">
    <img src="http://farm9.staticflickr.com/8359/8450229021_9d660578b4_n.jpg">
    <img src="http://farm9.staticflickr.com/8510/8452880627_0e673b24d8_n.jpg">
    <img src="http://farm9.staticflickr.com/8108/8456552856_a843b7a5e1_n.jpg">
    <img src="http://farm9.staticflickr.com/8230/8457936603_f2c8f48691_n.jpg">
    <img src="http://farm9.staticflickr.com/8329/8447290659_02c4765928_n.jpg">
</div>

      



CSS

.fadein {
    position:relative;
    height:320px;
    width:320px;
}

.fadein img {
    position:absolute;
    left:0;
    top:0;
}

      

JavaScript

$('.fadein img:gt(0)').hide();

setInterval(function () {
    $('.fadein :first-child').fadeOut()
                             .next('img')
                             .fadeIn()
                             .end()
                             .appendTo('.fadein');
}, 4000); // 4 seconds

      

+6


source







All Articles