純js實(shí)現(xiàn)遮罩層效果原理分析
更新時(shí)間:2014年05月27日 08:53:37 作者:
這篇文章主要介紹了純js實(shí)現(xiàn)遮罩層效果,下面就它的原理做下分析,感興趣的朋友可以參考下
可以說這個(gè)功能,在我理解了前面的“貪吃蛇”之后,實(shí)在是與剛開始想象的難度差了好多,當(dāng)然是這種方式有取巧之嫌,終歸是實(shí)現(xiàn)了功能,我們來進(jìn)行分析整理
1、實(shí)現(xiàn)原理
本片文章的 是實(shí)現(xiàn)原理如下:
* 實(shí)際上彈出層、遮罩層和原頁面顯示分別為三個(gè)不同的div
* 彈出層的層級(jí)在遮罩層之上,遮罩層的層級(jí)在原頁面顯示之上;
* 遮罩層有通明效果
* 遮罩層沒有實(shí)際意義,則無需在html部分就寫上,當(dāng)然寫上同樣可以實(shí)現(xiàn)
2、代碼實(shí)現(xiàn)
html語言如下:
<html>
....
<body>
<center>
<div ><input type="button" value="go" onclick="show()"></div>
<div id="alert" style="display:none;">
<form>
登錄
<input type="text"><input type="password"><input type="submit" value="login">
</form>
</div>
</center>
</body>
</html>
javascript實(shí)現(xiàn)彈出層和遮罩層:
<span style="font-size:12px;">function show(){
var alertPart=document.getElementById("alert");
alertPart.style.display="block";
alertPart.style.position = "absolute";
alertPart.style.top = "50%";
alertPart.style.left = "50%";
alertPart.style.marginTop = "-75px";
alertPart.style.marginLeft = "-150px";
alertPart.style.background="cyan";
alertPart.style.width="300px";
alertPart.style.height="200px";
alertPart.style.zIndex = "501";
var mybg = document.createElement("div");
mybg.setAttribute("id","mybg");
mybg.style.background = "#000";
mybg.style.width = "100%";
mybg.style.height = "100%";
mybg.style.position = "absolute";
mybg.style.top = "0";
mybg.style.left = "0";
mybg.style.zIndex = "500";
mybg.style.opacity = "0.3";
mybg.style.filter = "Alpha(opacity=30)";
document.body.appendChild(mybg);
document.body.style.overflow = "hidden";
}
</script></span>
這里用z-index來區(qū)分層級(jí),opacity和filter:alpha(opacity=)透明度,document.createElement("div")和document.body.appendChild()這些都是在之前出現(xiàn)過,應(yīng)用過的了,這樣我們就能實(shí)現(xiàn)了,其實(shí)當(dāng)原理明白了的那一刻,一切也就容易多了吧。
路漫漫而修遠(yuǎn)兮啊
1、實(shí)現(xiàn)原理
本片文章的 是實(shí)現(xiàn)原理如下:
* 實(shí)際上彈出層、遮罩層和原頁面顯示分別為三個(gè)不同的div
* 彈出層的層級(jí)在遮罩層之上,遮罩層的層級(jí)在原頁面顯示之上;
* 遮罩層有通明效果
* 遮罩層沒有實(shí)際意義,則無需在html部分就寫上,當(dāng)然寫上同樣可以實(shí)現(xiàn)
2、代碼實(shí)現(xiàn)
html語言如下:
復(fù)制代碼 代碼如下:
<html>
....
<body>
<center>
<div ><input type="button" value="go" onclick="show()"></div>
<div id="alert" style="display:none;">
<form>
登錄
<input type="text"><input type="password"><input type="submit" value="login">
</form>
</div>
</center>
</body>
</html>
javascript實(shí)現(xiàn)彈出層和遮罩層:
復(fù)制代碼 代碼如下:
<span style="font-size:12px;">function show(){
var alertPart=document.getElementById("alert");
alertPart.style.display="block";
alertPart.style.position = "absolute";
alertPart.style.top = "50%";
alertPart.style.left = "50%";
alertPart.style.marginTop = "-75px";
alertPart.style.marginLeft = "-150px";
alertPart.style.background="cyan";
alertPart.style.width="300px";
alertPart.style.height="200px";
alertPart.style.zIndex = "501";
var mybg = document.createElement("div");
mybg.setAttribute("id","mybg");
mybg.style.background = "#000";
mybg.style.width = "100%";
mybg.style.height = "100%";
mybg.style.position = "absolute";
mybg.style.top = "0";
mybg.style.left = "0";
mybg.style.zIndex = "500";
mybg.style.opacity = "0.3";
mybg.style.filter = "Alpha(opacity=30)";
document.body.appendChild(mybg);
document.body.style.overflow = "hidden";
}
</script></span>
這里用z-index來區(qū)分層級(jí),opacity和filter:alpha(opacity=)透明度,document.createElement("div")和document.body.appendChild()這些都是在之前出現(xiàn)過,應(yīng)用過的了,這樣我們就能實(shí)現(xiàn)了,其實(shí)當(dāng)原理明白了的那一刻,一切也就容易多了吧。
路漫漫而修遠(yuǎn)兮啊
您可能感興趣的文章:
- js+html5實(shí)現(xiàn)半透明遮罩層彈框效果
- js點(diǎn)擊按鈕實(shí)現(xiàn)帶遮罩層的彈出視頻效果
- js鼠標(biāo)懸浮出現(xiàn)遮罩層的方法
- js實(shí)現(xiàn)遮罩層彈出框的方法
- js css 實(shí)現(xiàn)遮罩層覆蓋其他頁面元素附圖
- js彈出div并顯示遮罩層
- JS實(shí)現(xiàn)遮罩層效果的簡單實(shí)例
- 原生js實(shí)現(xiàn)半透明遮罩層效果具體代碼
- JS遮罩層效果 兼容ie firefox jQuery遮罩層
- javascript實(shí)現(xiàn)遮罩層動(dòng)態(tài)效果實(shí)例
相關(guān)文章
JavaScript中 ES6 generator數(shù)據(jù)類型詳解
generator 是ES6引入的新的數(shù)據(jù)類型,由function* 定義, (注意*號(hào)),接下來通過本文給大家介紹js中 ES6 generator數(shù)據(jù)類型,非常不錯(cuò),感興趣的朋友一起學(xué)習(xí)吧2016-08-08
Javascript實(shí)現(xiàn)檢測客戶端類型代碼封包
在以前,總是以為使用用戶代理字符串檢測瀏覽器是那種類型就行了,這樣確實(shí)大錯(cuò)特錯(cuò)啊,下面就來說說如何通過js判斷出當(dāng)前瀏覽者使用的的設(shè)備類型呢2015-12-12
使用layui的router來進(jìn)行傳參的實(shí)現(xiàn)方法
今天小編就為大家分享一篇使用layui的router來進(jìn)行傳參的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
實(shí)用的JS正則表達(dá)式(手機(jī)號(hào)碼/IP正則/郵編正則/電話等)
實(shí)用的JS正則表達(dá)式(手機(jī)號(hào)碼/IP正則/郵編正則/電話等),經(jīng)驗(yàn)積累,感興趣的朋友可以了解下,一定會(huì)對(duì)你有幫助的2013-01-01

