在Internet Explorer 7中,body onmousemove或document.onmousemove事件只有在鼠标位于浏览器窗口内时才会触发,而不是在鼠标移动到窗口外时触发。但是在Firefox中,当我把鼠标移到窗口外时,onmousemove
事件被正确地调用。
我该如何在IE中设置一个事件,在浏览器窗口外被调用?
Google Maps在IE中可以实现这一点。如果您按住鼠标并将其移动到浏览器窗口外部,您会发现地图仍然在移动。
在Internet Explorer 7中,body onmousemove或document.onmousemove事件只有在鼠标位于浏览器窗口内时才会触发,而不是在鼠标移动到窗口外时触发。但是在Firefox中,当我把鼠标移到窗口外时,onmousemove
事件被正确地调用。
我该如何在IE中设置一个事件,在浏览器窗口外被调用?
Google Maps在IE中可以实现这一点。如果您按住鼠标并将其移动到浏览器窗口外部,您会发现地图仍然在移动。
mousemove
,用户可以自由地在窗口外部拖动。很好。mousemove
以模拟拖动:elementToDrag.unselectable = "on";
elementToDrag.onselectstart = function(){return false};
elementToDrag.style.userSelect = "none"; // w3c standard
elementToDrag.style.MozUserSelect = "none"; // Firefox
setCapture
(并在鼠标释放时使用releaseCapture
进行相反操作)。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple drag demo</title>
<style>
#dragme {
position:absolute;
cursor:move;
background:#eee;
border:1px solid #333;
padding:10px;
}
</style>
<script>
function makeDraggable(element) {
/* Simple drag implementation */
element.onmousedown = function(event) {
document.onmousemove = function(event) {
event = event || window.event;
element.style.left = event.clientX + 'px';
element.style.top = event.clientY + 'px';
};
document.onmouseup = function() {
document.onmousemove = null;
if(element.releaseCapture) { element.releaseCapture(); }
};
if(element.setCapture) { element.setCapture(); }
};
/* These 3 lines are helpful for the browser to not accidentally
* think the user is trying to "text select" the draggable object
* when drag initiation happens on text nodes.
* Unfortunately they also break draggability outside the window.
*/
element.unselectable = "on";
element.onselectstart = function(){return false};
element.style.userSelect = element.style.MozUserSelect = "none";
}
</script>
</head>
<body onload="makeDraggable(document.getElementById('dragme'))">
<div id="dragme">Drag me (outside window)</div>
</body>
</html>
这正是Google地图所做的(我在2004年反向工程谷歌地图时发现的,当时它首次发布)。
1我认为只有在对文本节点进行拖动操作(即mousedown
)时才会出现问题。元素/容器节点不会表现出相同的行为,可以在文档内或外拖动,只要用户在元素的“空白”部分按下鼠标即可。
2同样适用于文本节点的拖动启动。
mousedown
后跟随一个mouseup
的情况下,应该会弹出一个提示框。因此,在对文档进行mousedown
后,如果用户将鼠标拖动到窗口外并在窗口外释放,则应弹出提示框。我该怎么做?我不太理解你的代码,因为它涉及选择和拖动等内容... - SexyBeast