Jquery查找事件是否为滚动条点击

8
我有一个场景,当我点击 Div 块外的任何地方时,我会隐藏该 Div 块。
我正在使用Internet Explorer测试应用程序。如果没有滚动条,则我的代码可以正常工作。但是,如果Div块上有滚动条,当我单击滚动条时,它会认为滚动条不是 div 的一部分,并隐藏 div 块。我想让 div 块保持打开状态,即使用户单击滚动条并进行滚动操作。
var $container = $(".toolbarBlock");

 $(document).mouseup(function (e) {
        if (!$container.is(e.target) // if the target of the click isn't the container...
            && $container.has(e.target).length === 0) // ... nor a descendant of the container
        {
            toolbarClose();
        }
    });

 function toolbarClose() {
    return $.when(slideOut($container));
 }
2个回答

26
我希望发布答案以帮助那些遇到相同问题的人。
我使用了: e.target != $('html').get(0) // 不包括滚动条
var $container = $(".toolbarBlock");

$(document).mouseup(function (e) {
    if (!$container.is(e.target) // if the target of the click isn't the container...
        && ($container.has(e.target).length === 0) // ... nor a descendant of the container
        && (e.target != $('html').get(0))) // nor the scrollbar
    {
        toolbarClose();
    }
});

function toolbarClose() {
    return $.when(slideOut($container));
}

2
在jQuery中,滚动条没有单独的点击事件。但是可以使用.scroll()方法监听滚动事件并显示容器。因此,当用户点击滚动条时,元素会隐藏,但当他们滚动时,您可以再次显示该元素。虽然不是最理想的解决方案,但根据我的研究,这是唯一的选项。参考链接:http://forum.jquery.com/topic/click-event-for-scrollbarhttp://api.jquery.com/scroll/。请注意,要保留HTML标签。

我发现你的第一句话非常有帮助。谢谢! - Evik James

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接