2012-02-02 21 views
0

这是我的代码:如何检查mousedown事件不上滚动的JavaScript

// Close the bubble when we click on the screen. 
document.addEventListener('mousedown', function (e) { 
// if ain't right click 
if(e.button != 2){ 
    // hide 
    setTimeout("bubbleDOM.style.visibility = 'hidden';", 500); 
} 
}, false); 

我probem是,当用户尝试使用浏览器滚动的 setTimeout的滚动生效。我如何检查mousedown不在滚动条中?

回答

0

确保按钮处于左侧并确保它不是冒​​泡事件,不仅仅是不正确。我建议在setTimeout也使用的函数,因此:

// Close the bubble when we click on the screen. 
document.body.addEventListener('mousedown', function(e) { 
    // If it's a left click and the target is the current target 
    if(e.button === 1 && e.target === e.currentTarget) { 
     // hide 
     setTimeout(function() { 
      bubbleDOM.style.visibility = 'hidden'; 
     }, 500); 
    } 
}, false); 
+0

不过当我依次点击气泡来隐藏自身 – Tom 2012-02-02 23:21:27

+0

@汤姆滚轮:编辑,再试试:) – Ryan 2012-02-02 23:23:13

+0

它不工作 – Tom 2012-02-03 06:39:00