javascript - 使用 jQuery 进行图像预览,更改悬停图像大小

标签 javascript html css

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

我正在尝试使用 Easiest Tooltip and Image Preview Using jQuery 我正在尝试使用这个 example

它对我来说很好用,

我的问题是如何更改悬停图像的大小(悬停在小图像上时显示在工具提示中的图像) 我的图片太大,我想将其大小设置为 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>');                                

取决于你想如何限制它

关于javascript - 使用 jQuery 进行图像预览,更改悬停图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10882829/

相关文章:

javascript - 同一页面上有两个不同的 Google API,但出现 You have included the Google Maps API multiple times on this page 错误

javascript - Reactjs获取最新选择值的方法

php - 与 URL 混合时未传递 GET 变量

html - 当它们的宽度不适合它们的容器时,我如何垂直包装列?

html - 对齐 td 内的 div

html - IE8 允许用户与被模态窗口遮挡的元素交互

javascript - 带有 getter/setter 的 JSON stringify ES6 类属性

Javascript 检查鼠标在圆或多边形内单击

javascript - 如何避免浏览器的 Zoom 改变 Canvas 的大小?

javascript - Bootstrap 模式未通过 jquery data-remote-load 触发