2010-05-05 63 views
0

在頁面上,我有一個帶縮略圖的可排序列表。 當翻轉圖像時,我想要一個工具提示來顯示更大的圖像。 我發現qTip,但也許有更好/更容易?帶工具提示的jquery ui.sortable

如何將imgPath變量從排序連接到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> 
+0

你如何獲得的URL更大的圖像或在排序列表縮小縮略圖? – Mottie 2010-05-05 19:41:10

+0

我發現的另一個問題是,當前版本的qTip不支持jQuery 1.4.2(參考:http://craigsworks.com/projects/forums/thread-qtip-still-not-working-with-jquery- 1-4-2) – Mottie 2010-05-06 18:45:09

回答

0

我爲您發佈了demo。就像我喜歡qTip一樣,要弄清楚如何設置它來抓取當前HTML中的內容並不容易。我花了20分鐘搞定它,直到我放棄了。但是,好消息是我知道tooltip script有三個版本,其中一個專門用於圖像預覽。

所以你需要重新格式化你的HTML。 <a>中的href包含顯示在工具提示中的大圖像,而<img src>包含縮略圖。您也可以在工具提示中加入標題,方法是在鏈接的標題屬性中添加文本/ HTML(請參閱下面代碼中的第一張圖片)。

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; 
} 

腳本

$(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(); 
}); 
+0

嗨,非常感謝您的演示。最後,我和jqModal一起去了。較少膨脹。 – FFish 2010-05-06 22:14:36