jQuery缩略图悬停弹出窗口

问题描述 投票:1回答:2

我在Asp.Net中将鼠标悬停在图像上时正在创建JQuery Show Large Image Preview。

请看下面的截图

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的事情。

jquery css asp.net
2个回答
1
投票

您能否尝试使用以下功能来计算预览的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>

0
投票

请关注我的网站@ https://dailyimagefunda.com/merry-christmas-images,并支持我的网站。

© www.soinside.com 2019 - 2024. All rights reserved.