【问题标题】:Display div (tooltip) in correct position on hover悬停时在正确位置显示 div(工具提示)
【发布时间】:2014-01-15 20:49:15
【问题描述】:

我有一个带有 GridView 的表单和一个带有图像的表格。图像有一个 div (工具提示),显示在鼠标悬停上。 div 显示在表单的最底部,而不是像我想要的那样靠近图像。如何让 div 显示在图像的下方和左侧?

ascx 代码:

<table>
<tr>
<asp:Label ID="Tools" runat="server" Text="Tools" />
<div style="display: inline; position:relative; ">
<asp:ImageButton ID="CSV" ToolTip="CSV" runat="server" ImageUrl="document-excel-csv-icon.png"
OnClick="BtnCSV_Click" onmouseover="showToolTip_CSV()" onmouseout="hideToolTip_CSV()" />
<div id="tooltip-CSV" role="tooltip" class="tooltip" style="display: none;" >
 <label ID="CSV_label">CSV</label>
</div>
</div>
<div style="display: inline; position: relative;">
<asp:ImageButton ID="PDF" ToolTip="PDF" runat="server" ImageUrl="document-pdf-icon.png" 
OnClick="BtnPDF_Click" onmouseover="showToolTip_PDF()" onmouseout="hideToolTip_PDF()" />                            
<div id="tooltip-PDF" role="tooltip" class="tooltip" style="display: none;">
<label ID="PDF_Label">PDF</label>
</div> 
</div>
</td>
</tr>        
</table>

javascript

<script type="text/javascript">
function showToolTip_CSV() {
    document.getElementById("tooltip-CSV").style.display = "inline-block";
}    
function showToolTip_PDF() {
         document.getElementById("tooltip-PDF").style.display = "inline-block";
}   
function hideToolTip_CSV() {
     document.getElementById("tooltip-CSV").style.display = "none";
 }
function hideToolTip_PDF() {
     document.getElementById("tooltip-PDF").style.display = "none";
 }
</script>

css

.tooltip
{        
width: 30px;
padding: 10px;    
position: absolute;
left: 10px;
bottom: -20px;
background: #f4f0ec;
border: 2px solid #e0cfc2;
border-radius: 6px;  
}

【问题讨论】:

    标签: javascript asp.net css tooltip


    【解决方案1】:

    一种简单的方法是将弹出窗口的坐标设置为被点击元素的坐标。这里讨论了几个好的解决方案:Retrieve the position (X,Y) of an HTML element

    我更喜欢的另一种方法是在每个包含图像的 div 中嵌入一个工具提示 div。图像的父 div 将具有相对位置来设置定位上下文 (http://www.impressivewebs.com/absolute-position-css/)。然后,当您使用绝对定位时,工具提示将相对于包含图像的 div 定位。

    【讨论】:

    • 既然我在表格中有图像,我将如何嵌入 div?
    • 没关系。非常感谢您的帮助!不过我有一个小问题。出于某种原因,我只能在页面的第一个选项卡上看到 div。我不确定为什么它们没有出现在我的其他标签上。
    • 每个标签可能有不同的表格。您可能需要更改每个表的标记以包含 DIV。这就是为什么我通常使用 JavaScript 来动态插入那种 DIV。
    • 如果你选择使用带有 jQ​​uery 的 JavaScript 动态插入它们,你会做这样的事情 jsfiddle.net/8jY4n/38
    • 好吧,我决定采用不同的方法,而且它相当简单。我编辑了我的代码以在表格中包含 div。事实证明我已经安装了 jQuery 插件,我所要做的就是将 ToolTip="" 添加到我的 ascx 中,并将这段代码添加到我的 aspx $(function () { $(document).tooltip(); });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多