views:

96

answers:

1

On a page I have a sortable list with thumbnails. When rolling over the images I wanted a tooltip to show a bigger image. I found qTip, but maybe there is something better / easier?

How can I connect the imgPath var from the sortable to the qtip?

var imgPath = '<img src="002171/imm/001.jpg" />';

$("#sortable").sortable();
$("#sortable").disableSelection();

$('#sortable li img').qtip({
    content: {
        text: imgPath
    }
});


<div id="demo">
    <ul id="sortable">
        <li><img src="002171/tn/001.jpg" /></li>
        <li><img src="002171/tn/002.jpg" /></li>
        <li><img src="002171/tn/003.jpg" /></li>
    </ul> 
</div>
A: 

I posted a demo for you. As much as I like qTip, it isn't easy to figure out how to set it up to grab content from within the current HTML.. I spent 20 minutes messing with it until I gave up. But, good news is I do know that this tooltip script has three versions, and one is specifically made for image previews.

So you'll need to reformat your HTML a bit. the href in the <a> contains the large image that shows up in the tooltip while the <img src> contains the thumbnail. You can also include a caption in the tooltip by adding text/HTML into the title attribute of the link (see the first image in the code below).

HTML

<div id="demo">
    <ul id="sortable">
        <li><a class="preview" href="01.gif" title="This image has a caption"><img src="01.gif" /></a></li>
        <li><a class="preview" href="02.gif"><img src="02.gif" /></a></li>
        <li><a class="preview" href="03.gif"><img src="03.gif" /></a></li>
        <li><a class="preview" href="04.gif"><img src="04.gif" /></a></li>
        <li><a class="preview" href="05.gif"><img src="05.gif" /></a></li>
        <li><a class="preview" href="06.gif"><img src="06.gif" /></a></li>
        <li><a class="preview" href="07.gif"><img src="07.gif" /></a></li>
        <li><a class="preview" href="08.gif"><img src="08.gif" /></a></li>
        <li><a class="preview" href="09.gif"><img src="09.gif" /></a></li>
        <li><a class="preview" href="10.gif"><img src="10.gif" /></a></li>
    </ul> 
</div>

CSS

.preview { cursor:pointer; }
#preview {
    color: #ddd;
    background: #222;
    border: 1px solid #333;
    padding: 5px;
    display: none;
    opacity: 0.9;
    filter: alpha(opacity=90);
    text-align: center;
}

Script

$(document).ready(function(){
    $("#sortable").sortable();
    $("#sortable").disableSelection();
});

// You should load the image preview script below separately
// but I've included it here for completeness

/*
* Image preview script
* powered by jQuery (http://www.jquery.com)
*
* written by Alen Grakalic (http://cssglobe.com)
*
* for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
*
*/

this.imagePreview = function(){    
    /* CONFIG */
    xOffset = 10;
    yOffset = 30;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result
    /* END CONFIG */
    $("a.preview").hover(function(e){
        this.t = this.title;
        this.title = "";    
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");                                 
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("fast");                        
    }, function(){
        this.title = this.t;    
        $("#preview").remove();
    });    
    $("a.preview").mousemove(function(e){
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });            
};

// starting the script on page load
$(document).ready(function(){
    imagePreview();
});
fudgey
Hey fudgey, thanks very much for your demo. At the end I went with jqModal. Less bloated.
FFish