纯js实现遮罩层效果原理分析
1、实现原理
本片文章的是实现原理如下:
*实际上弹出层、遮罩层和原页面显示分别为三个不同的div
*弹出层的层级在遮罩层之上,遮罩层的层级在原页面显示之上;
*遮罩层有通明效果
*遮罩层没有实际意义,则无需在html部分就写上,当然写上同样可以实现
2、代码实现
html语言如下:
<html> .... <body> <center> <div><inputtype="button"value="go"onclick="show()"></div> <divid="alert"style="display:none;"> <form> 登录 <inputtype="text"><inputtype="password"><inputtype="submit"value="login"> </form> </div> </center> </body> </html>
javascript实现弹出层和遮罩层:
<spanstyle="font-size:12px;">functionshow(){
varalertPart=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";
varmybg=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来区分层级,opacity和filter:alpha(opacity=)透明度,document.createElement("div")和document.body.appendChild()这些都是在之前出现过,应用过的了,这样我们就能实现了,其实当原理明白了的那一刻,一切也就容易多了吧。
路漫漫而修远兮啊
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下