简体   繁体   English

jQuery UI - 防止在父 div 之外调整对话框的大小

[英]jQuery UI - prevent resizing of dialog outside the parent div

PROBLEM: When doing resize on dialog, I want to prevent it from going outside parent div dialog-container .问题:在对话框上调整大小时,我想防止它超出父 div dialog-container For some reason containment is not working as I expected.出于某种原因, containment没有像我预期的那样工作。 What else can I try?我还能尝试什么?

在此处输入图片说明

HTML: HTML:

<div id="top"></div>
<div id="dialog-container">
  <div id="dialog">My dialog</div>
</div>

JS: JS:

$(document).ready(function() {
    jQuery("#dialog").dialog({
                            autoOpen:true,
                            modal: false,
                            resizable: true,
                            draggable: true,
                            closeOnEscape: true,
                            title: "Title",
        open: function(){
            jQuery('.ui-widget-overlay').bind('click',function(){
                jQuery('#dialog').dialog('close');
            })
        }
    }).parent().draggable({
        containment: '#dialog-container'
    }).resizable({
        containment: '#dialog-container'
    });
});

JSFIDDLE: https://jsfiddle.net/4zfmbktr/ JSFIDDLE: https ://jsfiddle.net/4zfmbktr/

First, I would STRONGLY advise moving to a newer jQuery UI Library.首先,我强烈建议迁移到更新的 jQuery UI 库。 I found a lot of strange issues with the jQuery UI 1.8.18 selected in your Fiddle.我发现在您的 Fiddle 中选择的 jQuery UI 1.8.18 有很多奇怪的问题。

One of the things I found was that it was ignoring options applied to the resizable.我发现的一件事是它忽略了应用于可调整大小的options If you read this article , it talks about how to set this option.如果您阅读了这篇文章,它会讨论如何设置此选项。 Jump top the answer by Jason C. So that is where I started:跳到 Jason C 的回答上面。所以这就是我开始的地方:

JavaScript JavaScript

$(function() {
  $("#dialog").dialog({
    autoOpen: true,
    modal: false,
    resizable: false,
    draggable: true,
    closeOnEscape: true,
    title: "Title",
    open: function() {
      $('.ui-widget-overlay').bind('click', function() {
        $('#dialog').dialog('close');
      })
    }
  });
  var ui = $("#dialog").closest(".ui-dialog");
  ui.draggable("option", "containment", '#dialog-container');
  ui.resizable("option", "containment", '#dialog-container');
});

This did not work.这没有用。 Draggable containment worked, but resize containment failed HARD.可拖动遏制工作,但调整遏制失败硬。 I blame 1.8.18.我责怪1.8.18。 I might test this again with the modern 1.12.1 just to see.我可能会用现代的 1.12.1 再次测试这个,只是为了看看。

This does not mean you cannot use 1.8.18, if you can't change your library, here is a work around.这并不意味着你不能使用 1.8.18,如果你不能改变你的库,这里有一个变通方法。 There are caveats here.这里有一些注意事项。

Working example: https://jsfiddle.net/Twisty/2vaf3dr5/39/工作示例: https : //jsfiddle.net/Twisty/2vaf3dr5/39/

JavaScript JavaScript

$(function() {
  $("#dialog").dialog({
    autoOpen: true,
    modal: false,
    resizable: false,
    draggable: true,
    closeOnEscape: true,
    title: "Title",
    open: function() {
      $('.ui-widget-overlay').bind('click', function() {
        $('#dialog').dialog('close');
      })
    }
  });
  var ui = $("#dialog").closest(".ui-dialog");
  ui.draggable("option", "containment", '#dialog-container');
  ui.resizable({
    handles: "n, e, s, w, se",
    minHeight: 150,
    minWidth: 150,
    resize: function(e, ui) {
      var contPos = $("#dialog-container").position();
      contPos.bottom = contPos.top + $("#dialog-container").height();
      contPos.right = contPos.left + $("#dialog-container").width();
      contPos.height = $("#dialog-container").height();
      contPos.width = $("#dialog-container").width();
      if (ui.position.top <= contPos.top) {
        ui.position.top = contPos.top + 1;
      }
      if (ui.position.left <= contPos.left) {
        ui.position.left = contPos.left + 1;
      }
      if (ui.size.height >= contPos.height) {
        ui.size.height = contPos.height - 7;
      }
      if (ui.size.width >= contPos.width) {
        ui.size.width = contPos.width - 7;
      }
    }
  });
});

I stripped away the pre-configured resizable option in dialog and wrote out the options directly.我去掉了对话框中预先配置的可调整大小选项,直接写出了选项。 Again, containment didn't work here, so I had to make my own custom resize logic.同样,遏制在这里不起作用,所以我必须制定自己的自定义调整大小逻辑。 In the end, this does what you'd expect.最后,这符合您的期望。

One of the oddities or caveats, is that it's reading mouse movement and will continue to do so even when the mouse has exceeded the boundaries.奇怪之处或警告之一是,它正在读取鼠标移动,即使鼠标超出边界,它也会继续这样做。 So the Top and Left stop... but the width and height continue to grow.所以顶部和左侧停止......但宽度和高度继续增长。 I do not know why and you know where I will point the finger.我不知道为什么,你知道我会指向哪里。

I did try switching your libraries and the resize is... a bit better.我确实尝试过切换你的库,调整大小是......好一点。 Still odd with height but not with width.高度仍然奇怪,但宽度不奇怪。 See here: https://jsfiddle.net/Twisty/2vaf3dr5/44/见这里: https : //jsfiddle.net/Twisty/2vaf3dr5/44/

That should get you going, I hope it helps.这应该能让你继续前进,我希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM