標籤:style blog color io os 使用 java ar div
今天寫了一個簡易的彈出層,為了以後工作能夠重複使用,特地整理了一下。
首先,添加彈出層,賦id為tip_box
<div id="tip_box">
<div id="close_box">x</div>
<p>彈出層內容</p>
</div>
在HTML中調用box()函數
<a href="javascript:void(0)" onclick="box()"></a>
不需要單獨的樣式設定,以下為javascript源碼
1 function box() { 2 var tip_box=document.getElementById("tip_box") //擷取彈出層ID 3 var wrap_box=document.createElement("div") //建立遮罩層節點 4 var close_box=document.getElementById("close_box") //擷取關閉按鈕ID 5 var tip_box_w=400 //彈出層寬度 6 var tip_box_h=200 //彈出層高度 7 var tip_box_background="#fff" //彈出層背景顏色 8 var tip_box_x=(innerWidth-tip_box_w)/2 //彈出層橫座標 9 var tip_box_y=(innerHeight-tip_box_h)/2 //彈出層縱座標10 var wrap_box_w=innerWidth //遮罩層寬度11 var wrap_box_h=innerHeight //遮罩層高度12 wrap_box.style.position="fixed"; 13 wrap_box.style.background="rgba(0,0,0,0.5)";14 wrap_box.style.width=wrap_box_w+"px"; 15 wrap_box.style.height=wrap_box_h+"px";16 wrap_box.style.zIndex=98;17 wrap_box.style.top=0+"px";18 close_box.style.position="absolute";19 close_box.style.width=30+"px";20 close_box.style.height=30+"px";21 close_box.style.top=0+"px";22 close_box.style.right=0+"px";23 close_box.style.background="rgb(0,0,0)";24 close_box.style.color="rgb(255,255,255)";25 close_box.style.lineHeight=30+"px";26 close_box.style.textAlign="center";27 close_box.style.cursor="pointer";28 close_box.addEventListener("click",function(){29 tip_box.style.display="none";30 wrap_box.remove()31 })32 tip_box.style.display="block";33 tip_box.style.width=tip_box_w+"px";34 tip_box.style.height=tip_box_h+"px";35 tip_box.style.zIndex=99;36 tip_box.style.position="absolute";37 tip_box.style.top=tip_box_y+"px";38 tip_box.style.left=tip_box_x+"px";39 tip_box.style.background=tip_box_background;40 document.body.appendChild(wrap_box)41 };
簡易網頁彈出層