Jquery draggable with bootstrap modal,scrolller奇怪的行为

时间:2014-11-25 07:03:06

标签: javascript twitter-bootstrap jquery-ui

我尝试使用带有jquery-ui可拖动功能的bootstrap模式弹出窗口。 我这样用它:

        // Bootstrap modal
        $(element).modal({ keyboard: false,
                           show: value
        });
        // Jquery draggable
        $(element).draggable({
            handle: ".modal-header"
        });

但是当我尝试拖动弹出窗口时右侧滚动条正在拖动弹出窗口。 任何进步都是如此。

2 个答案:

答案 0 :(得分:34)

我认为您应该在draggable课程上应用.modal-dialog,请参阅:

  
<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>

<script>
        $('.modal').modal({ keyboard: false,
                           show: true
        });
        // Jquery draggable
        $('.modal-dialog').draggable({
            handle: ".modal-header"
        });
</script>  

答案 1 :(得分:0)

@Bass Jobsen的好回答

但是我觉得有时我们可能需要动态调整模态的大小。所以这里扩展了Bass的可调整大小和可拖动模态的解决方案

添加

<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" />

$('.modal-content').resizable();

&#13;
&#13;
  
&#13;
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" />
<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>

<script>
        $('.modal').modal({ keyboard: false,
                           show: true
        });
        // Jquery draggable
        $('.modal-dialog').draggable({
            handle: ".modal-header"
        });
        $('.modal-content').resizable();

</script>  
&#13;
&#13;
&#13;

此外,您可以指定minHeight和minWidth

$('.modal-content').resizable({
    minHeight: 100,
    minWidth: 100
});