DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
百度、淘寶、騰訊的 HTML 頁面規範對照拆解
編輯:HTML5詳解     

把頭部程式碼打開來看,其實就能讀出一個團隊對瀏覽器環境的判断。下面以百度、淘寶、騰訊 isux 研究院的線上頁面為樣本,逐項拆解它們在 HTML5 相容、相容模式、行動裝置適配與升級引導這四件事上的做法,最後整理出一份可直接套用的頁面骨架。

一、讓舊 IE 認得 HTML5 標籤

IE9 以下的版本不會把 header、section 這類新標籤當成真正的元素,樣式自然無處安放。通行解法是先用 JavaScript 把它們「創建」一遍,再補一條 display 兜底。

百度貼吧、百度圖片採用的就是這種自帶的寫法:

<!--[if lt IE 9]>
<script>
(function () {
  var tags = ['header','footer','figure','figcaption','details','summary','hgroup',
              'nav','aside','article','section','mark','abbr','meter','output',
              'progress','time','video','audio','canvas','dialog'];
  for (var i = tags.length - 1; i > -1; i--) {
    document.createElement(tags[i]);
  }
})();
</script>
<![endif]-->

<style>
header,footer,section,article,aside,nav,figure{display:block;margin:0;padding:0;border:0;}
</style>

騰訊 isux 研究院走的是外掛腳本的路線:

<!--[if lt IE 9]>
  <script src="https://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

淘寶「我的淘寶」頁面同理,只是換成了自家 CDN 上的版本:

<!--[if lt IE 9]>
<script src="https://g.tbcdn.cn//tb/mytaobao/12.12.15/common/??html5shiv-min.js"></script>
<![endif]-->

說到這裡必須提一下 html5shiv。這個專案在 GitHub 上累積了三千多顆星,是國內外都廣泛採用的方案。如果你早就受不了滿頁的 div,想盡快改用語義化標籤,直接引入它就好。

二、相容模式怎麼選:X-UA-Compatible

三家的選擇頗有耐人尋味之處。百度首頁寫的是:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">

百度搜索結果頁寫的是:

<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">

百度圖片、騰訊 isux 研究院、淘寶收藏頁則統一採用:

<meta http-equiv="X-UA-Compatible" content="IE=edge"/>

X-UA-Compatible 是 IE8 時代引入的專用標記,用來指定瀏覽器以哪個版本的引擎來渲染頁面。而 Chrome=1 的效果是:使用者若裝過 Google Chrome Frame,就交由它渲染;沒裝則退回當前可用的最高 IE 版本。

這個差異背後其實是取捨。首頁內容極簡,用哪種模式都無所謂,於是偏向照顧老機器;搜索頁對效能敏感,有 GCF 就情願用 Chrome 引擎;第三種寫法則因為要遷就各家銀行的支付頁面,反而成了大家默認的安全牌。當然,GCF 早已停止維護,今天的專案寫一行 <!DOCTYPE html> 就足夠了,這裡更多是作為一段技術史的參考。

三、行動優先的 viewport 設定

騰訊 isux 研究院的寫法:

<!-- Mobile Specific Metas
================================================== -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes" />

淘寶首頁的寫法:

<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />

內容幾乎一致,只是屬性順序不同。這裡沒列百度,並不代表百度不重視行動端——恰恰相反,當年以十九億美元收購 91 無線,加上百度 App、貼吧、音樂、圖片一系列獨立應用,它選擇的是另一條路。

四、還在用 IE8?給一條升級提示

<!--[if lt IE 8]>
<div class="g_tips">
  <p>已經有超過 90% 的使用者換用更高版本的
  <a target="_blank" title="下載 Chrome" href="https://www.google.com/chrome/">Google Chrome</a>
  或
  <a target="_blank" href="https://www.microsoft.com/zh-cn/download/IE.aspx?q=internet+explorer">Internet Explorer</a>
  獲得了更流暢的體驗,你還不試試?</p>
</div>
<![endif]-->
<style>
.g_tips{background:#FFF691;color:#010101;text-align:center;height:40px;line-height:40px;
        font-family:'STHeiti','\5fae\8f6f\96c5\9ed1'}
.g_tips a{color:#676767;padding:0 2px;zoom:1}
</style>

用條件註解包住,不支援的瀏覽器根本不會下載這段樣式,是很乾淨的降級手法。

五、一份可以照抄的頁面骨架

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
  <meta charset="UTF-8">
  <title>頁面名稱</title>
  <meta name="keywords" content="">
  <meta name="description" content="">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">

  <!-- Mobile Specific Metas -->
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta name="apple-mobile-web-app-capable" content="yes" />

  <!-- CSS -->
  <link rel="shortcut icon" href="favicon.ico">
  <link rel="stylesheet" href="css/index.css">
  <style>/* 頁面樣式 */</style>
</head>

<body>
  <!--[if lt IE 8]>
  <div class="g_tips">
    <p>你的瀏覽器版本較舊,建議升級到
    <a target="_blank" href="https://www.google.com/chrome/">Google Chrome</a>
    或
    <a target="_blank" href="https://www.microsoft.com/">Internet Explorer</a>
    以獲得完整體驗。</p>
  </div>
  <![endif]-->

  <h1>頁面主標題</h1>

  <script src="js/index.js"></script>
  <script>// 頁面腳本</script>
</body>
</html>

六、幾個容易被忽略的細節

lang 屬性。用來宣告文件的語言基礎,對螢幕閱讀器與搜尋引擎都有意義,多語系網站尤其該補上。

樣式在前,腳本在後。CSS 放 head 讓頁面更早完成首次渲染,JS 放 body 末尾避免阻塞內容呈現,這條規則幾乎沒有例外。

內外與否要看場合。頁面夠簡單時,把樣式和腳本直接寫進頁面完全沒問題,小型專題頁就是典型場景;但在正式專案裡,請一律外掛成獨立檔案,才能被快取與複用。

全域變量的克制。頁面的全域命名要有一致的約定,例如 PDC、FP、PageInfo、window.g_config,並且用立即執行函式包起來,避免把一堆變數丟進全域作用域:

(function () {
  window.PDC = {};
})();
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved