DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 亮點速查:從本地儲存到 Canvas 的程式碼備忘
編輯:HTML5詳解     

以下整理 HTML5 各項能力的簡短範例,可作為開發時的速查參考。部分 API 帶有廠商私有前綴,實際使用時建議搭配 Chrome 或 Safari 等 WebKit 系瀏覽器測試,並加上能力偵測。

本地儲存 — Web Storage

// localStorage 用於長期保存;sessionStorage 則限於單一分頁
textarea.addEventListener('keyup', function () {
  window.localStorage['value'] = area.value;
  window.localStorage['timestamp'] = (new Date()).getTime();
}, false);

textarea.value = window.localStorage['value'];

本地資料庫 — Web SQL Database

var db = window.openDatabase("Database Name", "Database Version");

db.transaction(function (tx) {
  tx.executeSql("SELECT * FROM test", [], successCallback, errorCallback);
});

檔案快取 — Application Cache API

<html manifest="cache-manifest">

window.applicationCache.addEventListener('checking', updateCacheStatus, false);

對應的 cache-manifest 內容:

CACHE MANIFEST
# version 1

CACHE:
/html5/src/refresh.png
/html5/src/logic.js
/html5/src/style.css
/html5/src/background.png

背景執行 — Web Workers

// main.js
var worker = new Worker('extra_work.js');
worker.onmessage = function (event) {
  alert(event.data);
};

// extra_work.js
// 進行運算,完成後回傳訊息
postMessage(some_data);

雙向通訊 — Web Sockets

var socket = new WebSocket(location);

socket.onopen = function (event) {
  socket.postMessage("Hello, WebSocket");
};
socket.onmessage = function (event) {
  alert(event.data);
};
socket.onclose = function (event) {
  alert("closed");
};

桌面通知 — Notifications

if (window.webkitNotifications.checkPermission() == 0) {
  window.webkitNotifications
    .createNotification(tweet.picture, tweet.title, tweet.text)
    .show();
} else {
  window.webkitNotifications.requestPermission();
}

拖放 — Drag and Drop

document.addEventListener('dragstart', function (event) {
  event.dataTransfer.setData('text', 'Customized text');
  event.dataTransfer.effectAllowed = 'copy';
}, false);

新的語意標籤

<body>
  <header>
    <hgroup>
      <h1>Page title</h1>
      <h2>Page subtitle</h2>
    </hgroup>
  </header>
  <nav>
    <ul>Navigation...</ul>
  </nav>
  <section>
    <article>
      <header><h1>Title</h1></header>
      <section>Content...</section>
    </article>
  </section>
  <aside>Top links...</aside>
  <footer>Copyright &copy; 2009.</footer>
</body>

新的連結屬性

<link rel="alternate">   <link rel="icon">
<link rel="nofollow">    <link rel="prefetch">

<a rel="archives">  <a rel="external">  <a rel="license">
<a rel="noreferrer"> <a rel="pingback"> <a rel="sidebar">
<a rel="tag">

新的表單元素型別

<input type="range" min="0" max="50" value="0">
<input autofocus type="search">
<input type="text" placeholder="Search inside">
<menu>
<progress>
<input type="color">   <input type="number">
<input type="email">   <input type="tel">
<input type="time">    <input type="date">
<input type="month">   <input type="week">
<input type="datetime">

音訊與影片 — Audio + Video

<audio src="sound.mp3" controls></audio>
document.getElementById("audio").muted = false;

<video src="movie.mp4" autoplay controls></video>
document.getElementById("video").play();

圖形繪製 — Canvas

<canvas id="canvas" width="838" height="220"></canvas>

<script>
var canvasContext = document.getElementById("canvas").getContext("2d");
canvasContext.fillRect(250, 25, 150, 100);
canvasContext.beginPath();
canvasContext.arc(450, 110, 100, Math.PI * 1/2, Math.PI * 3/2);
canvasContext.lineWidth = 15;
canvasContext.lineCap = 'round';
canvasContext.strokeStyle = 'rgba(255, 127, 0, 0.5)';
canvasContext.stroke();
</script>

CSS3:自訂字體

@font-face { font-family: 'Junction'; src: url(Junction02.otf); }
@font-face { font-family: 'LeagueGothic'; src: url(LeagueGothic.otf); }

header { font-family: 'LeagueGothic'; }

CSS3:結構偽類與選擇器

.row:nth-child(even) { background: #dde; }
.row:nth-child(odd)  { background: white; }

:not(.box) { color: #00c; }
:not(span) { display: block; }

h2:first-child { ... }
div.text > div { ... }
h2 + header    { ... }
input[type="text"] { background: #eee; }

CSS3:多欄排版

-webkit-column-count: 6;
-webkit-column-rule: 1px solid #bbb;
-webkit-column-gap: 2em;

CSS3:文字描邊

-webkit-text-fill-color: black;
-webkit-text-stroke-color: red;
-webkit-text-stroke-width: 0.00px;

CSS3:透明與 HSLA 色彩

color: rgba(255, 0, 0, 0.41);
background: rgba(0, 0, 255, 0.19);

color: hsla(184, 75%, 33%, 0.15);

CSS3:圓角與漸層

border-radius: 7px;

background: -webkit-gradient(linear, left top, left bottom,
  from(#00abeb), to(white),
  color-stop(0.5, white), color-stop(0.5, #66cc00));

background: -webkit-gradient(radial, 430 50, 0, 430 50, 590,
  from(red), to(#000));

text-shadow: rgba(0, 0, 0, 0.5) 0 -1px -1px;

CSS3:倒影效果

-webkit-box-reflect: below 10px
  -webkit-gradient(linear, left top, left bottom,
    from(transparent), to(rgba(255, 255, 255, 0.78)));

CSS3:轉場與動畫

#box {
  -webkit-transition: margin-left 1s ease-in-out;
}

@-webkit-keyframes pulse {
  from { opacity: 0.0; font-size: 100%; }
  to   { opacity: 1.0; font-size: 200%; }
}

div {
  -webkit-animation-name: pulse;
  -webkit-animation-duration: 2s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-timing-function: ease-in-out;
  -webkit-animation-direction: alternate;
}

使用提醒

上面這些 -webkit- 前綴的屬性在制訂當時多屬實驗性質,規格與語法後續有所調整,部分寫法(如舊版的 -webkit-gradient)已被標準的 linear-gradient、radial-gradient 取代;Web SQL Database 也已被廢棄,改用 IndexedDB。這份清單適合理解當時的能力邊界,實際專案請以現行標準為準。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved