Skip to content

Instantly share code, notes, and snippets.

@joshuafredrickson
Last active May 11, 2017 18:04
Show Gist options
  • Save joshuafredrickson/31b92fa0ac57ea993345424b22f9ad7c to your computer and use it in GitHub Desktop.
Save joshuafredrickson/31b92fa0ac57ea993345424b22f9ad7c to your computer and use it in GitHub Desktop.
jQuery UI Dialog - Fluid on resize and scroll - Works with 1.11.4
$dialog.dialog({
draggable: false,
modal: true,
resizable: false,
maxWidth: 600,
width: 500,
fluid: true, // Enables fluidDialog()
});
function fluidDialog() {
var $visible = $('.ui-dialog:visible');
// Each open dialog
$visible.each(function () {
var $this = $(this);
var dialog = $this.find('.ui-dialog-content');
// If fluid option == true
if (dialog.dialog('option', 'fluid')) {
var wWidth = $(window).width();
// Check window width against dialog width
if (wWidth < dialog.dialog('option', 'maxWidth') + 50) {
// Keep dialog from filling entire screen
$this.css('max-width', '90%');
} else {
// Fix maxWidth bug
$this.css('max-width', dialog.dialog('option', 'maxWidth'));
}
// Reposition dialog
dialog.dialog('option', 'position', dialog.dialog('option', 'position'));
}
});
}
$(window).resize(function() {
fluidDialog();
});
$(window).scroll(function() {
fluidDialog();
});
// Catch dialog if opened within a viewport smaller than the dialog width
$(document).on('dialogopen', '.ui-dialog', function (event, ui) {
fluidDialog();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment