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:
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.
to position the dialog in the center of the screen :
$('#my-selector').parent().position({
my: "center",
at: "center",
of: window
});
$('#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'
});
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.
I had the same problem, which was fixed when I entered a height for the dialog:
$("#dialog").dialog({
height: 500,
width: 800
});