用if语句检查是否mousedown? [英] Check if mousedown with an if statement?

查看:160
本文介绍了用if语句检查是否mousedown?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以这样做:

if ($(this).mousedown() == true) {

我认为这样可行,但事实并非如此。

I thought that would work but it doesn't.

其他详细信息:当鼠标离开特定的 DIV 时,我正在尝试检查鼠标按钮是否已关闭,所以如果此人在鼠标离开div时按住鼠标按钮,请执行此操作,否则执行此操作。

Additional details: I'm trying to check if the mouse button is down when the mouse leaves a specific DIV, so if the person is holding the mouse button down while their mouse leaves the div, do this, otherwise do that.

推荐答案

最简单我能想到的方法是将 mousedown mouseup 事件监听器绑定到文档并相应地更新全局变量。在元素的 mouseout 事件中,您可以检查该变量的状态并根据需要采取相应措施。 (注意:这假设你不关心鼠标是否在 div 之上被按下了......你将不得不澄清你的问题那个)。

The easiest way I can think of is to bind mousedown and mouseup event listeners to the document and update a global variable accordingly. In the mouseout event of your element you can check the state of that variable and act as appropriate. (Note: this assumes that you don't care whether or not the mouse was pressed down while over the div or not... you'll have to clarify your question around that).

var down = false;
$(document).mousedown(function() {
    down = true;
}).mouseup(function() {
    down = false;  
});
$("#example").mouseout(function() {
    if(down) {
        console.log("down");  
    } 
    else {
        console.log("up");   
    }
});

这是以上的工作示例

这篇关于用if语句检查是否mousedown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