简体   繁体   English

在jQuery UI Datepicker上检测Mouseover事件

[英]Detect mouseover event over jquery ui datepicker

I am using a form placed on a div that moves in and out of the screen when hover on it. 我正在使用放置在div上的窗体,当将鼠标悬停在其上时,该窗体可以移入和移出屏幕。 For that to take effect I use jquery .hover() event: 为此,我使用jquery .hover()事件:

$('#formulario').hover(
    function () {
        $(this).stop().animate({'marginRight':'-2px'},200);
        },
    function () {
        $(this).stop().animate({'marginRight':'-345px'},200);
        }
    );

The problem is I am using jquery UI datepicker in that form, and when I move to the widget to select the date, the div's mouseleave event executes and the form moves out. 问题是我正在该表单中使用jquery UI datepicker,并且当我移至小部件以选择日期时,将执行div的mouseleave事件,并且表单将移出。 I have tried to find a way to detect the mouseenter event to the datepicker but I can't find a way to avoid this. 我试图找到一种方法来检测对datepicker的mouseenter事件,但我找不到避免这种情况的方法。 Here is an example of the problem 这是问题的一个例子

The datepicker element is created outside of your div so doesn't carry the hover. datepicker元素是在div外部创建的,因此不会进行悬停。 It is created empty but display not yet set to "none", it is then filled in with the contents of the calendar when it first appears, after that it is simply set to display: none to hide. 它创建为空,但显示尚未设置为“ none”,然后在它第一次出现时用日历的内容填充,之后将其简单地设置为显示:无隐藏。

So I wanted to check for two situations, is the datepicker empty, or is it set to display: none. 因此,我想检查两种情况,即日期选择器为空,还是设置为显示:无。 This wrapped around your hide animation means the div cannot hide while the datepicker is open: 隐藏在隐藏动画中的这意​​味着打开日期选择器时div无法隐藏:

if ($('#ui-datepicker-div').html() == '' || $('#ui-datepicker-div').css('display') == 'none') {
    $(this).stop().animate({'marginRight':'-345px'},200);
}

Now there was a side effect of if you open the datepicker, then click somewhere off the pop-out div, it closes the datepicker but doesn't slide the div back in. This bit checks we're not over either the datepicker or the pop-out div and triggers the un-hover action: 现在有一个副作用,如果您打开日期选择器,然后单击弹出式div的某个位置,它将关闭日期选择器,但不会将div滑回。此位检查我们是否不在日期选择器或弹出div并触发取消悬停操作:

$(document).click(function () {
    if ($("#formulario:hover, #ui-datepicker-div:hover").length == 0) {
        $('#formulario').trigger('mouseout');
    }
});

Now though, if the datepicker was fading out on this click, it wasn't yet display: none; 现在,如果日期选择器在此单击上逐渐消失,则尚未显示。 so the mouseout didn't trigger the hide. 因此将鼠标移出并不会触发隐藏。 If it isn't exactly at opacity: 1 then it must be fading or already hidden, so I changed that if clause a bit more to: 如果不是完全不透明:1则它必须已经褪色或已经隐藏,因此我将if子句更改为:

if ($('#ui-datepicker-div').html() == '' || $('#ui-datepicker-div').css('display') == 'none' || $('#ui-datepicker-div').css('opacity') < 1) {
    $(this).stop().animate({'marginRight':'-345px'},200);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM