【问题标题】:How to Move a Bootstrap Modal Popup using Keyboard Arrow Keys如何使用键盘箭头键移动引导模式弹出窗口
【发布时间】:2017-09-15 19:12:37
【问题描述】:

我在这里使用带有 JQuery UI 可拖动功能的 Bootstrap 的默认模态弹出窗口,如下面的 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>

有人可以帮我启用可以让我通过键盘的向上/向下/向左/向右箭头键移动模式弹出窗口的功能吗?

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap bootstrap-modal


    【解决方案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>

    【讨论】:

    • 非常感谢。虽然我无法理解一件事,即left: "-=50"?你能解释一下为什么它在引号中以及它在理论上是如何工作的吗?
    • @vibs2006 因为对于 animate() 方法,所有属性名称都必须是驼峰式大小写,并且值应该在引号内。 “-=50” 称为相对值(该值相对于元素的当前值)。这是通过将 += 或 -= 放在值前面来完成的。
    猜你喜欢
    • 2012-07-25
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多