移動端H5網頁“上中下布局”的另類實現思路


QQ截圖20140221223815

記得曾經做過這樣的一個面試題,面試題要求如下:

  1. 相冊空間\硬盤空間的進度條,考慮百分比的控制;
  2. 左中右三欄等高,整個頁面的寬度不固定,左右寬度固定,中欄自寬度適應,論壇的文章標題也自適應寬;
  3. 加“…”的地方,考慮過長溢出省略處理;
  4. 假如這是一個訪問PV達2000W/日,因成本限制,網絡帶寬可能滿足不了此訪問量,會出現滯連情況,樣式文件可能加載不了,且中欄內容是最重要且要呈現在用戶眼前的。(也就是說,在無樣式不完全加載情況下,中欄內容要優先左右兩欄前);
  5. 關注標簽語義化;
  6. 關注提交的頁面原型便於團隊合作,開發實現;
  7. 關注HTTP請求和帶寬消耗所帶來的體驗和成本。

這幾點要求中,除了考察一個網頁的標簽語義化、頁面性能、開發技巧以外,頁面三欄等高布局、中間列優先加載應該算是開發過程當中的整個核心思路和決定整個頁面成敗的關鍵點。

關於三欄等高布局,網上已經有了舉不勝舉的實現方法和思路,且不說這些思路的好與壞,大家可以百度百度、谷歌谷歌,自作評價。

個人覺得各大互聯網公司做的比較好的三欄或者是兩欄布局有以下幾個站點:

當然兩欄或三欄布局,目前在各互聯網網站應用的實例非常之多,就不在此一一列舉。

在所有的這些案例當中,應用最多的一個css屬性非margin莫屬了,margin的值為“負值”。

 

有關margin負值,這里有篇經典文章:

負值之美:負margin在頁面布局中的應用

想必看完這篇文章,大家已經對負邊距有了更深一層次的認識。

於是得到了以下計算公式:

Margin負邊距 + 三欄等高布局 + 中間列優先加載 = 聖杯布局(業內盛傳的布局概念)的一部分。

 

在三列等高中間列優先加載的實現過程當中,嘗試再嘗試,最終沉淀出了以下“非公認的優秀代碼”:

html:

 1 <div class="wrapper">
 2 
 3   <!–頭部 start–>
 4   <header>header</header>
 5   <!–頭部 end–>
 6 
 7   <!–主體內容 start–>
 8    <div class="main_1">
 9        <div class="main_2">
10           <div class="main_3 cf">
11               <!–中間列 start–>
12               <section class="content">
13                   <ul>
14                       <li class="">
15                           <a href="#">測試內容www.css3china.com</a>
16                       </li>
17                       <li class="">
18                           <a href="#">測試內容www.css3china.com</a>
19                       </li>
20                       <li class="">
21                           <a href="#">測試內容www.css3china.com</a>
22                       </li>
23                   </ul>
24               </section>
25               <!–中間列 end–>
26               <!–左側邊欄 start–>
27               <aside class="aside_l">
28                   aside_l
29               </aside>
30               <!–左側邊欄 end–>
31               <!–右側邊欄 start–>
32               <aside class="aside_r">
33                   aside_r
34               </aside>
35              <!–右側邊欄 end–>
36           </div>
37       </div>
38   </div>
39   <!–主體內容 end–>
40 
41   <!–底部導航版權 start–>
42 
43   <footer>footer</footer>
44   <!–底部導航版權 end–>
45 
46  </div>

