在网站上听鼠标举办活动? [英] listen to mouse hold event on website?

查看:74
本文介绍了在网站上听鼠标举办活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道'mousedown'是用户按下鼠标时,'mouseup'是用户释放鼠标时。但是我想在用户按下鼠标并按住它直到它释放后听这个事件。有什么想法?

I know 'mousedown' is when user press the mouse, 'mouseup' is when user release the mouse. But I want to listen the event after user press the mouse and hold it until it release. Any ideas?

推荐答案

如果你想要保持状态那么它就是你所处的状态在 mousedown 事件状态一段时间。当您按 mousedown 而不是 mouseup 时,会出现此状态。因此,你需要一个记录事件当前状态的变量。

If you want the hold state then it will be the state when you are in mousedown event state for a while. This state exists when you press mousedown but not mouseup. Hence you need to take a variable which records the current state of the event.

JS

$('div').on('mousedown mouseup', function mouseState(e) {
    if (e.type == "mousedown") {
        //code triggers on hold
        console.log("hold");
    }
});

工作小提琴

Working Fiddle

这篇关于在网站上听鼠标举办活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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