首页 > 语言 > JavaScript > 正文

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

2024-05-06 16:18:23
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了基于jquery ui的alert,confirm方案(支持换肤),修改自网友的源码,有需要的小伙伴参考下。

实现功能:

1.修改标题样式。把jquery ui的标题样式放上去。支持换肤。

2.修改按钮样式,换成jqueryui的button按钮样式。

3.将模式化窗口的背景换成了jqueryui的模式化背景。

代码:

 

 
  1. //首先要引入jquery,以及ui的包和皮肤的样式如: 
  2. <script src="../js/ui/jquery-1.11.0.min.js"></script> 
  3. <script src="../js/ui/jquery-migrate-1.1.0.min.js"></script> 
  4. <script src="../js/ui/minified/jquery.ui.core.min.js"></script> 
  5. <script src="../js/ui/minified/jquery.ui.widget.min.js"></script> 
  6. <script src="../js/ui/minified/jquery.ui.mouse.min.js"></script> 
  7. <script src="../js/ui/minified/jquery.ui.button.min.js"></script> 
  8. <script src="../js/ui/minified/jquery.ui.draggable.min.js"></script> 
  9. <link rel="stylesheet" type="text/css" href="../js/ui/themes/humanity/jquery-ui.css"></link> 
  10.  
  11.  
  12.  
  13. (function($) { 
  14.  
  15. $.alerts = { 
  16.  
  17. // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time 
  18.  
  19. verticalOffset: -75, // vertical offset of the dialog from center screen, in pixels 
  20. horizontalOffset: 0, // horizontal offset of the dialog from center screen, in pixels/ 
  21. repositionOnResize: true, // re-centers the dialog on window resize 
  22. overlayOpacity: .01, // transparency level of overlay 
  23. overlayColor: '#FFF', // base color of overlay 
  24. draggable: true, // make the dialogs draggable (requires UI Draggables plugin) 
  25. okButton: '确认', // text for the OK button 
  26. cancelButton: '取消', // text for the Cancel button 
  27. dialogClass: null, // if specified, this class will be applied to all dialogs 
  28.  
  29. // Public methods 
  30.  
  31. alert: function(message, title, callback) { 
  32. if( title == null ) title = 'Alert'; 
  33. $.alerts._show(title, message, null, 'alert', function(result) { 
  34. if( callback ) callback(result); 
  35. }); 
  36. }, 
  37.  
  38. confirm: function(message, title, callback) { 
  39. if( title == null ) title = 'Confirm'; 
  40. $.alerts._show(title, message, null, 'confirm', function(result) { 
  41. if( callback ) callback(result); 
  42. }); 
  43. }, 
  44.  
  45. prompt: function(message, value, title, callback) { 
  46. if( title == null ) title = 'Prompt'; 
  47. $.alerts._show(title, message, value, 'prompt', function(result) { 
  48. if( callback ) callback(result); 
  49. }); 
  50. }, 
  51.  
  52. // Private methods 
  53.  
  54. _show: function(title, msg, value, type, callback) { 
  55.  
  56. $.alerts._hide(); 
  57. $.alerts._overlay('show'); 
  58.  
  59. $("BODY").append( 
  60. '<div id="popup_container" style="width:300px;height:150px;">' + 
  61. '<h2 id="popup_title" style="margin:0;padding:0;" class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"></h2>' + 
  62. '<div id="popup_content">' + 
  63. '<div id="popup_message"></div>' + 
  64. '</div>' + 
  65. '</div>'); 
  66.  
  67. if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass); 
  68.  
  69. // IE6 Fix 
  70. //var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
  71. var pos = ('undefined' == typeof (document.body.style.maxHeight)) ? 'absolute' : 'fixed'; 
  72.  
  73. $("#popup_container").css({ 
  74. position: pos, 
  75. zIndex: 99999, 
  76. padding: 0, 
  77. margin: 0 
  78. }); 
  79.  
  80. $("#popup_title").text(title); 
  81. $("#popup_content").addClass(type); 
  82. $("#popup_message").text(msg); 
  83. $("#popup_message").html( $("#popup_message").text().replace(//n/g, '<br />') ); 
  84.  
  85. $("#popup_container").css({ 
  86. minWidth: $("#popup_container").outerWidth(), 
  87. maxWidth: $("#popup_container").outerWidth() 
  88. }); 
  89.  
  90. $.alerts._reposition(); 
  91. $.alerts._maintainPosition(true); 
  92.  
  93. switch( type ) { 
  94. case 'alert': 
  95. $("#popup_message").after('<div id="popup_panel"><input type="button" onmouseover="$(this).addClass(/'ui-state-hover/')" onmouseout="$(this).removeClass(/'ui-state-hover/')" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="' + $.alerts.okButton + '" id="popup_ok" /></div>'); 
  96. $("#popup_ok").click( function() { 
  97. $.alerts._hide(); 
  98. callback(true); 
  99. }); 
  100. $("#popup_ok").focus().keypress( function(e) { 
  101. if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click'); 
  102. }); 
  103. break; 
  104. case 'confirm': 
  105. $("#popup_message").after('<div id="popup_panel"><input type="button" onmouseover="$(this).addClass(/'ui-state-hover/')" onmouseout="$(this).removeClass(/'ui-state-hover/')" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" onmouseover="$(this).addClass(/'ui-state-hover/')" onmouseout="$(this).removeClass(/'ui-state-hover/')" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>'); 
  106. $("#popup_ok").click( function() { 
  107. $.alerts._hide(); 
  108. if( callback ) callback(true); 
  109. }); 
  110. $("#popup_cancel").click( function() { 
  111. $.alerts._hide(); 
  112. if( callback ) callback(false); 
  113. }); 
  114. $("#popup_ok").focus(); 
  115. $("#popup_ok, #popup_cancel").keypress( function(e) { 
  116. if( e.keyCode == 13 ) $("#popup_ok").trigger('click'); 
  117. if( e.keyCode == 27 ) $("#popup_cancel").trigger('click'); 
  118. }); 
  119. break; 
  120. case 'prompt': 
  121. $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" onmouseover="$(this).addClass(/'ui-state-hover/')" onmouseout="$(this).removeClass(/'ui-state-hover/')" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" onmouseover="$(this).addClass(/'ui-state-hover/')" onmouseout="$(this).removeClass(/'ui-state-hover/')" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>'); 
  122. $("#popup_prompt").width( $("#popup_message").width() ); 
  123. $("#popup_ok").click( function() { 
  124. var val = $("#popup_prompt").val(); 
  125. $.alerts._hide(); 
  126. if( callback ) callback( val ); 
  127. }); 
  128. $("#popup_cancel").click( function() { 
  129. $.alerts._hide(); 
  130. if( callback ) callback( null ); 
  131. }); 
  132. $("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) { 
  133. if( e.keyCode == 13 ) $("#popup_ok").trigger('click'); 
  134. if( e.keyCode == 27 ) $("#popup_cancel").trigger('click'); 
  135. }); 
  136. if( value ) $("#popup_prompt").val(value); 
  137. $("#popup_prompt").focus().select(); 
  138. break; 
  139. } 
  140.  
  141. // Make draggable 
  142. if( $.alerts.draggable ) { 
  143. try { 
  144. $("#popup_container").draggable({ handle: $("#popup_title") }); 
  145. $("#popup_title").css({ cursor: 'move' }); 
  146. } catch(e) { /* requires jQuery UI draggables */ } 
  147. } 
  148. }, 
  149.  
  150. _hide: function() { 
  151. $("#popup_container").remove(); 
  152. $.alerts._overlay('hide'); 
  153. $.alerts._maintainPosition(false); 
  154. }, 
  155.  
  156. _overlay: function(status) { 
  157. switch( status ) { 
  158. case 'show': 
  159. $.alerts._overlay('hide'); 
  160. $("BODY").append('<div class="ui-widget-overlay" id="popup_overlay"></div>'); 
  161. break; 
  162. case 'hide': 
  163. $("#popup_overlay").remove(); 
  164. break; 
  165. } 
  166. }, 
  167.  
  168. _reposition: function() { 
  169. var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset; 
  170. var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset; 
  171. if( top < 0 ) top = 0; 
  172. if( left < 0 ) left = 0; 
  173.  
  174. // IE6 fix 
  175. if ('undefined' == typeof (document.body.style.maxHeight)) top = top + $(window).scrollTop(); 
  176.  
  177. $("#popup_container").css({ 
  178. top: top + 'px', 
  179. left: left + 'px' 
  180. }); 
  181. }, 
  182.  
  183. _maintainPosition: function(status) { 
  184. if( $.alerts.repositionOnResize ) { 
  185. switch(status) { 
  186. case true: 
  187. $(window).bind('resize', function() { 
  188. $.alerts._reposition(); 
  189. }); 
  190. break; 
  191. case false: 
  192. $(window).unbind('resize'); 
  193. break; 
  194. } 
  195. } 
  196. } 
  197.  
  198. } 
  199.  
  200. // Shortuct functions 
  201. jAlert = function(message, title, callback) { 
  202. $.alerts.alert(message, title, callback); 
  203. } 
  204.  
  205. jConfirm = function(message, title, callback) { 
  206. $.alerts.confirm(message, title, callback); 
  207. }; 
  208.  
  209. jPrompt = function(message, value, title, callback) { 
  210. $.alerts.prompt(message, value, title, callback); 
  211. }; 
  212.  
  213. })(jQuery); 
  214.  
  215.  
  216.  
  217. <style> 
  218. *{margin:0;padding:0;} 
  219. #popup_container { 
  220. font-family: Arial, sans-serif; 
  221. font-size: 12px; 
  222. min-width: 300px; /* Dialog will be no smaller than this */ 
  223. max-width: 600px; /* Dialog will wrap after this width */ 
  224. background: #FFF; 
  225. border: solid 1px #D09042; 
  226. color: #000; 
  227. -moz-border-radius: 5px; 
  228. -webkit-border-radius: 5px; 
  229. border-radius: 5px; 
  230. } 
  231.  
  232.  
  233. #popup_content { 
  234. background: 16px 16px no-repeat url(images/info.gif); 
  235. padding: 1em 1.75em; 
  236. margin: 0em; 
  237. } 
  238.  
  239. #popup_content.alert { 
  240. background-image: url(../images/info.png); 
  241. } 
  242.  
  243. #popup_content.confirm { 
  244. background-image: url(../images/important.png); 
  245. } 
  246.  
  247. #popup_content.prompt { 
  248. background-image: url(../images/help.png); 
  249. } 
  250.  
  251. #popup_message { 
  252. padding-left: 48px; 
  253. height:30px; 
  254. padding-top:10px; 
  255. font-size:15px; 
  256. } 
  257.  
  258. #popup_panel { 
  259. text-align: center; 
  260. margin: 1em 0em 0em 1em; 
  261. } 
  262.  
  263. #popup_prompt { 
  264. margin: .5em 0em; 
  265. } 
  266. </style> 
  267.  
  268. //使用方法 
  269. <script> 
  270. jConfirm('您确定吗?', '系统 提示', function(r) { 
  271. jAlert('你选择了: ' + r, '友情提示'); 
  272. }); 
  273. </script> 

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选