gpt4 book ai didi

jQuery 缩略图悬停弹出窗口

转载 作者:行者123 更新时间:2023-11-28 16:54:26 24 4
gpt4 key购买 nike

我正在创建 JQuery 在 Asp.Net 中将鼠标悬停在图像上时显示大图像预览。

请看下面的截图

enter image description here

我正在使用以下代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show Image Preview when hover on Link using jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
ShowImagePreview();
});
// Configuration of the x and y offsets
function ShowImagePreview() {
xOffset = -20;
yOffset = 40;

$("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("slow");
},

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

</script>
<style type="text/css">
#preview{
position:absolute;
border:3px solid #ccc;
background:#333;
padding:5px;
display:none;
color:#fff;
box-shadow: 4px 4px 3px rgba(103, 115, 130, 1);
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" class="preview" ToolTip='<%#Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server">
<asp:Image Width="100" ID="Image1" ImageUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server" />
</asp:HyperLink>
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
VerticalAlign="Bottom" />
</asp:DataList>
</div>
</form>
</body>
</html>

它工作正常,但是当我过去悬停任何图像时,悬停图像会超出页面边框。

请帮助它不要外出。它将管理类似 This 的内容.

最佳答案

能否请您尝试使用以下函数来计算预览的 x-y 位置:

使用辅助函数更新了 HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show Image Preview when hover on Link using jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
ShowImagePreview();
});
// Configuration of the x and y offsets
function ShowImagePreview() {
xOffset = -20;
yOffset = 40;

$("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>");

var left = getLeft(e,$(this));
var top = getTop(e,$(this));

$("#preview")
.css("top", (top) + "px")
.css("left", (left) + "px")
.fadeIn("slow");
},
function() {
this.title = this.t;
$("#preview").remove();
});

$("a.preview").mousemove(function(e) {

var left = getLeft(e,$(this));
var top = getTop(e,$(this));

$("#preview")
.css("top", (top) + "px")
.css("left", (left) + "px");
});

};

function getLeft(e,obj){
var left = e.pageX + yOffset;
var prevWidth = $("#preview").width();
if((left+prevWidth +50) > $(document).width())
{
left = $(obj).offset().left - yOffset - prevWidth;
}
return left;
}

function getTop(e,obj){
var top = e.pageY - xOffset;
var prevHeigth = $("#preview").height();
if((top+prevHeigth +50) > $(document).height())
{
top = $(obj).offset().top - xOffset - prevHeigth;
}
return top;
}

</script>
<style type="text/css">
#preview{
position:absolute;
border:3px solid #ccc;
background:#333;
padding:5px;
display:none;
color:#fff;
box-shadow: 4px 4px 3px rgba(103, 115, 130, 1);
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" class="preview" ToolTip='<%#Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server">
<asp:Image Width="100" ID="Image1" ImageUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server" />
</asp:HyperLink>
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
VerticalAlign="Bottom" />
</asp:DataList>
</div>
</form>
</body>
</html>

关于jQuery 缩略图悬停弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32367470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com