0

I'm making a hover image preview effect using jquery. The user hovers over a thumbnail and it displays a larger version. I have different widths/heights for the images being previewed. What it does right now is display the image to the right/bottom of the thumbnail. What I want it to do is display to the left/top of the image, but as there are different image dimensions, if I use a static dimension (aka the offsets in the current code), there's unwanted space between the cursor and image. I want the script to get the image height/width of each image and display dynamically based on what those properties are. Any help is greatly appreciated.

The html/css code is as follows:

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

The 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();
});
4

1 回答 1

0

我会使用 position:relative; 对于更大的图像,并使用它来将它带到左上角一点。

position: relative;
top: -10px;
left: -10px;

这对你有用吗?

于 2013-11-04T22:13:27.817 回答