listen to mouse hold event on website?

前端 未结 4 859
無奈伤痛
無奈伤痛 2020-12-15 09:16

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

4条回答
  •  天涯浪人
    2020-12-15 09:24

    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

提交回复
热议问题