Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jquery中创建一个可拖动的函数_Javascript_Jquery_Jquery Ui - Fatal编程技术网

Javascript 在jquery中创建一个可拖动的函数

Javascript 在jquery中创建一个可拖动的函数,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我做了一个函数,允许用户拖动身体内的任何元素。我遇到的问题是,当用户移动鼠标太快时,它会失去位置的跟踪。有更好的方法跟踪鼠标吗? 这是我的功能 $(this).on("mousedown",function(event){ //$(this).on("click",function(event){ $(this).css("cursor","move"); mouseX = event.pageX; mouseY = event.pageY;

我做了一个函数,允许用户拖动身体内的任何元素。我遇到的问题是,当用户移动鼠标太快时,它会失去位置的跟踪。有更好的方法跟踪鼠标吗? 这是我的功能

$(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 ) ;
        }
    });
});

它确实随光标移动,但不能随光标快速移动。

您知道jQueryUI已经实现了可拖动元素吗?是的,但我想做我自己的,并与其他功能结合。我想知道这把小提琴是否能帮助你:-使用我为这个问题编写的代码:哇,非常好。它是如此光滑。谢谢