您可以创建自己的小部件扩展 $.ui.dialog 以使用选项添加具有准确配置的覆盖显示和隐藏动画。
另一个缺少通过单击覆盖关闭对话框的功能也很容易添加:
在某个文件中,比如 jquery-ui.fsrc.dialog.js:
(function( $, undefined ) {
$.widget('fsrc.fsrc_dialog', $.ui.dialog, {
open: function() {
// some helpful vars
var self = this,
options = self.options;
// call parent method - it will create overlay and save it in self.overlay variable
var ret = $.ui.dialog.prototype.open.apply(this, arguments);
if (options.showOverlay) {
// immediately hide and animate overlay
// kind a hack, but jquery ui developers left no better way
self.overlay.$el.hide().show(options.showOverlay)
}
if (options.closeOnOverlay) {
// close dialog on click on overlay
self.overlay.$el.click(function() {
self.close();
})
}
return ret;
},
close: function(event) {
var self = this,
options = self.options;
if (options.hideOverlay) {
// save and unset self.overlay, so it will not be destroyed by parent function during animation
var overlay = self.overlay
self.overlay = null;
overlay.$el.hide(options.hideOverlay, function() {
// destroy overlay after animation as parent would do
overlay.destroy();
})
}
// call parent method
var ret = $.ui.dialog.prototype.close.apply(this, arguments);
return ret;
}
});
}(jQuery));
在页面中:
<script src='/js/jquery-ui.fsrc.dialog.js' type='text/javascript'></script>
<script type="text/javascript">
<!--
jQuery(document).ready(function(){
jQuery('#show').click(function(){
jQuery('#order_form_container').fsrc_dialog({
modal: true,
closeOnOverlay: true,
show: {effect: "fade", duration: 500},
showOverlay: {effect: "fade", duration: 500},
hide: {effect: "fade", duration: 300},
hideOverlay: {effect: "fade", duration: 300},
});
})
})
-->
</script>`
我将命名空间、小部件和选项命名为对我有利的名称。
测试过jquery1.7.2、jquery-ui1.8.19、IE9、ff11、chrome18.0.1025.168m、opera11.61