JS實現(xiàn)的添加彈出層并完成鎖屏操作示例
本文實例講述了JS實現(xiàn)的添加彈出層并完成鎖屏操作。分享給大家供大家參考,具體如下:
上圖:
代碼:
<html> <head> <title>彈出層</title> <style type="text/css"> *{ padding:0px; margin:0px; } .up{ width:500px; height: 400px; border:1px solid silver; position: absolute; display: none; z-index: 9999; background:#fff; /* top:50%; left: 50%;*/ /* margin-left: -250px; margin-top: -200px;*/ } .up h2{ background-color: #f2f2f2; text-align: center; } .con span{ width:20px; height:30px; text-align: center; line-height: 30px; background-color:red; cursor: pointer; } .mask{ width:3000px; height: 3000px; background:#000; opacity: 0.3; position: absolute; top:0; left: 0; z-index: 9998; display:none; } </style> </head> <body> <div class="con"> <span id="open">打開彈出層</span> </div> <div class="up" id="up"> <h2>彈出層效果</h2> <span id="close">關(guān)閉</span> </div> <img src="a.jpg"> </body> <script type="text/javascript"> //兩種方式實現(xiàn)div居中:1:用css方式:top:50%,left:50%; margin-let:-divwidth/2 margin-top:divheight/2; 2:用js實現(xiàn):獲取窗口的高寬,top=(屏幕高-div高)/2,為了隨時的監(jiān)聽瀏覽器的改變,需要用到瀏覽器事件 window.onload=function(){ function $(id){ return document.getElementById(id); } //將div的位置封裝在一個函數(shù)內(nèi)部,直接調(diào)用 function myDiv(){ var mTop=(document.documentElement.clientHeight-500)/2; var mleft=(document.documentElement.clientWidth-400)/2; $("up").style.top=mTop+"px"; $("up").style.left=mleft+"px"; } myDiv(); $("open").onclick=function(){ $("up").style.display="block"; mask.style.display="block"; } $("close").onclick=function(){ $("up").style.display="none"; mask.style.display="none" } //創(chuàng)建一個DIV var mask=document.createElement("div"); // //給這個DIV一個id和class屬性 // mask.id="mask"; mask.className="mask"; mask.style.width=document.documentElement.clientWidth; mask.style.height=document.documentElement.clientHeight; //將這個DIV放置到body里面--》一般是:父節(jié)點.appendChild(子節(jié)點) //這里注意的是absolute,要設(shè)置top和left; document.body.appendChild(mask); //屏幕改變大小的時候,div不會自動的改變,需要添加窗口改變事件 window.onresize=function(){ myDiv(); mask.style.width=document.documentElement.clientWidth; mask.style.height=document.documentElement.clientHeight; } } </script> </html>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
純Javascript實現(xiàn)Windows 8 Metro風(fēng)格實現(xiàn)
Windows 8 Metro風(fēng)格設(shè)計,實現(xiàn)網(wǎng)站或系統(tǒng)功能的導(dǎo)航,在本文將為大家介紹下如何用純Javascript實現(xiàn)Windows 8 Metro風(fēng)格,感興趣的朋友可以參考下2013-10-10uniapp基礎(chǔ)篇之上傳圖片的實戰(zhàn)步驟
應(yīng)用uni-app開發(fā)跨平臺App項目時,上傳圖片、文檔等資源功能需求十分常見,下面這篇文章主要給大家介紹了關(guān)于uniapp基礎(chǔ)篇之上傳圖片的相關(guān)資料,需要的朋友可以參考下2022-12-12JavaScript自定義插件實現(xiàn)tabs切換功能
這篇文章主要為大家詳細(xì)介紹了JavaScript自定義插件實現(xiàn)tabs切換功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04JS連接SQL數(shù)據(jù)庫與ACCESS數(shù)據(jù)庫的方法實例
這篇文章主要介紹了JS連接SQL數(shù)據(jù)庫與ACCESS數(shù)據(jù)庫的方法實例,有需要的朋友可以參考一下2013-11-11細(xì)數(shù)promise與async/await的使用及區(qū)別說明
這篇文章主要介紹了細(xì)數(shù)promise與async/await的使用及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07