'onmousedrag' event js

前端 未结 5 936
情深已故
情深已故 2020-12-18 11:24

I have some code that works each time onmouseclick and continuously onmousemove when I set them accordingly. I am looking for a way to combine the two (i.e. like a click and

5条回答
  •  情深已故
    2020-12-18 11:45

    If you don't want to use global variable for dragging, you can use event.buttons for determining which mouse button is pressed.

    I tested this code in Chrome and Firefox.

    document.onmousemove = function(event) {
        if(event.buttons == 1) { //dragged with left mouse button
            //your code
        }
    
        if(event.buttons > 0) { //dragged with any mouse button
            //your code
        }
    }
    

提交回复
热议问题