簡易網頁彈出層

來源:互聯網
上載者:User

標籤: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         };

簡易網頁彈出層

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.