Getting mouse position while dragging (JS + HTML5)

前端 未结 2 1723
囚心锁ツ
囚心锁ツ 2021-02-05 07:04

I\'m currently implementing a small demo app trying to get my head around drag and drop with HTML5. What I\'m trying to do at the moment is getting the position of the cursor wh

2条回答
  •  梦如初夏
    2021-02-05 07:51

    document.ondragover = function(evt) {
        evt = evt || window.event;
        var x = evt.pageX,
            y = evt.pageY;
    
        console.log(x, y);
    }
    

    jsFiddle.

    If you were using jQuery...

    $(document).on('dragover', function(evt) {
        var x = evt.pageX,
            y = evt.pageY;
    
        console.log(x, y);
    
    });
    

提交回复
热议问题