css:

 1 html, body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td{margin:0;padding:0;}
 2 html, body{height:100%;line-height:22px;color:#333;-webkit-text-size-adjust:none;}
 3 body{height:100%;background:#fff;min-width:600px;font:16px '微軟雅黑',Arial,Helvetica,sans-serif;}
 4 img, a img, input{vertical-align:middle;}
 5 img, a img, button {border:0;cursor:pointer;}
 6 input, textarea{outline:none;-moz-outline:none;}
 7 table, td ,th, tr{border-collapse:collapse;border:0;}
 8 ul, ol, li{list-style:none;}
 9 h1,h2,h3,h4,h5,h6{font-size:12px;}
10 a{color:#333333;text-decoration:underline;}
11 /*****清除浮動*****/
12 .cf:after{content:".";display:block;height:0;clear:both;visibility:hidden;font-size:0;}
13 .cf{display: inline-table;}
14 * html .cf{height:1%;}
15 .cf{display: block;}
16 /*****頭腳****/
17 header, footer{height:44px;background:#555555;line-height:44px;text-align:center;color:#FFFFFF;}
18 /*****三欄布局*****/
19 .main_1{background:#666666;padding-right:170px;}
20 .main_1 .main_2{background:#666666;padding-left:180px;}
21 .main_1 .main_3{background:#999;position:relative;}
22 .content, .aside_l, .aside_r{height:100%;float:left;position:relative;text-align:center;}
23 .aside_l, .aside_r{line-height:50px;}
24 /*****中間列*****/
25 .content{width:100%;}
26 .content ul{padding:10px;font-size:14px;line-height:22px;}
27 /*****左欄*****/
28 .aside_l{width:180px;left:-100%;margin-left:-180px;}
29 /*****右欄*****/
30 .aside_r{width:170px;margin-right:-170px;}

猛戳DEMO

同理在開發移動端webapp時,我們同樣需要面對各種布局以適應不同的產品需求,例如:上中下布局,曾經就是讓重構頭疼的一個問題。

關於webapp上中下布局,白樹給我們整理一篇非常有料的文章:

移動web頁面支持彈性滾動的3個方案

怎么樣,是不是像打了雞血一樣興奮,再也不用發愁移動端布局的事了!

 

接下來我為大家介紹一種新的實現移動端“上中下布局”的簡單思路,同理於聖杯布局(margin負邊距的巧妙應用)。

html:

 1 <div class="container">
 2      <header class="header">header</header>
 3      <section class="main">
 4          <div class="wrapper" id="wrapper"><!–  iscroll  容器 –>
 5              <div class="con"><!–  iscroll  滾動層 –>
 6                  <ul class="list">
 7                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
 8                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
 9                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
10                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
11                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
12                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
13                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
14                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
15                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
16                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
17                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
18                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
19                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
20                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
21                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
22                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
23                      <li><a class="" href="#">測試內容www.css3china.com</a></li>
24                  </ul>
25              </div>
26          </div>
27      </section>
28      <footer class="footer">footer</footer>
29  </div>

css:

 1 html, body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td{margin:0;padding:0;}
 2 html, body{height:100%;line-height:22px;color:#333;-webkit-text-size-adjust:none;}
 3 body{height:100%;background:#fff;min-width:600px;font:16px '微軟雅黑',Arial,Helvetica,sans-serif;overflow:hidden;}
 4 img, a img, input{vertical-align:middle;}
 5 img, a img, button {border:0;cursor:pointer;}
 6 input, textarea{outline:none;-moz-outline:none;}
 7 table, td ,th, tr{border-collapse:collapse;border:0;}
 8 ul, ol, li{list-style:none;}
 9 h1,h2,h3,h4,h5,h6{font-size:12px;}
10 a{color:#333333;text-decoration:underline;}
11 .container{height:100%;}
12 .header{background:#555555;height:44px;position:relative;z-index:100;line-height:44px;text-align:center;color:#FFFFFF;}
13 .main{background:#999999;height:100%;margin:-44px 0;padding:44px 0;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}
14 .wrapper{height:100%;overflow-y:auto\9;position:relative;}
15 .wrapper .con{padding:10px;}
16 .footer{background:#555555;height:44px;margin-top:-44px;position:relative;z-index:100;line-height:44px;text-align:center;color:#FFFFFF;}

chrome猛戳DEMO吧!ie猛戳DEMO吧!

代碼中標紅代碼為針對IE做的兼容,webkit內核使用iscroll插件實現滾動,IE使用瀏覽器默認滾動!

注意點:main的高度必須為整個網頁的高度,即為height:100%,所以需要繼承容器container{height:100%}的高度,container繼承body{height:100%;}的高度,以此類推。

最終使用margin負值和relative、z-index設置元素的位置和層級關系。從而使用純css實現了不用css3、不專門針對IE做特殊兼容處理的移動端“上中下布局”。

原文地址:http://www.css3china.com/?cat=16


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM