0

I made a function that allow user to drag any element inside body. The problem I am having is when user move the mouse too fast, it lost track of the position. Is there a better way to track the mouse ? here is my functions

$(this).on("mousedown",function(event){
    //$(this).on("click",function(event){       
    $(this).css("cursor","move");
    mouseX = event.pageX;
    mouseY = event.pageY;
    xx = mouseX - $(this).position().left;
    yy = mouseY - $(this).position().top;

    console.log(" mouseX : " +mouseX+ " mouseY : " +mouseY  ) ;
    console.log(" XX : " +xx+ " YY : " +yy  ) ;

    $(this).on("mousemove",function(event){

        startX = event.pageX;
        startY =  event.pageY;
        console.log("after move page X : " +startX+ " page Y : " +startY  ) ;
        console.log("before left : " + $(this).position().left + " top : " + $(this).position().top ) ;
        dragging = true;    
        if(dragging){
            //this.style.top =  startY  - yy + 'px';
            //this.style.left = startX  - xx + 'px';
            //  $(this).animate({"left":(startX  - xx),"top":(startY  - yy)},0);
            $(this).css("top",(startY  - yy));
            $(this).css("left",(startX  - xx));

            console.log("after left : " + this.style.left + " top : " + this.style.top ) ;
        }
    });
});

it did move with the cursor but can't follow up with fast movement.

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59

0 Answers0