实现悬停信息框

发布于 2024-10-13 19:08:10 字数 1607 浏览 1 评论 0原文

我有一个日历,当用户将鼠标悬停在单元格上时,会出现一个很大的信息框,其中包含该日期的详细信息。虽然当用户离开时使信息框消失,但我遇到了一些麻烦。

我基本上想要它,这样当鼠标光标移出信息框隐藏的日历单元格时,它就会消失。但我在这方面遇到了麻烦,因为 mouseentermouseleave 由于将信息框作为顶部元素而变得混乱。

因此,我尝试通过使用透明的“占位符”div 来解决此问题,其形状和位置与其下方的日历单元格相同,并且 z 索引为 1000,因此它们位于信息框上方。然后,我将 mouseentermouseleave 事件应用于这些 div。

但这有两个问题。第一,我现在在语义上弄乱了我的 HTML。 div 没有任何目的,只是为了绕过似乎存在的限制。其次,它们弄乱了我的 jQuery UI 选择(我已将其应用到日历单元格 - 单击不再选择单元格)。

有没有一种干净的方法来处理显示信息框?用户不会与信息框进行交互——它只是显示信息。

编辑:这是一些代码:

<li>
    <div class="day-content">
    </div>
    <div class="day-content-placeholder">
    </div>
</li>

以及 CSS

li
    { position: absolute; width: 15%; height: 20%; top: ... left: ... }
.day-content
    { position: absolute; width: 100%; height: 100%; }
.day-content-placeholder
    { position: absolute; width: 100%; height: 100%; z-index: 1000; }
.popup
    { position: absolute; width: 300%; height: 300%; left: -150%; top: -150%; z-index: 500; }

和 Javascript

var popup;
$('.week-content-placeholder')
    .mouseenter(function()
        {
        popup = $('<div class="popup">'+a_lot_of_text+'</div>').insertAfter(this);
        })
    .mouseleave(function()
        {
        popup.remove();
        });

这不是确切的代码,但您明白了。这工作正常,但就像我说的,由于 .week-content-placeholder 位于 .week-content 之上,jQuery UI 的选择功能在 .week-content-placeholder 上无法正常工作。代码>.week-content。

I have a calendar, and when the user hovers over a cell, a large-ish info box appears with details for that date. I am having some trouble though making the info box disappear when the user moves away.

I basically want it so that when the mouse cursor moves out of the calendar cell which is hidden by the info box it will disappear. But I'm having trouble with this because mouseenter and mouseleave get messed up by having the info box as the top element.

So I tried to get around this by using "placeholder" divs that are transparent, have the same shape and location as the calendar cell beneath it, and have a z-index of 1000 so they are above the info box. I then apply the mouseenter and mouseleave events to these divs instead.

There's two problems with this though. One, I have now messed up my HTML semantically. The divs have no purpose but to get around what seems to be a limitation. And secondly, they mess up my jQuery UI selection (I've applied it to the calendar cells - a click no longer selects a cell).

Is there a clean way to handle displaying an info box? There will be no user interaction with the info box -- it's just to display information.

EDIT: Here is some code:

<li>
    <div class="day-content">
    </div>
    <div class="day-content-placeholder">
    </div>
</li>

and CSS

li
    { position: absolute; width: 15%; height: 20%; top: ... left: ... }
.day-content
    { position: absolute; width: 100%; height: 100%; }
.day-content-placeholder
    { position: absolute; width: 100%; height: 100%; z-index: 1000; }
.popup
    { position: absolute; width: 300%; height: 300%; left: -150%; top: -150%; z-index: 500; }

and Javascript

var popup;
$('.week-content-placeholder')
    .mouseenter(function()
        {
        popup = $('<div class="popup">'+a_lot_of_text+'</div>').insertAfter(this);
        })
    .mouseleave(function()
        {
        popup.remove();
        });

That's not the exact code, but you get the idea. This works okay, but like I said, since .week-content-placeholder is above .week-content, the selection capability with jQuery UI doesn't work properly on .week-content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

送舟行 2024-10-20 19:08:11

这里的技巧是使信息框成为单元格的子元素:

<div id='box'>
    Normal content
    <div id='inner'>
        This big box obscures everything in the cell!
    </div>
</div>

内部框隐藏直到发生悬停。请注意,我们如何使用 CSS 使框比单元格本身更大,并且边距为负。

#box
{
  width:100px;
  height:100px;
  margin:100px;
  border:solid 2px darkblue;
  position:relative;
}
#box #inner
{
  display:none;
  position:absolute;
  background-color:#eeee00;
  top:-10px;
  left:-10px;
  width:120px;
  height:120px;
}

