JavaScript 彈出層,背景變暗

來源:互聯網
上載者:User

JavaScript 彈出層,背景變暗,代碼不算多,根據你的需要調整一下,這裡只是實現了基礎的思路,美化不是太好。

 

<title>JavaScript 彈出層,背景變暗</title>
<script>
var docEle = function() {
   return document.getElementById(arguments[0]) || false;
}
function openNewDiv(_id) {
   var m = "mask";
   if (docEle(_id)) document.removeChild(docEle(_id));
   if (docEle(m)) document.removeChild(docEle(m));
   // www.codefans.net 新啟用圖層
   var newDiv = document.createElement("div");
   newDiv.id = _id;
   newDiv.style.position = "absolute";
   newDiv.style.zIndex = "9999";
   newDiv.style.width = "200px";
   newDiv.style.height = "300px";
   newDiv.style.top = "100px";
   newDiv.style.left = (parseInt(document.body.scrollWidth) - 300) / 2 + "px"; // 螢幕置中
   newDiv.style.background = "EEEEEE";
   newDiv.style.border = "1px solid #0066cc";
   newDiv.style.padding = "5px";
   newDiv.innerHTML = "新啟用圖層內容";
   document.body.appendChild(newDiv);
   // mask圖層
   var newMask = document.createElement("div");
   newMask.id = m;
   newMask.style.position = "absolute";
   newMask.style.zIndex = "1";
   newMask.style.width = document.body.scrollWidth + "px";
   newMask.style.height = document.body.scrollHeight + "px";
   newMask.style.top = "0px";
   newMask.style.left = "0px";
   newMask.style.background = "#000";
   newMask.style.filter = "alpha(opacity=40)";
   newMask.style.opacity = "0.40";
   document.body.appendChild(newMask);
// docutment www.codefans.net 
   // 關閉mask和新圖層
   var newA = document.createElement("a");
   newA.href = "#";
   newA.innerHTML = "關閉啟用層";
   newA.onclick = function() {
    document.body.removeChild(docEle(_id));
    document.body.removeChild(docEle(m));
    return false;
   }
   newDiv.appendChild(newA);
}
</script>
<a href="#" onclick="openNewDiv(newDiv);return false;">彈出新層</a>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.