SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie question: Jquery hover problem with array of thumbnails

    Hi,
    Newbie question here - sorry if this appears a bit naive / dumb etc.
    I'm having problems with a function and not sure what the answer is. The function is as follows:

    Code:
    function display (array) {
    	for (i=0, max = array.length; i<max; i++){	
    		var galleryTitle=array[i][3];
    		var gallerySize=array[i][2];
    		var galleryDate=array[i][1];
    		var galleryDescription=array[i][4];
    		
    		$('#container').append(
    		
    		'<div id="'+array[i][0]+'" style="position: absolute;  left:'+array[i][6]+'px; top: '+array[i][7]+'px;"><a href="#"><img src=images/'+array[i][5]+'></a></div>'
    		);
    		
    		$("#"+i).hover( 
    			function () { 
    				$(this).append('<div id="description"><b>Title: </b>'+galleryTitle+'<br /><b>Images: </b>'+gallerySize+'<br /><b>Date: </b>'+galleryDate+'<br /><br />'+galleryDescription+'</div>');
    			},  	
    
    			function () { 
        			$(this).find("div:last").remove(); 
    			} 
    		);
    	}
    }
    What I'm trying to do is cause a css box to appear with information about a thumbnail in it, whenever that particular thumbnail is hovered over.

    The information and, indeed, the urls of the thumbnails are stored in an array at the start of the script.

    The code above works and the css box appears *except* whichever thumb I hover over, only information about the thumbnail identified by the last iteration (is that the right word?) of the for loop appears.

    I understand why this is happening (ie, the loop is finished by the time the hover statement comes into being and so 'i' will always be set to the last value) but I can't work out what to do about it. I've tried inserting get element by id calls within the first div but this just seems to knock out the entire script.

    I'm happy to post the script in its entirety up here if that helps but thought I;d just hit you all with the bare minimum first.

    Any ideas where I should be heading on this?

    Many thanks,
    Stef
    Last edited by ParkinT; Mar 5, 2012 at 13:45. Reason: Corrected unfinished "thanks"

  2. #2
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I think you are making this harder than it need to be. Let the function loop do it's job, then you have a jQuery hover for the thumbnails. Set a class for the thumbnail and a alt on each thumnal with info. Make a jQuery like this $('.thumbnailclass').hover(function(){$(this).append('<div id="description"><b>Title: </b>'+galleryTitle+'<br /><b>Images: </b>'+gallerySize+'<br /><b>Date: </b>'+galleryDate+'<br /><br />'+galleryDescription+'</div>')});


Tags for this Thread

Bookmarks

Posting Permissions

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