0

I would like to design a page on which the back ground image changes in a round-robin fashion, indefinitely. Now I have something that works but only if there's no other <div>s on that page as it loops through all the divs. Now I want to add content in other divs so I need to edit that jQuery somehow to only loop through the divs with "backgnd" in their class name. How do I do this? My example link: http://jsfiddle.net/bbqunfhu/26/ function to edit:

function fadeDivs() {
var visibleDiv = $('.bckgnd:visible:first'); //find first visible div
visibleDiv.fadeOut(400, function () {  //fade out first visible div
   var allDivs = visibleDiv.parent().children(); //all divs to fade out / in
   var nextDivIndex = (allDivs.index(visibleDiv) + 1) % allDivs.length;  //index of next div that comes after visible div
   var nextdiv = allDivs.eq(nextDivIndex); //find the next visible div
   nextdiv.fadeIn(400); //fade it in
});
};
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
stdcerr
  • 13,725
  • 25
  • 71
  • 128

2 Answers2

3

i have changed to search only children with class '.bckgnd'.

var allDivs = visibleDiv.parent().children('.bckgnd'); //all divs to fade out / in

here

Nisanth Sojan
  • 1,099
  • 8
  • 21
1

Use below code

in the below code i have images array, place u r image name there , i will pick the background image randomly and it will display there

in the below code i use the background dom name as "background_image" , u can change the dom name as per your requirement

$(function() {

var images = ['img_login_bknd3.jpg', 'img_login_bknd2.jpg', 'img_login_bknd1.jpg', 'img_login_bknd4.jpg', 'img_login_bknd5.jpg', 'img_login_bknd6.jpg','img_login_bknd7.jpg', 'img_login_bknd8.jpg'];

$('#background_image').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});

});