简体   繁体   中英

jQuery's draggable() function isn't working

Here's a JSFiddle I have: http://jsfiddle.net/o5tupLcb/ .

My problem is, why isn't the div dragging? Even if I make $(".drag").draggable(); its own statement outside of the switch statements, it doesn't work.

Here's some jQuery:

$(document).ready(function() {
    $(".drag").mousedown(function(e) {
        switch (e.which) {
        case 1:
            $(this).toggleClass("mousedown");
            $(this).draggable(); /* This probably shouldn't be here */
        case 2:
            break;
        case 3:
            break;
        default:
            break;
        }
    });

    $(".drag").mouseup(function(e) {
        switch (e.which) {
        case 1:
            $(this).toggleClass("mousedown");
            $(this).draggable(); /* This probably shouldn't be here */
        case 2:
            break;
        case 3:
            break;
        default:
            break;
        }
    });
});

Please note that I already tried to import jQuery UI and nothing happened.

Edit: Turns out I was importing the wrong thing. Also, the draggable() function works better when I take it out of the switch statements and just put it below.

Cleaned up your code on jsfiddle, included jQuery and jQuery UI. Also changed html to be only:

<div class="drag">Hello</div>

Now works (after first click). This makes sense since the event is fired after you first click (and again after you release).

http://jsfiddle.net/o5tupLcb/1/

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