July 13, 201115 yr I am using a jquery background image slider which I would like to add a preload function to. The slider part works fine but I have no idea how to incorporate the preload part as I have never really worked with javascript before. Could someone show me how to get these two playing together? This is the slider script; jQuery(function($) { $(document).ready(function() { // Speed of the automatic slideshow var slideshowSpeed = 6000; // Variable to store the images we need to set as background // which also includes some text and url's. var photos = [ {"image" : "slide-motionless-rock.jpg"}, {"image" : "slide-stac-pollaidh.jpg"}, {"image" : "slide-skiff-race.jpg"}, {"image" : "slide-dundonnel-mountains.jpg"}, {"image" : "slide-achnahaird-suilven.jpg"}, {"image" : "slide-summer-isles.jpg"} ]; $(document).ready(function() { var activeContainer = 1; var currentImg = 0; var animating = false; var navigate = function(direction) { // Check if no animation is running. If it is, prevent the action if(animating) { return; } // Check which current image we need to show if(direction == "next") { currentImg++; if(currentImg == photos.length + 1) { currentImg = 1; } } else { currentImg--; if(currentImg == 0) { currentImg = photos.length; } } // Check which container we need to use var currentContainer = activeContainer; if(activeContainer == 1) { activeContainer = 2; } else { activeContainer = 1; } showImage(photos[currentImg - 1], currentContainer, activeContainer); }; var currentZindex = -1; var showImage = function(photoObject, currentContainer, activeContainer) { animating = true; // Make sure the new container is always on the background currentZindex--; // Set the background image of the new active container $("#headerimg" + activeContainer).css({ "background-image" : "url(wp-content/themes/ata/images/" + photoObject.image + ")", "display" : "block", "z-index" : currentZindex }); // Fade out the current container // and display the header text when animation is complete $("#headerimg" + currentContainer).fadeOut(function() { setTimeout(function() { $("#headertxt").css({"display" : "block"}); animating = false; }, 500); }); }; var stopAnimation = function() { // Change the background image to "play" $("#control").css({ "background-image" : "url(images/btn_play.png)" }); // Clear the interval clearInterval(interval); }; // We should statically set the first image navigate("next"); // Start playing the animation interval = setInterval(function() { navigate("next"); }, slideshowSpeed); }); }); }); And this is the preload I would like to add to the slider script; var images = [ src1, src2 ]; //basic usage $({}).imageLoader({ images: images, async: true }); //with events $({}).imageLoader({ images: images, async: true, complete: function(e, ui) { var i = ( ui.i + 1 ); var t = ( new Date( ).getTime( ) - time.getTime( ) ); var o = getGetOrdinal( orderNum ); $(body).append("<p>image "+i+ " was uploaded "+o+", "+t+"ms after imageLoader was called</p>"); }, error: function(e, ui) { var i = ( ui.i + 1 ); var t = ( new Date( ).getTime( ) - time.getTime( ) ); var o = getGetOrdinal( orderNum ); $(body).append("<p>image "+i+ " did not load properly. Please check the source: "+ui.data[ui.i].src+"</p>"); }, allcomplete: function(e, ui) { $(body).append("<p>all images have finished uploading</p>"); } }); And credit where credit is due; I'm using a cut down version of the background slider script found at http://www.marcofolio.net/webdesign/advanced_jquery_background_image_slideshow.html And I found the preload script at http://staticvoid.info/imageLoader/
Create an account or sign in to comment