jquery - 可调整大小的模态弹出窗口

标签 jquery html angularjs twitter-bootstrap bootstrap-modal

我试图通过单击增加和减少图标来调整模态窗口的大小。每次点击 increase/decrease 后,模态窗口也应位于屏幕中间。

到目前为止,只有增加是有效的。谁能阐明我做错了什么?

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 未分配给元素,因此增加/减少高度不起作用。

最佳答案

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/

关于jquery - 可调整大小的模态弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408969/

相关文章:

javascript - Jquery:检查第一个实例

jquery - 选择所有没有特定类别的复选框

asp.net - <asp :GridView> width, <div> 宽度

html - 使用嵌入式 Ruby 代码 (erb) 缩进 HTML

javascript - 使用jquery检查特殊字符

javascript - 将鼠标悬停在一个 div 上以显示某个类的最近 div

javascript - 最佳实践 ?消息显示应该是服务还是指令?

AngularJS 类似于 jQuery PreventDefault for keydown?

ios - Angular JS $http.get 从 WP REST API 返回空数据,但仅在实际 iOS 设备上

javascript - 单击第二个后取消选中第一个过滤器