3

I want to make a basic fadein fadeout slideshow.I only make it auto run once. How to make it have a loop??

html

<img src="image1.jpg" class="img1">
<img src="image2.jpg" class="img2">

js

$(document).ready(function () {
function playslider(){
        $('.img1').fadeIn(800).delay(800).fadeOut(800);
        $('.img2').delay(1600).fadeIn(800).delay(800).fadeOut(800);
}   
playslider();
});
2
  • you mean you want to autoplay it only once? Commented Mar 14, 2012 at 4:28
  • NO,I make it--> fadeOut(800, playslider).And how to I make last image fadeout and cross with the next first image fadein?? Commented Mar 14, 2012 at 4:33

4 Answers 4

8

Try changing it to this:

$(document).ready(function () {
function playslider(){
        $('.img1').fadeIn(800).delay(800).fadeOut(800);
        $('.img2').delay(1600).fadeIn(800).delay(800).fadeOut(800, playslider);
                                                                 //^callback function
}   
playslider();
});

This is called a "callback function". The idea is that it gets called when your last fadeout completes. You can read more here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.How to I make last image fadeout and cross with the next first image fadein.
Cross fading is beyond the scope of your current question and would be difficult for me to answer in a comment. I would recommend creating a new question for that particular problem. I will say that in the past I have typically used a jQuery plugin to handle cross fading.
2

You can use good old Javascript:

 $(document).ready(function () {
 function playslider(){
      // code to execute
     x = setTimeout("playslider()", 5000); //will loop every 5 seconds.
 }   
 playslider(); //start it up the first time

 });

Comments

2

The answer is:

$(document).ready(function () {
 function playslider(){
      // code to execute
     x = setTimeout(function(){playslider()}, 5000);
 }   
 playslider();

 });

Comments

0

it works good but if u change it in this way it works better

    $('.img1').fadeIn(800).delay(800).fadeOut(800);
    $('.img2').delay(2400).fadeIn(800).delay(800).fadeOut(800, playslider);

just increase dalay time to 2400(2400=800+800+800)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.