jquery 自定义弹窗

IT����

IT����


 
 
  1. ( function () {
  2. $.MsgBox = {
  3. Alert: function (title, msg) {
  4. GenerateHtml( "alert", title, msg);
  5. btnOk(); //alert只是弹出消息,因此没必要用到回调函数callback
  6. btnNo();
  7. },
  8. Confirm: function (title, msg, callback) {
  9. GenerateHtml( "confirm", title, msg);
  10. btnOk(callback);
  11. btnNo();
  12. }
  13. }
  14. //生成Html
  15. var GenerateHtml = function (type, title, msg) {
  16. var _html = "";
  17. _html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
  18. _html += '<a id="mb_ico">x</a><div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
  19. if (type == "alert") {
  20. _html += '<input id="mb_btn_ok" type="button" value="确定" />';
  21. }
  22. if (type == "confirm") {
  23. _html += '<input id="mb_btn_ok" type="button" value="确定" />';
  24. _html += '<input id="mb_btn_no" type="button" value="取消" />';
  25. }
  26. _html += '</div></div>';
  27. //必须先将_html添加到body,再设置Css样式
  28. $( "body").append(_html);
  29. //生成Css
  30. GenerateCss();
  31. }
  32. //生成Css
  33. var GenerateCss = function () {
  34. $( "#mb_box").css({ width: '100%', height: '100%', zIndex: '99999', position: 'fixed',
  35. filter: 'Alpha(opacity=60)', backgroundColor: 'black', top: '0', left: '0', opacity: '0.6'
  36. });
  37. $( "#mb_con").css({ zIndex: '999999', width: '400px', position: 'fixed',
  38. backgroundColor: 'White', borderRadius: '15px'
  39. });
  40. $( "#mb_tit").css({ display: 'block', fontSize: '14px', color: '#444', padding: '10px 15px',
  41. backgroundColor: '#DDD', borderRadius: '15px 15px 0 0',
  42. borderBottom: '3px solid #009BFE', fontWeight: 'bold'
  43. });
  44. $( "#mb_msg").css({ padding: '20px', lineHeight: '20px',
  45. borderBottom: '1px dashed #DDD', fontSize: '13px'
  46. });
  47. $( "#mb_ico").css({ display: 'block', position: 'absolute', right: '10px', top: '9px',
  48. border: '1px solid Gray', width: '18px', height: '18px', textAlign: 'center',
  49. lineHeight: '16px', cursor: 'pointer', borderRadius: '12px', fontFamily: '微软雅黑'
  50. });
  51. $( "#mb_btnbox").css({ margin: '15px 0 10px 0', textAlign: 'center' });
  52. $( "#mb_btn_ok,#mb_btn_no").css({ width: '85px', height: '30px', color: 'white', border: 'none' });
  53. $( "#mb_btn_ok").css({ backgroundColor: '#168bbb' });
  54. $( "#mb_btn_no").css({ backgroundColor: 'gray', marginLeft: '20px' });
  55. //右上角关闭按钮hover样式
  56. $( "#mb_ico").hover( function () {
  57. $( this).css({ backgroundColor: 'Red', color: 'White' });
  58. }, function () {
  59. $( this).css({ backgroundColor: '#DDD', color: 'black' });
  60. });
  61. var _widht = document.documentElement.clientWidth; //屏幕宽
  62. var _height = document.documentElement.clientHeight; //屏幕高
  63. var boxWidth = $( "#mb_con").width();
  64. var boxHeight = $( "#mb_con").height();
  65. //让提示框居中
  66. $( "#mb_con").css({ top: (_height - boxHeight) / 2 + "px", left: (_widht - boxWidth) / 2 + "px" });
  67. }
  68. //确定按钮事件
  69. var btnOk = function (callback) {
  70. $( "#mb_btn_ok").click( function () {
  71. $( "#mb_box,#mb_con").remove();
  72. if ( typeof (callback) == 'function') {
  73. callback();
  74. }
  75. });
  76. }
  77. //取消按钮事件
  78. var btnNo = function () {
  79. $( "#mb_btn_no,#mb_ico").click( function () {
  80. $( "#mb_box,#mb_con").remove();
  81. });
  82. }
  83. })();
Html代码结构如下,js里面拼接的不直观,给出如下:


 
 
  1. <div id= "mb_box"> </div>
  2. <div id= "mb_con">
  3. <span id="mb_tit">title</span> <a id="mb_ico">x</a>
  4. <div id= "mb_msg">msg< /div>
  5. <div id="mb_btnbox">
  6. <input id="mb_btn_ok" type="button" value="确定" />
  7. <input id="mb_btn_no" type="button" value="取消" />
  8. </div>
  9. < /div>

demo:


 
 
  1. <html xmlns= "http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title>模拟alert和confirm提示框 </title>
  4. </head>
  5. <body>
  6. <input id="add" type="button" value="添加" />
  7. <input id="delete" type="button" value="删除" />
  8. <input id="update" type="button" value="修改" />
  9. <script src="../js/jquery-1.4.1.min.js" type="text/javascript"> </script>
  10. <script src="../js/jquery.similar.msgbox.js" type="text/javascript"> </script>
  11. <script type="text/javascript">
  12. $( "#add").bind( "click", function () {
  13. $.MsgBox.Alert( "消息", "哈哈,添加成功!");
  14. });
  15. //回调函数可以直接写方法function(){}
  16. $( "#delete").bind( "click", function () {
  17. $.MsgBox.Confirm( "温馨提示", "执行删除后将无法恢复,确定继续吗?温馨提示", function () { alert( "你居然真的删除了..."); });
  18. });
  19. function test() {
  20. alert( "你点击了确定,进行了修改");
  21. }
  22. //也可以传方法名 test
  23. $( "#update").bind( "click", function () {
  24. $.MsgBox.Confirm( "温馨提示", "确定要进行修改吗?", test);
  25. });
  26. //当然你也可以不给回调函数,点击确定后什么也不做,只是关闭弹出层
  27.      //$("#update").bind("click", function () { $.MsgBox.Confirm("温馨提示", "确定要进行修改吗?"); });
  28. </script>
  29. </body>
  30. </html>







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值