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


基础教程推荐
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01