網頁開發筆記【一】建立一個半透明的頁面

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   使用   java   

近來正在溫習網頁製作,系列《網頁開發筆記》記錄本人在解決網頁開發過程中遇到的問題以及解決方案。

 

半透明的頁面在網頁開發中使用的比較廣泛,尤其在web app中,這種設計使用的地方更為廣泛,本文記錄這種半透明的頁面的開發方法。

半透明頁面常用於使用者註冊,這時候彈出一個頁面能在不離開當前頁面的情況下完成註冊。

如百度的登陸介面所示。

我的處理方法如下:

首先在頁面寫一個div,這個div平時display設定為none,當需要的時候設定為display:block;

這個div(設其id為popup)為實際的登陸面板(也就是的白色地區)的容器。

則popup的css代碼如下:

.popup{position:fixed;left:0px;top:0px;padding:0px;border:none;margin:0px;background:black;opacity:0.5;}

div設定為fixed方式定位,相對body進行定位。

然後注意這個popup的寬度和高度並沒有設定,我們可以在window.onload事件中來設定popup的寬度和高度。

javascript代碼如下:

window.onload = function(){var height = Math.max(document.body.clientHeight,document.documentElement.clientHeight);var width = Math.max(document.body.clientWidth,document.documentElement.clientWidth);var obj = document.getElementById("popup");obj.style.width=width+"px";obj.style.height=height+"px";}

 這裡面值得注意的是:var height = Math.max(document.body.clientHeight,document.documentElement.clientHeight);

這段代碼的原因是這樣的:

因為w3c的標準的原因,不同的瀏覽器以及加不加DOCTYPE都有區別:http://hi.baidu.com/bluedream_119/item/26db5a73c9774344ee1e532a

此外,關於clientTop,clientWidth等參數的含義參考這篇文章:http://blog.csdn.net/xuantian868/article/details/3116442

完整的範例程式碼如下:

 1 <!DOCTYPE html>  2  3 <html> 4 <head>  5     <title>Demo1</title> 6 </head> 7  8 <script type="text/javascript"> 9 window.onload=function()10 {11 var height = Math.max(document.body.clientHeight,document.documentElement.clientHeight);12 var width = Math.max(document.body.clientWidth,document.documentElement.clientWidth);13 14 var obj = document.getElementById("div1");15 obj.style.width=width+"px";16 obj.style.height=height+"px";17 }18 </script>19 20 <style type="text/css">21 22 .wrapper{23  width:100%;24  height:2000px;25  padding:0px;26  margin:0px;27  border:none;28  background:green;29 }30 31 32 .trans{33 position:fixed;34 left:0px;35 top:0px;36 padding:0px;37 border:none;38 margin:0px;39 background:black;40 opacity:0.5;41 }42 43 .addplan{44 background:white;45 }46 </style>47 48 <body class="wrapper">49 <div id="div1" class="trans">50 </div>51 </body>52 </html>

 

 至此,半透明的頁面完成了。

 

網頁開發筆記【一】建立一個半透明的頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.