2017-09-15 77 views
0

我在此處使用Bootstrap的默認模式彈出式菜單,使用JQuery UI可拖動功能,如以下代碼中所示,代碼如下:JS fiddle如何使用鍵盤箭頭鍵移動引導模式彈出式菜單

$(document).ready(function() { 
 

 
    $("#btnTest").click(function() { 
 
    $('.modal').modal({ 
 
     keyboard: false, 
 
     show: true 
 
    }); 
 
    // Jquery draggable 
 
    $('.modal-dialog').draggable({ 
 
     handle: ".modal-header" 
 
    }); 
 
    }); 
 

 

 
});
.modal-header { 
 
    cursor: move; 
 
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
 
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> 
 

 
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
 

 
<div> 
 
    <div class="modal fade"> 
 
    <div class="modal-dialog"> 
 
     <div class="modal-content"> 
 
     <div class="modal-header"> 
 
      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
 
      <h4 class="modal-title">Modal title</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
      <p>One fine body&hellip;</p> 
 
     </div> 
 
     <div class="modal-footer"> 
 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
      <button type="button" class="btn btn-primary">Save changes</button> 
 
     </div> 
 
     </div> 
 
     <!-- /.modal-content --> 
 
    </div> 
 
    <!-- /.modal-dialog --> 
 
    </div> 
 
    <!-- /.modal --> 
 
</div> 
 
<div> 
 
    <h4> 
 
Draggable Modal Demo by Vibs 
 
</h4> 
 
    <input type="button" id="btnTest" value="Show Popup" /> 
 
</div>

有人可以幫我實現的功能,這將讓我通過向上/下/左/右的鍵盤上的方向鍵移動模式彈出?

回答

1

$(document).ready(function() { 
 

 
    $("#btnTest").click(function() { 
 
    $('.modal').modal({ 
 
     keyboard: false, 
 
     show: true 
 
    }); 
 
    // Jquery draggable 
 
    $('.modal-dialog').draggable({ 
 
     handle: ".modal-header" 
 
    }); 
 
    }); 
 

 

 
$(document).keydown(function(e){ 
 
    switch (e.which){ 
 
    case 37: //left arrow key 
 
     $(".modal-dialog").finish().animate({ 
 
      left: "-=50" 
 
     }); 
 
     break; 
 
    case 38: //up arrow key 
 
     $(".modal-dialog").finish().animate({ 
 
      top: "-=50" 
 
     }); 
 
     break; 
 
    case 39: //right arrow key 
 
     $(".modal-dialog").finish().animate({ 
 
      left: "+=50" 
 
     }); 
 
     break; 
 
    case 40: //bottom arrow key 
 
     $(".modal-dialog").finish().animate({ 
 
      top: "+=50" 
 
     }); 
 
     break; 
 
    } 
 
}); 
 

 
});
.modal-header { 
 
    cursor: move; 
 
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
 
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> 
 

 
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
 

 
<div> 
 
    <div class="modal fade"> 
 
    <div class="modal-dialog"> 
 
     <div class="modal-content"> 
 
     <div class="modal-header"> 
 
      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
 
      <h4 class="modal-title">Modal title</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
      <p>One fine body&hellip;</p> 
 
     </div> 
 
     <div class="modal-footer"> 
 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
      <button type="button" class="btn btn-primary">Save changes</button> 
 
     </div> 
 
     </div> 
 
     <!-- /.modal-content --> 
 
    </div> 
 
    <!-- /.modal-dialog --> 
 
    </div> 
 
    <!-- /.modal --> 
 
</div> 
 
<div> 
 
    <h4> 
 
Draggable Modal Demo by Vibs 
 
</h4> 
 
    <input type="button" id="btnTest" value="Show Popup" /> 
 
</div>

+0

非常感謝。儘管有一件事我不能理解,即「左:」 - = 50「'?你能解釋一下爲什麼它在報價中,以及它在理論上如何工作? – vibs2006

+1

@ vibs2006因爲對於animate()方法,所有的屬性名稱必須是駝峯式的,值應該在引號內。 「 - = 50」它被稱爲相對值(相對於元素的當前值)。這是通過將+ =或 - =放在值的前面來完成的。 –

相關問題