I currently have a dialog with a bunch of options defined. It is shown when the user clicks on a button. My dialog is defined as:
$('#newIssueDialog').dialog({
autoOpen: false,
modal: true,
closeOnEscape: true,
draggable: false,
resizable: false,
width: 415,
height: 175,
position: {my: "top", at: "bottom", of: "#btnNewIssue"}
});
This pops my dialog up, centered below my button.
What I’d actually like to do is pop the dialog below the button, but centered on my window.
Any ideas on how to do that?
Edit: Also, is it possible to take into account the position of the window relative to the document? As in, if there are scrollbars on the page and the user has scrolled the page vertically or horizontally, can we calculate the position of the button and where we are on the page so that it is still below the button and centered?
Instead of using
my, at, ofyou can calculate the position yourself for theleftandtoplike this:Using the basic HTML as example:
DEMO – Positioning dialog below button but centred of window/document
I’m using
documentin the example above but you can use window instead if you want, there should be little to no difference.Edit
Fixed an oversight whereby the border height, the padding and margin of the button was not included correctly.
I saved the fiddle without adding the deduction of the
heightfromouterHeight(true)to include theborders,paddingandmarginsof the button as well in case the button had any specified.This:
Should be this:
Demo has been updated as well.
Edit
For the vertical scroll position, just deduct the current
scrollTop()value at the end like this:For placing the modal in the middle horizontally you don’t need to change anything. The above code to calculate
leftseems to always work. However if it is an issue for you in your specific markup, please have a look at scrollLeft() which I would assume would work the same way in that you need to deduct it similar to:or possibly
As said, the DEMO below doesn’t need it but if you do, move it around and see how it fits in.
DEMO – Including window scroll position
(You need to scroll down to get to the button and you can also scroll to the right to test it properly)