3

我有一个显示 onclick 链接的 div,当鼠标在 div 外部单击时我想隐藏(类似于大多数模式框功能) - 问题是当用户使用浏览器滚动条时,这被认为是单击并隐藏 div

这是我用来显示 div

$('.trigger').click(function(e){
    e.preventDefault();
    open_slideout(this);
});

function open_slideout(el){
    $(document).unbind('mousedown');

    //code here to display the div if its not already shown

    //close on click-out
    $(document).bind('mousedown',function(){
        $(panel_id).removeClass('active').hide('fast');
        $(el).removeClass('active');
    });
    $('.panel.active').bind('mousedown',function(e){e.stopPropagation();});
    $('.trigger').bind('mousedown',function(e){e.stopPropagation();});
}

所以如果他们在活动区域​​内单击,我已经设置了 stopPropagation,但就像我说的,如果他们使用滚动条,它会隐藏 div

4

2 回答 2

3

这似乎成功了:

$(document.body).bind('mousedown',function(){
于 2011-04-21T16:24:17.577 回答
0
$(window).scroll(function(){
   scrolling = true;
});

/*other code */
$(document).bind('mousedown',function(){
if(!scrolling){
        $(panel_id).removeClass('active').hide('fast');
        $(el).removeClass('active');
}
    }).bind('mouseup',function(){ scrolling = false; })
/*other code */
于 2011-04-20T18:50:48.533 回答