您可以使用普通的 jquery 悬停,因为悬停覆盖了框及其子框:

$('#box').hover(function(){
    $('#inner').show();
},function(){
    $('#inner').hide();
});

这是它的运行情况:

http://jsfiddle。 net/RbqCT/

您可以像在代码中一样动态创建信息框。

The trick here is to make the info box a child of the cell:

<div id='box'>
    Normal content
    <div id='inner'>
        This big box obscures everything in the cell!
    </div>
</div>

The inner box is hidden until the hover occurs. Notice how with CSS we can make the box bigger than the cell itself with negative margins.

#box
{
  width:100px;
  height:100px;
  margin:100px;
  border:solid 2px darkblue;
  position:relative;
}
#box #inner
{
  display:none;
  position:absolute;
  background-color:#eeee00;
  top:-10px;
  left:-10px;
  width:120px;
  height:120px;
}

And you can use normal jquery hover because the hover covers box the box and it's child:

$('#box').hover(function(){
    $('#inner').show();
},function(){
    $('#inner').hide();
});

Here's it running:

http://jsfiddle.net/RbqCT/

You can create the info box dynamically as you do in your code.

用心笑 2024-10-20 19:08:11

这里有 15 个不同的插件,可让您使用 jquery 执行此操作:

http://www.webdesignbooth .com/15-jquery-plugins-to-create-an-user-friend-tooltip/

Here's 15 different plugins that let you do this with jquery:

http://www.webdesignbooth.com/15-jquery-plugins-to-create-an-user-friendly-tooltip/

猛虎独行 2024-10-20 19:08:11

您可以跟踪 mousemouse 并使用悬停触发器的 offsetLeft + width 和 offsetTop + height 与 event.pageX 和 event.pageY 进行比较。

You could track mousemouse and use the offsetLeft + width and offsetTop + height of your hover trigger against the event.pageX and event.pageY to compare.

梦里寻她 2024-10-20 19:08:11

如果您按照您所描述的方式进行此工作,则保留在日历单元格内(甚至不可见)的微小鼠标移动会将弹出窗口保留在适当的位置,但是退出单元格的稍大的移动会使弹出窗口消失。

用户只能看到弹出窗口本身的移动——弹出窗口内的微小移动会将其保持在原位;大的运动使它消失。

我建议在退出弹出窗口 div 本身时触发弹出窗口的消失。任何留在“提示”面板内的移动都会将其保留。我认为(1)这是更好的可用性,(2)它避免了模糊日历单元事件处理的整个问题。

您可以通过在创建 div 时添加 .mouseleave() 处理程序来实现此目的。

If you make this work as you described a tiny mouse movement that remains within the calendar cell (which is not even visible) leaves the popup in place, but a slightly larger movement that exits the cell makes the popup disappear.

The user sees only movement within the popup itself — small movement within the popup leaves it in place; large movement makes it go away.

I suggest triggering the disappearance of the popup on exiting the popup div itself. Any movement that remains within the "tip" panel leaves it up. I think that (1) this is better usability and (2) it avoids the whole problem with the obscured calendar cell event handling.

You could do that by adding a .mouseleave() handler to the div when you create it.

阳光的暖冬 2024-10-20 19:08:10

您可以通过以下方式使用透明的“占位符”div 修改您的解决方案:

使用 {zIndex: -1} 将“占位符”潜水到“日历单元格”下方。

当您输入日历单元格时,取消隐藏大的“内容”div 并在“占位符”div 上设置 {zIndex: 1000} 以将其置于顶部。

在占位符 div 上有一个“mouseout”事件,该事件将隐藏“内容”div 并为“占位符”单元格设置 {zIndex: -1}

您可以在 JavaScript 中创建一个“占位符”单元格,然后在“鼠标插入”时将其移动到每个“日历”单元格的位置,而不是在 HTML 中创建“占位符”单元格。您还可以将“日历单元格”上的任何“点击”事件复制到此单元格上。

让我知道这是否有效。

You could modify your solution with the transparent "placeholder" divs in the following way:

Have the "placeholder" dive underneath the "calendar cell", using {zIndex: -1}.

When you enter a calendar cell, unhide the large "content" div and set {zIndex: 1000} on the "placeholder" div to bring it to the top.

Have a "mouseout" event on the placeholder div that will hide the "content" div and set {zIndex: -1} for the the "placeholder" cell.

Rather than create the "placeholder" cells in the HTML, you could create one in the javascript and move it to the postion of each "calendar" cell as you "mouseIn" it. You could also duplicate any "click" events on the "calendar cell" onto this one as well.

Let me know if this works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文