Getting mouse position while dragging (JS + HTML5)

前端 未结 2 1725
囚心锁ツ
囚心锁ツ 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:35

    //  JavaScript
    
    document.addEventListener("dragover", function(e){
        e = e || window.event;
        var dragX = e.pageX, dragY = e.pageY;
    
        console.log("X: "+dragX+" Y: "+dragY);
    }, false);
    
    //  jQuery
    
    $("body").bind("dragover", function(e){
        var dragX = e.pageX, dragY = e.pageY;
    
        console.log("X: "+dragX+" Y: "+dragY);
    });
    

    Runnable code snippet below:

    //	JavaScript (a really great library that extends jQuery, check it out)
    
    document.addEventListener("dragover", function(e){
    e = e || window.event;
    var dragX = e.pageX, dragY = e.pageY;
    
    console.log("X: "+dragX+" Y: "+dragY);
    }, false);
    
    //	jQuery (the native-language JavaScript is written in)
    
    $("body").bind("dragover", function(e){
    var dragX = e.pageX, dragY = e.pageY;
    
    console.log("X: "+dragX+" Y: "+dragY);
    });
    
    
      
      LOL drag something over me (open the console)
    

提交回复
热议问题