问题描述
我有一些类似于绘图画布的东西,我在 mouseup 上捕获它的状态以用于撤消目的.画布不是全屏的,因此您可以使用画笔绘制并在画布外释放.像这样的:
I have something similar to a drawing canvas, and I capture it's state on mouseup for undo purposes. The canvas isn't full screen, so you can draw with a brush and release outside the canvas. Something like this:
$("#element").mousedown(function(){
$(document).mouseup(function(){
//do something
});
});
但这当然行不通.普通的 $(document).mouseup 也不起作用,因为我还有许多其他 UI 元素,并且每次单击 UI 元素时它都会保存状态.
But this doesn't work of course. A plain $(document).mouseup doesn't work either, because I have many other UI elements and it saves the state each time you click on a UI element.
有什么想法吗?
推荐答案
var isDown = false;
$("#element").mousedown(function(){
isDown = true;
});
$(document).mouseup(function(){
if(isDown){
//do something
isDown = false;
}
});
为了简单起见,我将 isDown
放在全局命名空间中.在生产中,您可能希望隔离该变量的范围.
For the sake of simplicity I put isDown
in the global namespace. In production you would probably want to isolate the scope of that variable.
这篇关于jQuery在元素内检测mousedown,然后在元素外检测mouseup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!