可调整大小的模态弹窗

4

我正在尝试通过单击增加和减少图标来使模态窗口可调整大小。每次增加/减少点击后,模态窗口还应该位于屏幕中央。

目前只有增加功能可用。请问我做错了什么?

this.increaseModal = function () {
        var maxHeight = ($(window).height() * 90) / 100;
        var maxWidth = ($(window).width() * 90) / 100;
        var height = ($(window).height() * 10) / 100;
        var width = ($(window).width() * 10) / 100;
        if ($('.modal-content').height() <= maxHeight - 100) {
            $('.modal-content').height($('.modal-content').height() + height);
            increaseHeightCount = increaseHeightCount + 1;
        }
        if ($('.modal-content').width() <= maxWidth - 100) {
            $('.modal-content').width($('.modal-content').width() + width);
            increaseWidthCount = increaseWidthCount + 1;
        }
        $('.modal-dialog').draggable();
        $('#myModal').addClass('outPopUp');
    }


this.decreaseModal = function () {
    var maxHeight = ($(window).height() * 90) / 100;
    var maxWidth = ($(window).width() * 90) / 100;
    var height = ($(window).height() * 10) / 100;
    var width = ($(window).width() * 10) / 100;
    if (increaseWidthCount > 0) {
        $('.modal-content').width($('.modal-content').width() - width);
        increaseWidthCount = increaseWidthCount - 1;
    }
    if (increaseHeightCount > 0) {
        $('.popup').height($('.popup').height() - height);
        increaseHeightCount = increaseHeightCount - 1;
    }
}

outPopUp 是我重写 bootstrap css 以更改模态窗口位置的类。

据我观察,在 DOM 浏览器中,.popup 没有分配给元素,因此增加/减少高度无法正常工作。


请参考@dsg的建议,创建一个fiddle或者(更好的方式)在你的问题中添加一个代码片段,这样我们就可以看到问题所在。请查看提问的正确姿势 - Mosh Feu
点击增加图标时,模态框宽度会增加,但高度不会增加。减少操作根本不起作用,且定位不会更改为屏幕中央。 - aka17
这是我刚刚创建的 JSFiddle:https://jsfiddle.net/569n1mh4/ - aka17
嗯,它没起作用。我会更新它。 - aka17
只需更新您的问题,不要在评论中放置您的fiddle链接。并非每个人都能看到它。 - choz
显示剩余2条评论
1个回答

3
jQuery已经拥有您需要完成此操作的功能。

$('.modal-content').resizable({
    //alsoResize: ".modal-dialog",
    //minHeight: 150
});
$('.modal-dialog').draggable();

$('#myModal').on('show.bs.modal', function () {
    $(this).find('.modal-body').css({
        'max-height':'100%'
    });
});
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="control-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </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>
  </div>
</div>

https://jsfiddle.net/p7o2mkg4/


1
仅供澄清,此功能是jQuery UI库的一部分。 - Gabe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接