基于jquery ui的alert,confirm方案(支持换肤)

2020-05-18 09:01:48易采站长站整理

$("#popup_prompt").width( $("#popup_message").width() );
$("#popup_ok").click( function() {
var val = $("#popup_prompt").val();
$.alerts._hide();
if( callback ) callback( val );
});
$("#popup_cancel").click( function() {
$.alerts._hide();
if( callback ) callback( null );
});
$("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) {
if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
});
if( value ) $("#popup_prompt").val(value);
$("#popup_prompt").focus().select();
break;
}

// Make draggable
if( $.alerts.draggable ) {
try {
$("#popup_container").draggable({ handle: $("#popup_title") });
$("#popup_title").css({ cursor: 'move' });
} catch(e) { /* requires jQuery UI draggables */ }
}
},

_hide: function() {
$("#popup_container").remove();
$.alerts._overlay('hide');
$.alerts._maintainPosition(false);
},

_overlay: function(status) {
switch( status ) {
case 'show':
$.alerts._overlay('hide');
$("BODY").append('<div class="ui-widget-overlay" id="popup_overlay"></div>');
break;
case 'hide':
$("#popup_overlay").remove();
break;
}
},

_reposition: function() {
var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
if( top < 0 ) top = 0;
if( left < 0 ) left = 0;

// IE6 fix
if ('undefined' == typeof (document.body.style.maxHeight)) top = top + $(window).scrollTop();

$("#popup_container").css({
top: top + 'px',
left: left + 'px'
});
},

_maintainPosition: function(status) {
if( $.alerts.repositionOnResize ) {
switch(status) {
case true:
$(window).bind('resize', function() {
$.alerts._reposition();
});
break;
case false:
$(window).unbind('resize');
break;
}
}
}

}

// Shortuct functions
jAlert = function(message, title, callback) {
$.alerts.alert(message, title, callback);
}

jConfirm = function(message, title, callback) {
$.alerts.confirm(message, title, callback);
};

jPrompt = function(message, value, title, callback) {
$.alerts.prompt(message, value, title, callback);
};

})(jQuery);

<style>
*{margin:0;padding:0;}
#popup_container {
font-family: Arial, sans-serif;
font-size: 12px;
min-width: 300px; /* Dialog will be no smaller than this */
max-width: 600px; /* Dialog will wrap after this width */
background: #FFF;
border: solid 1px #D09042;
color: #000;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}

#popup_content {
background: 16px 16px no-repeat url(images/info.gif);
padding: 1em 1.75em;