2009-05-04 115 views
4

在IE中存在.setCapture(); .releaseCapture()函数。 在没有使用jQuery的情况下,Firefox中这些函数的等效功能是什么? (我的客户不想使用它)在Firefox中捕获鼠标

+1

你到底要使用这些功能呢?客户很可能想要一些行为,而不是功能,对吗? – 2009-05-04 19:41:34

回答

-3

FF/JavaScript中没有这样的功能。捕获函数仅存在于JScript中。

+0

我知道这一点。它也必须是FF的解决方案 – Cornel 2009-05-04 13:18:57

+0

Firefox从版本4开始添加setCapture和releaseCapture,请参阅[Chip Cressman的答案](http://stackoverflow.com/a/13829499/1421194)和[Kaj Dijkstra的答案](https: //developer.mozilla.org/samples/domref/mousecapture.html)。 – Sasha 2016-01-21 16:36:35

0

SetCapture和ReleaseCapture是您发现的IE propriatory功能。在Firefox中没有本地的方式来操作内容菜单。可以在http://www.codeplex.com/gimme/Wiki/Recent.aspx找到Gimme。这里有一篇博客文章:http://blog.stchur.com/2007/11/21/setcapture-with-gimme,其中描述了使用它来替换函数的一个场景。

+0

Firefox从版本4开始添加了setCapture和releaseCapture,请参见[Chip Cressman的回答](http://stackoverflow.com/a/13829499/1421194)和[Kaj Dijkstra的回答](https://developer.mozilla.org/samples /domref/mousecapture.html)。 – Sasha 2016-01-21 16:39:55

0

setCapture()和releaseCapture()是Internet Explorer特定的非标准方法。 Firefox中没有实现。有一个名为Gimme的框架,它提供了一些鼠标捕获功能。 http://www.codeplex.com/gimme/

+0

Firefox从版本4开始添加了setCapture和releaseCapture,请参见[Chip Cressman的回答](http://stackoverflow.com/a/13829499/1421194)和[Kaj Dijkstra的回答](https://developer.mozilla.org/samples /domref/mousecapture.html)。 – Sasha 2016-01-21 16:39:47

1

使用事件冒泡:将冒泡鼠标事件的事件侦听器添加到高级容器(可能甚至是document),并使用变量来追踪哪个元素应该是捕获的元素。

没有关于你想要做什么的进一步信息,没有什么可说的。

+0

我想实现一个页面分割条 – Cornel 2009-05-05 07:51:27

12

如上所述,Firefox不提供此功能,您可以通过监视整个文档上的事件来解决此问题。为了确保没有更好的技巧,我刚刚检查过jQuery UI,看起来他们使用相同的方法。因此,举例来说,如果你想捕捉鼠标移动时,鼠标在jQuery的了,你会怎么做:

$("#someElement"). 
    mousedown(function() { $(document).mousemove(captureMouseMove) }). 
    mouseup(function() { $(document).unbind("mousemove", captureMouseMove) }); 

function captureMouseMove(event) 
{ 
    // ... 
} 
+0

Firefox从版本4开始添加了setCapture和releaseCapture,请参见[Chip Cressman的回答](http://stackoverflow.com/a/13829499/1421194)和[Kaj Dijkstra的回答](https: //developer.mozilla.org/samples/domref/mousecapture.html)。 – Sasha 2016-01-21 16:39:12

2

捕捉随时鼠标不好的行为,我认为这就是为什么不提供setCapture

但是,要捕获鼠标进行拖放操作,只需要处理document对象的鼠标事件(鼠标{上,下,移动}),该对象甚至可以在拖动客户区。

<html> 
<head> 
    <title>Capture test</title> 
</head> 
<body> 
<script type="text/javascript"> 
    document.onmousedown = function() { 
     state.innerHTML = "Dragging started"; 
    }; 
    document.onmousemove = function (e) { 
     coord.innerHTML = e.clientX + ',' + e.clientY; 
    } 
    document.onmouseup = function (e) { 
     state.innerHTML = "Dragging stopped"; 
    } 
</script> 
<p id="state">.</p> 
<p id="coord">.</p> 
</body> 
</html> 
2

我相信element.setCapture()和document.releaseCapture()加入到Firefox作为FF4的: https://developer.mozilla.org/en/DOM/element.setCapture

“mouseDown事件的处理期间调用element.setCapture()方法将所有鼠标事件重定向到此元素,直到释放鼠标按钮或调用document.releaseCapture()。“

2

@ JanZich的解决方案效果很好,除非鼠标不在元素中时捕获鼠标事件。这个工作更好地为我:

$("#someElement").mousedown(function() { 
    $(document).mousemove(captureMouseMove); 
    $(document).mouseup(captureMouseUp); 
}); 

function captureMouseMove(event) { 
    console.log("mouse move"); 
}      

function captureMouseUp(event) { 
    console.log("mouse up"); 
    $(document).unbind("mousemove", captureMouseMove); 
    $(document).unbind("mouseup", captureMouseUp); 
}