Below is how I achieved a responsive jQuery UI Dialog.
To do this, I added a new option to the config - fluid: true
, which says to make the dialog responsive.
I then catch the resize and dialog open events, to change the max-width of the dialog on the fly, and reposition the dialog.
You can see it in action here: http://codepen.io/jasonday/pen/amlqz
Please review and post any edits or improvements.
$("#content").dialog({
width: 'auto',
maxWidth: 600,
height: 'auto',
modal: true,
fluid: true,
resizable: false
});
$(window).resize(function () {
fluidDialog();
});
$(document).on("dialogopen", ".ui-dialog", function (event, ui) {
fluidDialog();
});
function fluidDialog() {
var $visible = $(".ui-dialog:visible");
$visible.each(function () {
var $this = $(this);
var dialog = $this.find(".ui-dialog-content").data("ui-dialog");
if (dialog.options.fluid) {
var wWidth = $(window).width();
if (wWidth < (parseInt(dialog.options.maxWidth) + 50)) {
$this.css("max-width", "90%");
} else {
$this.css("max-width", dialog.options.maxWidth + "px");
}
dialog.option("position", dialog.options.position);
}
});
}
EDIT
Updated approach:
https://github.com/jasonday/jQuery-UI-Dialog-extended
The repository above also includes options for:
- Click outside of dialog to close
- Hide title bar
- hide close button
- responsive (to address above)
- scale width & height for responsive (ex: 80% of window width)