【问题标题】:jQuery Thumbnail Hover PopupjQuery 缩略图悬停弹出窗口
【发布时间】:2015-11-28 19:21:45
【问题描述】:

我正在创建 JQuery Show Large Image Preview When Hover On Image In Asp.Net。

请看下面的截图

我正在使用以下代码

<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的东西。

【问题讨论】:

  • 放置 HTML 或更好地制作 jsfiddle.net 这样可以轻松访问答案。在 jQuery 中,您可以获得 mousemove x,y 位置并检查它是否大于您的页面边框 x,y 值,如果大于则根据(减去某个值)设置您的预览元素
  • 我认为您还需要在 this link 中提到的文档中处理mousemove
  • @GuruprasadRao 我明白了:/
  • 您能否提供@Satindersingh 在他的 cmets 中提到的问题,以便我们可以实际看到正在发生的事情。另外请提及您使用的图像尺寸是多少?
  • 实际上我不知道如何创建asp.net fiddle,因为值来自数据库,thumb 图像的大小为170 x 170,鼠标悬停为340 x 340 。水平和垂直图像都包含在其中。 @GuruprasadRao

标签: jquery css asp.net


【解决方案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>

【讨论】:

  • 没有得到任何结果:(
  • 使用这个后鼠标悬停图像不显示
  • 好的..给我一点时间...将修改您的整个 HTML 并发布到答案中
  • 你好 Gitz,这个新的 HTML 成功了吗?
  • 很高兴知道...谢谢!
猜你喜欢
  • 2011-09-24
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
相关资源
最近更新 更多