jquery dialog height & width issue

坚强是说给别人听的谎言 提交于 2020-01-23 16:56:13

问题


initially i am showing jquery dialog with fixed height and width at center of page. now i want to put a div with lots of html content inside the dialog. now i want to dynamically increase dialog height & width according to the div height & width with animate function but i want that dialog should stick at the center of page.

when dialog open first time at center of page and later if i change the height & width then dialog is not position at the center of page. so help me with concept that how to force dialog stick at center of page when height & width will be increasing with animate function.

this way i am working with dialog....here is my small code

 $(document).ready(function () {

         $("#dialog").dialog({
             autoOpen: false,
             bgiframe: true,
             height: 85,
             width: 330,
             modal: false,
             draggable: true,
             resizable: false,
             position: 'center',
             show: {
                 effect: "fade",
                 duration: 1000
             },
             hide: {
                 effect: "fade",
                 duration: 500
             },
             open: function (type, data) {
                 $(this).parent().appendTo("form");
             }
         });
         });

if possible then please show me the trick with sample code. thanks


回答1:


OK, would this work, assuming nothing about how the element is resized?

$(#dialog).resize(function(){
    var $host = $("#idOfParent");
    var hostHeight = $host.Height();
    var hostWidth = $host.Width();
    // center dialog on screen
    this.left = (hostWidth - this.width) / 2
    this.top= (hostHeight - this.height) / 2
});

http://api.jquery.com/resize/




回答2:


You can try with this: DEMO

This automatically increases the height of the dialog dynamically

HTML:

<div id="dialog">
    <div id="body">I'm a dialog</div>
</div>

Script:

<script>
     $('#dialog').dialog(
      "resize", "auto"
     );
     $('#dialog').dialog();
     setTimeout(function() {
        $('#body').append("Hello!<br>");
        setTimeout(arguments.callee, 1000);
      }, 1000);
</script>



回答3:


Rather than put javascript watchers on the div, you can simply use the 'minHeight' option for the dialog.

http://jqueryui.com/demos/dialog/ -> Options

That makes your code look something like this:

 $("#dialog").dialog({
         autoOpen: false,
         bgiframe: true,
         minHeight: 85,
         width: 330,
         modal: false,
         draggable: true,
         resizable: false,
         position: 'center',
         show: {
             effect: "fade",
             duration: 1000
         },
         hide: {
             effect: "fade",
             duration: 500
         },
         open: function (type, data) {
             $(this).parent().appendTo("form");
         }
     });


来源:https://stackoverflow.com/questions/10384712/jquery-dialog-height-width-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!