简体   繁体   English

当拖动子元素时,IE会为绝对定位的div生成虚假的鼠标单击事件

[英]IE generates spurious mouse click event for absolute-positioned div when dragging child element

In the jsFiddle below, click the link 'Click to show menu' to display an absolute-positioned div with a jScrollPane attached. 在下面的jsFiddle中,单击链接“单击以显示菜单”以显示带有jScrollPane的绝对位置的div。 Start dragging the scrollbar thumb but allow the mouse to wander a little to the right of the scrollbar, and then release the mouse. 开始拖动滚动条的拇指,但是允许鼠标在滚动条的右侧稍稍漂移,然后释放鼠标。 In IE (versions 8,9,10) a click event is generated on the document, which triggers our code to hide the menu. 在IE(版本8、9、10)中,文档上会生成一个click事件,这会触发我们的代码隐藏菜单。 In all other browsers I've tested (Firefox, Chrome, Safari) no such click event is generated on the document and the menu remains displayed (as desired). 在我测试过的所有其他浏览器(Firefox,Chrome,Safari)中,不会在文档上生成此类单击事件,并且菜单仍会显示(根据需要)。

In our web app, we want clicks outside the menu (ie, those that reach the document) to hide the menu. 在我们的Web应用程序中,我们希望菜单外的点击(即到达文档的那些点击)隐藏菜单。 However, we don't want the menu to be hidden as a side-effect of a drag initiated from within the scrollpane itself. 但是,我们不希望菜单隐藏为从滚动窗格自身内部发起的拖动的副作用。

Is there any simple workaround to avoid this issue? 有没有简单的解决方法来避免此问题? Can the jScrollPane be updated somehow to avoid the problem? 可以通过某种方式更新jScrollPane以避免该问题吗?

$(document).ready(function () {

    $('.scroll-pane').jScrollPane();

    $('#menu').click(function () {
        console.info('menu clicked');
        var api = $('.scroll-pane').show().data('jsp');
        api.reinitialise();

        return false;
    });

    $(document).click(function () {
        console.info('document clicked');
        $('.scroll-pane').hide();
    });

    $('.scroll-pane').bind('mousedown', function (ev) {
        console.info('scroll pane mousedown');
    }).bind('mouseup', function (ev) {
        console.info('scroll pane mouseup');
    }).bind('click', function (ev) {
        console.info('scroll pane click');
        return false;
    });
});

http://jsfiddle.net/catweazle/KWbhM/2/ http://jsfiddle.net/catweazle/KWbhM/2/

I encountered similar click event inconsistencies previously as well. 我以前也遇到过类似的click事件不一致问题。 It turns out that IE fires the click event for whatever element the mouse is over during mouseup , and not for the element it was over when the click first started with mousedown . 事实证明,IE会为mouseup期间鼠标悬停的任何元素触发click事件,而不是第一次单击mousedown时发生的元素触发click事件。

https://stackoverflow.com/a/4606960/17803 https://stackoverflow.com/a/4606960/17803

In other words - if you click down while over element A, but move mouse to element B, and then release, in IE you will get: 换句话说-如果您在元素A上方单击时将鼠标移至元素B,然后释放,则在IE中,您将获得:

  • mousedown for A A的mousedown
  • mouseup for B B的mouseup
  • click for B click B

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

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