純js實現(xiàn)遮罩層效果原理分析
更新時間:2014年05月27日 08:53:37 作者:
這篇文章主要介紹了純js實現(xiàn)遮罩層效果,下面就它的原理做下分析,感興趣的朋友可以參考下
可以說這個功能,在我理解了前面的“貪吃蛇”之后,實在是與剛開始想象的難度差了好多,當(dāng)然是這種方式有取巧之嫌,終歸是實現(xiàn)了功能,我們來進行分析整理
1、實現(xiàn)原理
本片文章的 是實現(xiàn)原理如下:
* 實際上彈出層、遮罩層和原頁面顯示分別為三個不同的div
* 彈出層的層級在遮罩層之上,遮罩層的層級在原頁面顯示之上;
* 遮罩層有通明效果
* 遮罩層沒有實際意義,則無需在html部分就寫上,當(dāng)然寫上同樣可以實現(xiàn)
2、代碼實現(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實現(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ū)分層級,opacity和filter:alpha(opacity=)透明度,document.createElement("div")和document.body.appendChild()這些都是在之前出現(xiàn)過,應(yīng)用過的了,這樣我們就能實現(xiàn)了,其實當(dāng)原理明白了的那一刻,一切也就容易多了吧。
路漫漫而修遠兮啊
1、實現(xiàn)原理
本片文章的 是實現(xiàn)原理如下:
* 實際上彈出層、遮罩層和原頁面顯示分別為三個不同的div
* 彈出層的層級在遮罩層之上,遮罩層的層級在原頁面顯示之上;
* 遮罩層有通明效果
* 遮罩層沒有實際意義,則無需在html部分就寫上,當(dāng)然寫上同樣可以實現(xiàn)
2、代碼實現(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實現(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ū)分層級,opacity和filter:alpha(opacity=)透明度,document.createElement("div")和document.body.appendChild()這些都是在之前出現(xiàn)過,應(yīng)用過的了,這樣我們就能實現(xiàn)了,其實當(dāng)原理明白了的那一刻,一切也就容易多了吧。
路漫漫而修遠兮啊
相關(guān)文章
JavaScript中 ES6 generator數(shù)據(jù)類型詳解
generator 是ES6引入的新的數(shù)據(jù)類型,由function* 定義, (注意*號),接下來通過本文給大家介紹js中 ES6 generator數(shù)據(jù)類型,非常不錯,感興趣的朋友一起學(xué)習(xí)吧2016-08-08
使用layui的router來進行傳參的實現(xiàn)方法
今天小編就為大家分享一篇使用layui的router來進行傳參的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
實用的JS正則表達式(手機號碼/IP正則/郵編正則/電話等)
實用的JS正則表達式(手機號碼/IP正則/郵編正則/電話等),經(jīng)驗積累,感興趣的朋友可以了解下,一定會對你有幫助的2013-01-01

