悬停时检查鼠标按钮是否按下? [英] Check if mouse button is down while hovering?

查看:50
本文介绍了悬停时检查鼠标按钮是否按下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上有一个与用户互动的盒子网格.如果他们单击一个框,它将更改颜色.有很多盒子,我希望它不那么乏味,因此最好具有以下功能:如果按下鼠标按钮并将鼠标悬停在盒子上,它将改变状态.有什么想法吗?

I have a grid of boxes that a user interacts with on a website. If they click a box, it changes color. There are quite a lot of boxes and I'd like it to be less tedious, so it would be nice to have the functionality be: if mouse button is down and you hover the box, it changes states. Any thoughts?

推荐答案

您可以使用

You can use the buttons property on the event passed to the hover callback to check what mouse buttons were pressed when the event was triggered.

例如,要检测用鼠标输入元素时是否按下了左键,可以使用:

For example, to detect whether the left button was pressed when an element is entered with the mouse, you could use:

myElement.addEventListener("mouseover", function(e){
    if(e.buttons == 1 || e.buttons == 3){
        //do some stuff
    }
})

以下是此想法的演示: http://jsfiddle.net/Ah6pw/

Here is a demonstration of this idea: http://jsfiddle.net/Ah6pw/

按住鼠标左键,然后在不同的列表项中移动鼠标.

Hold down the left mouse button and move your mouse through different list items.

这篇关于悬停时检查鼠标按钮是否按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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