how to implement mousemove while mouseDown pressed js

后端 未结 2 998
日久生厌
日久生厌 2021-02-14 09:23

I have to implement mouse move event only when mouse down is pressed.

I need to execute \"OK Moved\" only when mouse down and mouse move.

I used this code

<
2条回答
  •  长发绾君心
    2021-02-14 10:00

    Use the mosemove event.

    From mousemove and mouseover jquery docs:

    The mousemove event is sent to an element when the mouse pointer moves inside the element.

    The mouseover event is sent to an element when the mouse pointer enters the element.

    Example: (check console output)

    $(".floor").mousedown(function () {
        $(this).mousemove(function () {
            console.log("OK Moved!");
        });
    }).mouseup(function () {
        $(this).unbind('mousemove');
    }).mouseout(function () {
        $(this).unbind('mousemove');
    });
    

    https://jsfiddle.net/n4820hsh/

提交回复
热议问题