本文為大家分享一個帶關閉按鈕的浮層彈窗效果:點擊按鈕後,頁面中央彈出一個帶標題欄的 DIV 窗口,背景被半透明遮罩鎖定,點擊右上角「關閉」即可收起。這是很多老式後台系統中常見的對話框樣式。
彈窗由兩個層構成:一個是全屏的半透明遮罩層 ly,用來鎖定背景、阻止用戶誤操作;另一個是承載內容的浮層 Layer2,通過 CSS 的 expression 讓它相對於頁面居中顯示。locking 函數負責打開(同時把遮罩層尺寸撐滿整個頁面),Lock_CheckForm 函數負責關閉,切換兩層的 display 即可。
完整代碼如下:
<html>
<head>
<title>JS+CSS實現帶關閉按鈕的DIV彈出窗口</title>
<script>
function locking(){
document.all.ly.style.display="block";
document.all.ly.style.width=document.body.clientWidth;
document.all.ly.style.height=document.body.clientHeight;
document.all.Layer2.style.display='block';
}
function Lock_CheckForm(theForm){
document.all.ly.style.display='none';document.all.Layer2.style.display='none';
return false;
}
</script>
<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
a:link {color: #FFFFFF;text-decoration: none;}
a:visited {text-decoration: none;}
a:hover {text-decoration: none;}
a:active {text-decoration: none;}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p align="center">
<input type="button" value="彈出DIV" onClick="locking()" />
</p>
<div id="ly" style="position: absolute; top: 0px; filter: alpha(opacity=60); background-color: #777;
z-index: 2; left: 0px; display: none;">
</div>
<div id="Layer2" align="center" style="position: absolute; z-index: 3; left: expression((document.body.offsetWidth-540)/2); top: expression((document.body.offsetHeight-170)/10);
background-color: #fff; display: none;" >
<table width="540" height="300" border="0" cellpadding="0" cellspacing="0" style="border: 0 solid #e7e3e7;
border-collapse: collapse ;" >
<tr>
<td style="background-color: #73A2d6; color: #fff; padding-left: 4px; padding-top: 2px;
font-weight: bold; font-size: 12px;" height="10" valign="middle">
<div align="right"><a href=JavaScript:; class="STYLE1" onclick="Lock_CheckForm(this);">[關閉]</a> </div></td>
</tr>
<tr>
<td height="130" align="center">
</td>
</tr>
</table>
</div>
</body>
</html>
以上就是本次分享的全部內容,希望對大家的 JavaScript 開發有所幫助。