Draggable element on ctrl

时间:2015-11-12 11:21:08

标签: jquery jquery-ui-draggable

How can I let an element be draggable only when CTRL key is pressed? I tried to read documentation but it looks like there is no such option.

2 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
   $("#draggable").draggable();
   $("#draggable").draggable("disable");
   $(window).keydown(function(e) {
       if(e.keyCode == 17) {
            $("#draggable").draggable("enable");
       }
   }).keyup(function(e){
       if(e.keyCode == 17) {
           $("#draggable").draggable("disable");
       }
   });
});

This will work. When you let go of control, you can't move it anymore.

JSFiddle: http://jsfiddle.net/1wjbnqm2/

答案 1 :(得分:1)

I have a fiddle showing my solution:

var box_dragOps = { 
        start : box_start_drag,
        drag  : box_dragging,
        stop : box_stop_drag
    };

$('#box').draggable(box_dragOps);


function box_start_drag(e, ui) {
    if(!e.ctrlKey)
        return false;
}

function box_dragging(e,ui) {

}

function box_stop_drag(e, ui) {

}