使用 jQuery 预览图像,更改悬停图像大小

Image Preview with jQuery, change the Hover image size

本文关键字:图像 悬停 jQuery 使用      更新时间:2023-09-26

这应该是一个非常简单的问题:

我正在尝试使用jQuery使用最简单的工具提示和图像预览我正在尝试使用此示例

它对我来说工作正常,

我的问题是如何更改悬停图像大小(悬停小图像后工具提示中显示的图像大小)我的图像太大,我想将其大小设置为 200px x 200px我该怎么做?

这是当前样式:

<style>
#preview{
    position:absolute;
    border:1px solid #ccc;
    background:#333;
    padding:5px;
    display:none;
    color:#fff;
    }
</style>

这是Javascript:

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

这是主要的:

<a href="1.jpg" /*class="preview"*/><img src="1s.jpg" alt="gallery thumbnail" /></a>

添加

#preview img {
  height:200px;
}

或更改

    $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");                                

    $("body").append('<p id="preview"><img style="height:200px" src="'+ this.href +'" alt="Image preview" />'+ c +'</p>');                                

    $("body").append('<p id="preview"><img style="width:200px" src="'+ this.href +'" alt="Image preview" />'+ c +'</p>');                                

取决于您要如何限制它