如何實現全屏遮罩層效果

antzone發表於2017-04-10

本章節介紹一下如何實現點選按鈕能夠彈出一個彈出層,並且這個彈出層是全屏的。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script type="text/javascript">
function show(){
  var win=document.getElementById("win");
  win.style.display="block";
  win.style.position="absolute";
  win.style.top="50%";
  win.style.left="50%";
  win.style.marginTop="-75px";
  win.style.marginLeft="-150px";
  win.style.background="cyan";
  win.style.width="300px";
  win.style.height="200px";
  win.style.zIndex="1000";
  var mark = document.createElement("div");
  mark.setAttribute("id","mark");
  mark.style.background="#000";
  mark.style.width="100%";
  mark.style.height="100%";
  mark.style.position="absolute";
  mark.style.top="0";
  mark.style.left="0";
  mark.style.zIndex="500";
  mark.style.opacity="0.3";
  mark.style.filter="Alpha(opacity=30)";
  document.body.appendChild(mark);
}
window.onload=function(){
  var obt=document.getElementById("bt");
  obt.onclick=function(){
    show();
  }
}
</script>
</head>
<body>
<center>
  <div><input type="button" value="檢視效果" id="bt" /></div>
  <div id="win" style="display:none;"></div>
</center>
</body>
</html>

上面的程式碼中,點選按鈕可以彈出一個全屏的遮罩層效果,但是它並不是完美的。

因為當玩野的內容超出一屏的高度的時候,就會出現滾動條,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
body {
  margin:0px;
  padding:0px;
}
#antzone {
  margin:0px auto;
  width:20px;
  height:1200px;
  background:#ccc;
}
</style>
<script type="text/javascript">
function show(){
  var win=document.getElementById("win");
  win.style.display="block";
  win.style.position="absolute";
  win.style.top="50%";
  win.style.left="50%";
  win.style.marginTop="-75px";
  win.style.marginLeft="-150px";
  win.style.background="cyan";
  win.style.width="300px";
  win.style.height="200px";
  win.style.zIndex="1000";
  var mark = document.createElement("div");
  mark.setAttribute("id","mark");
  mark.style.background="#000";
  mark.style.width="100%";
  mark.style.height="100%";
  mark.style.position="absolute";
  mark.style.top="0";
  mark.style.left="0";
  mark.style.zIndex="500";
  mark.style.opacity="0.3";
  mark.style.filter="Alpha(opacity=30)";
  document.body.appendChild(mark);
}
window.onload=function(){
  var obt=document.getElementById("bt");
  obt.onclick=function(){
    show();
  }
}
</script>
</head>
<body>
<center>
  <div><input type="button" value="檢視效果" id="bt" /></div>
  <div id="win" style="display:none;"></div>
</center>
<div id="antzone"></div>
</body>
</html>

但是如果高度超過一屏的話,就會出現滾動條,下拉滾動條的話,那就有點慘不忍睹了。

上面的問題還是很好解決的,只要給body新增一個overflow:hidden即可:

[JavaScript] 純文字檢視 複製程式碼
document.body.style.overflow = "hidden";

但是上面的程式碼還不夠完美,因為有可能我們彈出的那個視窗高度是很大的,看如下程式碼例項:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
body {
  margin:0px;
  padding:0px;
}
#antzone {
  margin:0px auto;
  width:20px;
  height:1200px;
  background:#ccc;
}
</style>
<script type="text/javascript">
function show(){
  var win=document.getElementById("win");
  win.style.display="block";
  win.style.position="absolute";
  win.style.top="50%";
  win.style.left="50%";
  win.style.marginTop="-75px";
  win.style.marginLeft="-150px";
  win.style.background="cyan";
  win.style.width="300px";
  win.style.height="800px";
  win.style.zIndex="1000";
  var mark = document.createElement("div");
  mark.setAttribute("id","mark");
  mark.style.background="#000";
  mark.style.width="100%";
  mark.style.height="100%";
  mark.style.position="absolute";
  mark.style.top="0";
  mark.style.left="0";
  mark.style.zIndex="500";
  mark.style.opacity="0.3";
  mark.style.filter="Alpha(opacity=30)";
  document.body.appendChild(mark);
  document.body.style.overflow = "hidden";
}
window.onload=function(){
  var obt=document.getElementById("bt");
  obt.onclick=function(){
    show();
  }
}
</script>
</head>
<body>
<center>
  <div><input type="button" value="檢視效果" id="bt" /></div>
  <div id="win" style="display:none;"></div>
</center>
<div id="antzone"></div>
</body>
</html>

上面的程式碼中,彈出視窗的高度是800px,但是由於設定了body的overflow:hidden,所以沒法顯示完整。

在這種情況下,我們也需要計算遮罩層的高度了,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
body {
  margin:0px;
  padding:0px;
}
#antzone {
  margin:0px auto;
  width:20px;
  height:1200px;
  background:#ccc;
}
</style>
<script type="text/javascript">
function show(){
  var win=document.getElementById("win");
  win.style.display="block";
  win.style.position="absolute";
  win.style.top="50%";
  win.style.left="50%";
  win.style.marginTop="-75px";
  win.style.marginLeft="-150px";
  win.style.background="cyan";
  win.style.width="300px";
  win.style.height="800px";
  win.style.zIndex="1000";
  var mark = document.createElement("div");
  mark.setAttribute("id","mark");
  mark.style.background="#000";
  mark.style.width="100%";
  mark.style.height=document.body.offsetHeight+"px";
  mark.style.position="absolute";
  mark.style.top="0";
  mark.style.left="0";
  mark.style.zIndex="500";
  mark.style.opacity="0.3";
  mark.style.filter="Alpha(opacity=30)";
  document.body.appendChild(mark);
}
window.onload=function(){
  var obt=document.getElementById("bt");
  obt.onclick=function(){
    show();
  }
}
</script>
</head>
<body>
<center>
  <div><input type="button" value="檢視效果" id="bt" /></div>
  <div id="win" style="display:none;"></div>
</center>
<div id="antzone"></div>
</body>
</html>

上面的核心程式碼如下:

[JavaScript] 純文字檢視 複製程式碼
mark.style.height=document.body.offsetHeight+"px";

相關文章