Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Image Slideshow

  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Image Slideshow

    So I am still on the same school assignment as my prior post. Officially pulling my hair out now. This image slideshow is set up to start a slideshow onload event. That works fine. The application has a previous and next button. I am supposed to stop the setInterval slideshow and control it by the previous and next buttons. Here is what I have so far and I cannot figure out why it will not work for the life of me. Please forgive me if this is a dumb question as I am a complete noob in javascript. Any help is greatly appreciated.

    Code:
    var $ = function (id) { 
    	return document.getElementById(id); 
    }
    
    	//next button
    	var imageCounter = 0;
    	
    	var next_button = function (){
    		    	
    		    imageCounter = (imageCounter + 1) % imageCache.length;
    		    image = imageCache[imageCounter];
    		    imageNode.src = image.src;
    		    captionNode.firstChild.nodeValue = image.title;
            }
           
    		
    	//previous button
    	
    	var previous_button = function (){
    		 	imageCounter = (imageCounter - 1) % imageCache.length;
    		    image = imageCache[imageCounter];
    		    imageNode.src = image.src;
    		    captionNode.firstChild.nodeValue = image.title;
            }
    	
    
    window.onload = function () {
    	$("next").onclick = next_button;
    	$("previous").onclick = previous_button;
        var listNode = $("image_list");    
        var captionNode = $("caption");
        var imageNode = $("image");
            
        var links = listNode.getElementsByTagName("a");
        
        // Process image links
        var i, linkNode, image;
        var imageCache = [];
        for ( i = 0; i < links.length; i++ ) {
            linkNode = links[i];
    
            // Preload image and copy title properties
            image = new Image();
            image.src = linkNode.getAttribute("href");
            image.title = linkNode.getAttribute("title");
            imageCache.push(image);
        }
    
        
        
    		
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    What "setInterval slideshow"? I don't see any setInterval any place in there.

    I'm surprised you are allowed to use jQuery in JavaScript homework.

    I will say that this is wrong:
    Code:
    imageCounter = (imageCounter - 1) % imageCache.length;
    Think about it. What happens if imageCounter is zero when you hit that statement?

    What can you easily do to fix that line?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •