Alias
Alias

Reputation: 3081

Loop through images

I know this is around a lot, but I just can't get it to work :/

    <div class='container'>
        <img src="images/1.jpg" alt="">
        <img src="images/2.jpg" alt="">
    </div>

So I have 2 images, which with the below CSS are fixed to the full size of the browser window:

        <style type="text/css">
        .container {
            position:fixed; 
            top:-50%; 
            left:-50%; 
            width:200%; 
            height:200%;
        }
        .container img {
            position:absolute; 
            top:0; 
            left:0; 
            right:0; 
            bottom:0; 
            margin:auto; 
            min-width:50%;
            min-height:50%;
        }
    </style>

Currently, it just shows the last image (2) to appear at full size. However, I want to use some jQuery to show the first one, then after X seconds, fade out number 1 and loop through fading in each image. I assume it would be best to use setInterval, then fadeOut and then fadeIn images, but whatever I do doesn't work.

I was trying to go down this line:

        <script type="text/javascript">
        $(document).ready(function() {

            $('.container').children('img').each(function(i) { 
                $(this).fadeOut();
            }); 

        });
    </script>

Upvotes: 4

Views: 16476

Answers (5)

Mamut
Mamut

Reputation: 106

You can try this.

$(document).ready(function() {
    var _intervalId;

    function fadeInLastImg()
    {
        var backImg = $('.container img:first');
        backImg.hide();
        $('.container' ).append( backImg );
        backImg.fadeIn()
    };

    _intervalId = setInterval( function() 
    {
        fadeInLastImg();
    }, 1000 );

});​

Here is the jsFiddle http://jsfiddle.net/KQ3wu/128/

Upvotes: 6

peter
peter

Reputation: 185

Using the plugin jquery-timing it's a one-liner:

$.repeat(1000).$('#container img:first').hide().appendTo('#container').fadeIn();

See as fiddle: http://jsfiddle.net/creativecouple/zjUEs/

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

Reputation: 205997

jsBin demo

Just modified the Fademe jQuery plugin to remove the mouseover cause you use full screen images :):

(function($){
    $.fn.fademe = function(F,P,S){
        F=F||700;
        P=P||3000;
        S=S-1||0;       
        var e=this.children(), T;
        function a(){ e.eq(S=++S%e.length).fadeTo(F,1).siblings(e).stop(1).fadeTo(F,0); }
        e.eq(S).show().siblings(e).hide();
        function aa(){T=setTimeout(function(){a();aa();},F+P);}aa();
    };
})(jQuery);



// USE PLUGIN:
$(function(){    
  $('.container').fademe();  
});

Upvotes: 1

<script>
function showImage1()
{
    $('#IDimage1').show();
    setTimeout(showImage2,3000);
}

function showImage2()
{
   $('#IDimage1').hide();
   $('#IDimage2').show();
}
</script>

well this is just the simplest approach , other than this u can try toggle or fadein/out functions from JQuery

Upvotes: 0

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

try this code:

<script type="text/javascript">
        $(document).ready(function() {
            var intervalId = setInterval(function() {
              $('.container').find('img').each(function(i) { 
                $(this).fadeOut();
              }); 
            $(this).data('intervalId', intervalId);
        });
    </script>

Upvotes: 0

Related Questions