views:

46

answers:

1

Hello,

I dont know alot about Javascript. I want about 5 background images to continuously rotate and fade with a 5 second interval.

There are a lot of image slideshows available on the web, but I need it to be with a background image on my site.

Any help is much appreciated!

A: 

Using jQuery you can do it like this:

setInterval(function(){
    var source = $("#background-images img:first").attr("src");
    $('#background-images img:first').appendTo($('#background-images'));
    $('#fading-images-container').css('background', 'url('+ source +') no-repeat');
},5000);

html:

<div id="fading-images-container"> </div> <!-- this div will show the fading background images after picking them up from the background-images div -->
<div id="background-images"> <!-- hide this div in the CSS, it's only to hold the images--> 
 <img src="" alt="" />
 <img src="" alt="" />
 <img src="" alt="" />
 <img src="" alt="" />
 <img src="" alt="" />
</div>

To use fading, I would suggest not using background images, but actual images in the div.

I have done it like this in the past:

setInterval(function(){
    $('#fading-images:first-child').fadeOut('slow')
     .next('img').fadeIn('slow')
     .end().appendTo('#slideshow');}, 
     4000);

html:

  <div id="fading-images"> 
    <img src="img/home-1.jpg"> 
    <img src="img/home-2.jpg"> 
    <img src="img/home-3.jpg"> 
  </div> 
Moin Zaman
Wouldn't the .remove() completely remove that image from the DOM? Maybe change it to $('#background-images img:first').appendTo($('#background-images')); ? Which would move it to the end of the list?
WSkid
Thanks for the quick reply,Although I cant seem to get it to work. I tried also replacing$('#background-images img:first').remove();with$('#background-images img:first').appendTo($('#background-images'));but still no luck.does the javascript just need <script type="text/javascript"> and </script> around it?Thanks!
Eza
@WSKid: you're right. appending would be better. updated. @Eza: sorry, I forgot to mention that my code is assuming you can or are using the jQuery library
Moin Zaman
oh I see, So i will need to put the code inside:$(function(){//*CODE*//}); ?I have this library http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.jsThanks!
Eza
@Eza: yes you are right, you can put inside the `$(function(){//*CODE*//});` which fires on document ready
Moin Zaman
@Moin Zaman: I might be doing something wrong but I cant get it working.I have been trying to insert code to show you what i have done but i dont know the tags to but around the code. Thanks!
Eza
Actually, Its working! in your HTML code you had the 'background-images' as a class, and in the Javascript it was #(id). Thanks so much for all the help, really appreciate it. It doesnt seem it 'fade' through the images, is that easy to add?
Eza
yes, my mistake! fixed now.
Moin Zaman
Thanks, I was hoping to put some kind of FadeIn/Out to smoothen the transitions, how would I add that to the Javascript code?
Eza
@Eza: See my updated answer
Moin Zaman