JavaScript蒙板(model)功能的簡單實現代碼_javascript技巧

來源:互聯網
上載者:User

思路:

•建立一個蒙板, 設定蒙板的堆疊順序保證能將其它元素蓋住

position: absolute;top: 0;left: 0;display: none;background-color: rgba(9, 9, 9, 0.63);width: 100%;height: 100%;z-index: 1000; •設定蒙板中內容的背景色和展示格式width: 50%;text-align: center;background: #ffffff;border-radius: 6px;margin: 100px auto;line-height: 30px;z-index: 10001; •綁定事件, 動態切換蒙板的 display 屬性function showModel() {document.getElementById('myModel').style.display = 'block';}function closeModel() {document.getElementById('myModel').style.display = 'none';}

注意事項: 蒙板要採用絕對位置, 寬和高要佔満整個頁面, 堆疊順序要大

原始碼

<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>蒙板</title><style>.container {width: 900px;margin: 50px auto;text-align: center;}#myModel {position: absolute;top: 0;left: 0;display: none;background-color: rgba(9, 9, 9, 0.63);width: 100%;height: 100%;z-index: 1000;}.model-content {width: 50%;text-align: center;background: #ffffff;border-radius: 6px;margin: 100px auto;line-height: 30px;z-index: 10001;}</style></head><body><div class="container"><button onclick="showModel()">彈出蒙板</button><div id="myModel" onclick="closeModel()"><div class="model-content"><p>你好啊,我是內容~~</p><p><button id="closeModel" onclick="closeModel()">關閉</button></p></div></div></div><script>function showModel() {document.getElementById('myModel').style.display = 'block';}function closeModel() {document.getElementById('myModel').style.display = 'none';}</script></body></html>

以上所述是小編給大家介紹的JavaScript蒙板(model)功能的簡單實現代碼,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.