jQuery detect mousedown inside an element and then mouseup outside the element(jQuery在元素内检测mousedown,然后在元素外检测mouseup)
问题描述
我有一些类似于绘图画布的东西,我在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery在元素内检测mousedown,然后在元素外检测mouseup


基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01