0
我正在使用jquery進行懸停圖像預覽效果。用戶懸停在縮略圖上,並顯示更大的版本。對於正在預覽的圖像,我有不同的寬度/高度。它現在所做的是將圖像顯示在縮略圖的右側/底部。我想要它做的是顯示在圖像的左側/頂部,但由於圖像尺寸不同,如果我使用靜態尺寸(也就是當前代碼中的偏移量),光標和圖像之間會存在不需要的空間。我希望腳本獲取每個圖像的圖像高度/寬度,並根據這些屬性的內容動態顯示。任何幫助是極大的讚賞。jQuery懸停圖像效果
的HTML/CSS代碼如下:
<ul class="gallery" style="margin-top: 50px;" align="center">
<li><a href="images/lpage-image1.png" class="preview" onclick="return false"><img src="images/lpage-image1-thumb.png" alt="gallery thumbnail" /></a></li>
<li><a href="images/lpage-image2.png" class="preview" onclick="return false"><img src="images/lpage-image2-thumb.png" alt="gallery thumbnail" /></a></li>
<li><a href="images/lpage-image3.png" class="preview" onclick="return false"><img src="images/lpage-image3-thumb.png" alt="gallery thumbnail" /></a></li>
</ul>
#preview{
position:absolute;
border:1px solid #ccc;
background:#333;
padding:5px;
display:none;
margin-top: -250px;
float: left;
color:#fff;
}
jQuery的:
this.imagePreview = function() {
/* CONFIG */
xOffset = 80;
yOffset = -400;
var height = 0;
var width = 0;
// these variables determine popup's distance from the cursor
/* 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")
.each(
height = $(this).height();
width = $(this).width();)
.css("top", (e.pageY - height) + "px")
.css("left", (e.pageX - width) + "px")
.fadeIn("fast")
},
function() {
this.title = this.t;
$("#preview").remove();
});
$("a.preview").mousemove(function (e) {
$("#preview")
.css("top", (e.pageY - width) + "px")
.css("left", (e.pageX + height) + "px");
});
};
// starting the script on page load
$(document).ready(function() {
imagePreview();
});
我做了一個小提琴... http://fiddle.jshell.net/VM4sv/ – user2896540