本文實例講述了JavaScript實現的彈出遮罩層特效。分享給大家供大家參考,具體如下:
這篇給大家分享一個簡單的遮罩層特效,先上效果圖。
代碼:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>查看,修改,刪除</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style> table{ width:500px; border:1px solid blue; border-collapse: collapse; } table th{ border:1px solid blue; height:30px; } table td{ border:1px solid blue; text-align:center; height:30px; } table td a { color:red; } div.proDiv{ width:500px; position: absolute; left:50%; margin-left:-250px; padding:10px; border:1px solid red; top:100px; background: #fff; display: none; z-index: 3 } div.proDiv p{ border-bottom:1px solid red; } div.proDiv a.close{ color:red; } </style></head><body> <table> <tr> <th>姓名</th> <th>年齡</th> <th>工作</th> <th>工資</th> <th>操作</th> </tr> <tr> <td>張三</td> <td>22</td> <td>項目經理</td> <td>12000</td> <td> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="view">查看</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">刪除</a> </td> </tr> <tr> <td>李四</td> <td>24</td> <td>前端工程師</td> <td>10000</td> <td> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="view">查看</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">刪除</a> </td> </tr> <tr> <td>王五</td> <td>21</td> <td>java工程師</td> <td>12000</td> <td> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="view">查看</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">刪除</a> </td> </tr> </table> <div class="proDiv"> <p><strong>姓名:</strong><span></span></p> <p><strong>年齡:</strong><span></span></p> <p><strong>工作:</strong><span></span></p> <p><strong>工資:</strong><span></span></p> <a class="close" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >關閉</a> </div></body><script> //查看操作 $('a.view').click(function(){ //獲取文檔的寬和高 var maskWidth = $(document).width(); var maskHeight = $(document).height(); //遮罩層初始化 $('<div class="mask"></div>').appendTo($('body')); $('div.mask').css({ 'position':'absolute', 'top':0, 'left':0, 'background':'black', 'opacity':0.5, 'width':maskWidth, 'height':maskHeight, 'z-index':2 }); var data = [];//保存數據的數組 //將一行的數據添加到數據中 $(this).parent().siblings().each(function(){ data.push($(this).text()) }); //將內容顯示到彈出層中 $('div.proDiv').children().each(function(i){ $(this).children('span').text(data[i]); }); $('div.proDiv').show();//顯示彈出層 //關閉操作 $('a.close').click(function(){ $(this).parent().hide(); $('div.mask').remove(); }); }); //刪除操作 $('a.del').click(function(){ $(this).parents('tr').remove(); });</script></html>
頁面中有一個表格,一個隱藏的彈出層,當點擊查看按鈕,首先創建一個遮罩層,然后獲取這一行中的數據,并把數據顯示到彈出層中,最后把彈出層隱藏,點擊關閉按鈕關閉彈出層并關閉遮罩層。點擊刪除按鈕把這個tr刪除即可。
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼運行效果。
更多關于jQuery相關內容可查看本站專題:《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結》、《jQuery常用插件及用法總結》、《jQuery擴展技巧總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
新聞熱點
疑難解答