简体   繁体   中英

JQuery - Clicking and dragging divs on a screen WITHOUT draggable

I'm currently working on a small project, with draggable divs. The code I've created seems to not be working, and it causes JQuery to stop responding.

Is there a nice and easy way to do this without using .draggable ?

var hold = false;
        //Drag Elements

        $('#example').mousedown(function(){
            hold = true;
        });

        $('#example').mouseup(function(){
            hold = false;
        });

        $(document).on('mousemove', function(e){
            $('#example').css({
                while(hold){
                    left:   e.pageX-50,
                    top:    e.pageY-50
                }
            });
        });

Thanks

Since Javascript is single-threaded, your while loop never exits, because the mouseup handler can't run while you're stuck in the mousemove handler.

Change while to if :

    $(document).on('mousemove', function(e){
        if (hold) {
            $('#example').css({
                left:   e.pageX-50,
                top:    e.pageY-50
            });
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM