Jquery 悬停时显示较大的图像并停留在视口内
所以我发现这个脚本完全符合我的要求(从其主页下载),唯一的问题是,如果图像太大或缩略图靠近浏览器边缘,则较大的图像不会完全显示在浏览器中(我在这里找到的视口)。经过一些搜索,我修改了它,但仍然停留在悬停状态,它有时会显示在视口外,然后在鼠标移动后,较大的img得到正确的重新定位, 原始jsJquery 悬停时显示较大的图像并停留在视口内,jquery,image,Jquery,Image,所以我发现这个脚本完全符合我的要求(从其主页下载),唯一的问题是,如果图像太大或缩略图靠近浏览器边缘,则较大的图像不会完全显示在浏览器中(我在这里找到的视口)。经过一些搜索,我修改了它,但仍然停留在悬停状态,它有时会显示在视口外,然后在鼠标移动后,较大的img得到正确的重新定位, 原始js this.imagePreview = function(){ xOffset = 10; yOffset = 30; $("a.preview").hove
this.imagePreview = function(){
xOffset = 10;
yOffset = 30;
$("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();
});
this.imagePreview=function(){
xOffset=10;
yOffset=30;
$(“a.preview”).hover(函数(e){
this.t=this.title;
this.title=“”;
var c=(this.t!=”)?“
”+this.t:”;
$(“正文”)。追加(“”+c+”
”;
$(“预览”)
.css(“顶部”(e.pageY-xOffset)+“px”)
.css(“左”(e.pageX+yOffset)+“px”)
.fadeIn(“快速”);
},
函数(){
this.title=this.t;
$(“#预览”).remove();
});
$(“a.preview”).mousemove(函数(e){
$(“预览”)
.css(“顶部”(e.pageY-xOffset)+“px”)
.css(“左”(e.pageX+yOffset)+“px”);
});
};
//在页面加载时启动脚本
$(文档).ready(函数(){
图像预览();
});
我改变了悬停和鼠标移动来制作这个版本
this.imagePreview = function(){
xOffset = 10;
yOffset = 20;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
$(".preview").hover(function(kmouse){
this.t = this.title;
this.title = "";
src=this.src;
if($(this).data("imgmode")=='1')
{
src=src.replace("/thumbs","");
}
var c = (this.t != "") ? "<br/>" + this.t : "";
$("body").append("<p id='preview'><img src='"+ src +"' alt='Image preview' />"+ c +"</p>");
},
function(){
this.title = this.t;
$("#preview").remove();
});
$(".preview"). mousemove(function(kmouse){
var border_top = $(window).scrollTop();
var border_right = $(window).width();
var left_pos; var top_pos; var offset = 15;
if(border_right - (offset *2) >= $("#preview").width() + kmouse.pageX){ left_pos = kmouse.pageX+offset; } else{ left_pos = border_right-$("#preview").width()-offset; } if(border_top + (offset *2)>= kmouse.pageY - $("#preview").height()){ top_pos = border_top +offset; } else{ top_pos = kmouse.pageY-$("#preview").height()-offset; } $("#preview").css({left:left_pos, top:top_pos}).fadeIn("fast");
});
};
$(document).ready(function(){
imagePreview();
});
this.imagePreview=function(){
xOffset=10;
yOffset=20;
//这两个变量确定弹出窗口与光标的距离
//您可能需要调整以获得正确的结果
$(“.preview”).hover(函数(kmouse){
this.t=this.title;
this.title=“”;
src=this.src;
if($(this).data(“imgmode”)=='1')
{
src=src.replace(“/thumbs”,”);
}
var c=(this.t!=”)?“
”+this.t:”;
$(“正文”)。追加(“”+c+”
”;
},
函数(){
this.title=this.t;
$(“#预览”).remove();
});
$(“.preview”).mousemove(函数(kmouse){
var border_top=$(窗口).scrollTop();
var border_right=$(window.width();
左侧变量位置;顶部变量位置;变量偏移=15;
if(border_right-(offset*2)>=$(“#preview”).width()+kmouse.pageX{left_pos=kmouse.pageX+offset;}else{left_pos=border_right-$(“#preview”).width()-offset;}if(border#top+(offset*2)>=kmouse.pageY-$(“#preview”).height(){top#pos border#top+offset;}else}else{top#pos border#top=kmouse.pageY-$.pageY;}preview;}高度预览($)({left:left_pos,top:top_pos});
});
};
$(文档).ready(函数(){
图像预览();
});
正如你所看到的,我做了一些其他的小改动,比如用类预览调整所有元素,不仅添加了一个自定义的HTML5标记数据imgmode,尽管它在这个问题上不起作用,只是为了满足我的站点需求
这里的第一篇文章,仍然是js/jQuery的初学者,但希望我能给你们所有你们需要的信息:)