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


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01