jQuery UI dialog box not positioned center screen

后端 未结 23 2951
失恋的感觉
失恋的感觉 2020-12-01 00:31

I have a jQuery dialog box that is meant to position in the middle of the screen. However, it seems slightly off-center vertically.

Here is the code:

         


        
相关标签:
23条回答
  • 2020-12-01 01:32

    You must add the declaration

    At the top of your document.

    Without it, jquery tends to put the dialog on the bottom of the page and errors may occur when trying to drag it.

    0 讨论(0)
  • 2020-12-01 01:33

    to position the dialog in the center of the screen :

    $('#my-selector').parent().position({
                        my: "center",
                        at: "center",
                        of: window
    });
    
    0 讨论(0)
  • 2020-12-01 01:35
    $('#dlg').dialog({
        title: 'My Dialog',
        left: (parseInt(jQuery(window).width())-1200)/2,
        top:(parseInt(jQuery(window).height())-720)/2,
        width: 1200,
        height: 720,
        closed: false,
        cache: false,
        modal: true,
        toolbar:'#dlg-toolbar'
    });
    
    0 讨论(0)
  • 2020-12-01 01:36

    None of the above solutions worked for me. I will present below my scenario and the final solution, just in case someone has the same problem.

    Scenario: I use a custom jQuery plugin to add a scroll bar to an HTML element that is located inside the Dialog box.

    I used it as

    $(response).dialog({
     create: function (event, ui) {
      $(".content-topp").mCustomScrollbar();
     })
    });
    

    The solution was to move it from create to open, like this:

    $(response).dialog({
     open: function (event, ui) {
      $(".content-topp").mCustomScrollbar();
      $(this).dialog('option', 'position', 'center');
     })
    });
    

    So, if you use any custom jQuery plugin that manipulates the content then call it using the open event.

    0 讨论(0)
  • 2020-12-01 01:37

    I had the same problem, which was fixed when I entered a height for the dialog:

    $("#dialog").dialog({
        height: 500,
        width: 800
    });
    
    0 讨论(0)
提交回复
热议问题