jQuery实现单击按钮遮罩弹出对话框(仿天猫的删除对话框)

我们在天猫进行购物的时候,经常会碰到单击删除按钮或者登陆按钮后,弹出对话框问你是否删除或者弹出一个登陆对话框,并且我们也是可以看到我们之前页面的信息,就是点击不了,只有对对话框进行操作后才有相应的变化。截图如下(以天猫为例) 

如图所示,上面就是天猫的效果图,其实这就是通过jQuery实现的,并且实现的过程也不是很不复杂,那么现在就让我们来看看实现的过程吧。 

首先是页面的布局部分:delete.html 

 
 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <title>遮罩弹出窗口</title> 
  5.  
  6. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  7. <meta http-equiv="description" content="this is my page"> 
  8. <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
  9.  
  10. <link rel="stylesheet" type="text/css" href="../css/delete.css"> 
  11. <script type="text/javascript" src="../js/jquery-1.10.2.js"></script> 
  12. <script type="text/javascript" src="../js/delete.js"></script> 
  13.  
  14. </head> 
  15.  
  16. <body> 
  17. <div class="divShow"> 
  18. <input type="checkbox" id="chexkBox1"> <a href="#">这是一条可以删除的记录</a> 
  19. <input id="button1" type="button" value="删除" class="btn"> 
  20.  
  21.  
  22. </div> 
  23.  
  24.  
  25. <div class="mask"></div> 
  26. <div class="dialog"> 
  27. <div class="title"> 
  28. <img alt="点击可以关闭" src="../images/delete.gif" width="30px" height="30px;"> 
  29. 删除时提示 
  30. </div> 
  31. <div class="content"> 
  32. <img alt="" src="../images/delete.gif" width="60px" height="60px"> 
  33. <span>你真的要删除这条记录吗?</span> 
  34.  
  35. </div> 
  36. <div class="bottom"> 
  37. <input type="button" id="ok" value="确定" class="btn"> 
  38. <input type="button" id="noOk" value="取消" class="btn"> 
  39.  
  40. </div> 
  41. </div> 
  42.  
  43. </body> 
  44. </html> 

需要做出说明的是,我只添加了一条记录,其实可以模拟多条记录的删除。这里我们有三层div结构,其中mask和dialog使我们通过jquery进行触发的,接下来我们讲下css的布局,先上代码:delete.html 

  1. @CHARSET "UTF-8"; 
  2. *{ 
  3. margin: 0px; 
  4. padding: 0px; 
  5.  
  6. } 
  7. .divShow{ 
  8. line-height: 32px; 
  9. height: 32px; 
  10. background-color: #eee; 
  11. width: 280px; 
  12. padding-left: 10px; 
  13. } 
  14.  
  15.  
  16.  
  17. .dialog{ 
  18. width: 360px; 
  19. border: 1px #666 solid; 
  20. position: absolute; 
  21. display: none; 
  22. z-index: 101;//保证该层在最上面显示 
  23. } 
  24.  
  25. .dialog .title{ 
  26. background:#fbaf15; 
  27. padding: 10px; 
  28. color: #fff; 
  29. font-weight: bold; 
  30.  
  31. } 
  32.  
  33. .dialog .title img{ 
  34. float:right; 
  35. } 
  36.  
  37. .dialog .content{ 
  38.  
  39. background: #fff; 
  40. padding: 25px; 
  41. height: 60px; 
  42. } 
  43.  
  44. .dialog .content img{ 
  45. float: left; 
  46. } 
  47. .dialog .content span{ 
  48. float: left; 
  49. padding: 10px; 
  50.  
  51. } 
  52.  
  53.  
  54. .dialog .bottom{ 
  55.  
  56. text-align: right; 
  57. padding: 10 10 10 0; 
  58. background: #eee; 
  59. } 
  60.  
  61. .mask{ 
  62.  
  63. width: 100%; 
  64. height: 100%; 
  65. background: #000; 
  66. position: absolute; 
  67. top: 0px; 
  68. left: 0px; 
  69. display: none; 
  70. z-index: 100; 
  71.  
  72. } 
  73. .btn{ 
  74.  
  75. border: #666 1px solid; 
  76. width: 65px; 
  77.  
  78. } 

在CSS文件中,我需要着重说明的是z-index的使用,z-index表示的层的堆叠顺序,如果数值越高,表示越在上层显示,mask的z-index是100,dialog的z-index是101,数值足够大的原因就是保证绝对在顶层显示,通过数值的调增可以控制div层的显示。 

接下来就是最为主要的js代码,当然在使用jquery时,我们要导入jquery包:<script type="text/javascript" src="../js/jquery-1.10.2.js"></script> 

delete.js 

  1. $(function(){ 
  2.  
  3. //绑定删除按钮的触发事件 
  4. $("#button1").click(function(){ 
  5.  
  6. $(".mask").css("opacity","0.3").show(); 
  7. showDialog(); 
  8. $(".dialog").show(); 
  9. }); 
  10.  
  11. /* 
  12. * 根据当前页面于滚动条的位置,设置提示对话框的TOP和left 
  13. */ 
  14. function showDialog(){ 
  15. var objw=$(window);//当前窗口 
  16. var objc=$(".dialog");//当前对话框 
  17. var brsw=objw.width(); 
  18. var brsh=objw.height(); 
  19. var sclL=objw.scrollLeft(); 
  20. var sclT=objw.scrollTop(); 
  21. var curw=objc.width(); 
  22. var curh=objc.height(); 
  23. //计算对话框居中时的左边距 
  24. var left=sclL+(brsw -curw)/2; 
  25. var top=sclT+(brsh-curh)/2; 
  26.  
  27. //设置对话框居中 
  28. objc.css({"left":left,"top":top}); 
  29.  
  30. } 
  31.  
  32. //当页面窗口大小改变时触发的事件 
  33. $(window).resize(function(){ 
  34.  
  35. if(!$(".dialog").is(":visible")){ 
  36. return; 
  37. } 
  38. showDialog(); 
  39. }); 
  40.  
  41. //注册关闭图片单击事件 
  42. $(".title img").click(function(){ 
  43.  
  44. $(".dialog").hide(); 
  45. $(".mask").hide(); 
  46.  
  47. }); 
  48. //取消按钮事件 
  49. $("#noOk").click(function(){ 
  50. $(".dialog").hide(); 
  51. $(".mask").hide(); 
  52. }); 
  53.  
  54. //确定按钮事假 
  55. $("#ok").click(function(){ 
  56.  
  57. $(".dialog").hide(); 
  58. $(".mask").hide(); 
  59.  
  60. if($("input:checked").length !=0){ 
  61. //注意过滤器选择器中间不能存在空格$("input :checked")这样是错误的 
  62.  
  63. $(".divShow").remove();//删除某条数据 
  64. } 
  65.  
  66. }); 
  67.  
  68.  
  69. });<span style="white-space:pre"> 
需要说明的是主要代买就是showDialog()的用于动态的确定对话框的显示位置。 

猜你喜欢

转载自blog.csdn.net/coding_1/article/details/56011572