【问题标题】:Jquery Find if Event is a scrollbar clickJquery查找事件是否为滚动条点击
【发布时间】:2014-11-12 00:54:44
【问题描述】:

如果我点击 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));
 }

【问题讨论】:

    标签: jquery click scrollbar mouseup


    【解决方案1】:

    jQuery 中没有滚动条的单击事件。 http://forum.jquery.com/topic/click-event-for-scrollbar

    但是有一个 .scroll() http://api.jquery.com/scroll/

    您可以监听滚动事件并显示容器。

    因此,一旦他们单击该栏,该元素就会隐藏,但当他们滚动时,您可以再次显示该元素。

    不理想,但根据我的研究,这是唯一的选择。

    【讨论】:

    • 我发现你的第一句话很有帮助。谢谢!
    【解决方案2】:

    我想发布答案,以便对遇到相同问题的其他人有所帮助。

    我用过:

    e.target != $('html').get(0) // nor the scrollbar

    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));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多