<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>彬Go &#187; 技术</title>
	<atom:link href="http://blog.bingo929.com/category/technology/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bingo929.com</link>
	<description>关注前端开发/网页设计/网站可用性/用户体验</description>
	<lastBuildDate>Fri, 16 Dec 2011 17:14:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)</title>
		<link>http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html</link>
		<comments>http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html#comments</comments>
		<pubDate>Sun, 11 Dec 2011 15:30:11 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[我的作品]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[拖拽]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=3186</guid>
		<description><![CDATA[　　早在公元2011年6月3日傍晚，人人网推出了一个很装B且完全无视IE浏览器的功能——拖拽上床。哦，Sorry, 是拖拽上传。到现在，这个功能已经上线了整整半年，这篇文章也足足拖延了半年才... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html"><img title="人人网拖拽上传实例详解" src="http://blog.bingo929.com/wp-content/uploads/2011/12/blog-title.jpg" alt="" width="500" height="150" /></a></p>
<p>　　早在公元2011年6月3日傍晚，人人网推出了一个很装B且完全无视IE浏览器的功能——拖拽上床。哦，Sorry, 是<strong>拖拽上传</strong>。到现在，这个功能已经上线了整整半年，这篇文章也足足拖延了半年才分享给大家，实在是对不住了，呵呵，今后彬Go一定要勤奋发Blog！</p>
<p><span id="more-3186"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下HTML5相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html">HTML5 Drag&amp;Drop 拖拽、FileReader实例教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-websockets.html">HTML5 WebSockets 基础使用教程</a>》<br />
《<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">关于HTML 5 canvas 的基础教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-ie-enabling-script.html">让所有IE支持HTML5的解决方案</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》</p>
<p>　　关于这个拖拽上传，其实国外有很多网站已经有这样的应用，最早推出拖拽上传应用的是Gmail，它支持标准浏览器下拖拽本地文件到浏览器中作为邮件的附件发送。人人网的这个拖拽上传也是同理，可以让使用标准浏览器的用户通过简单的拖拽行为，将本地文件夹中的照片直接上传到人人网，用户体验能得到提升的同时，也希望借此机会推广一下标准浏览器，淘汰IE。人人网当时也向广大用户推出升级浏览器活动，并喊出口号：&#8221;工欲善其计算机，必先利其浏览器&#8221;。本次拖拽上传的宣传口号：你敢&#8221;脱&#8221;，我就敢上传&#8230;</p>
<p><a href="http://blog.bingo929.com/wp-content/uploads/2011/12/b_large_pbOj_3004000003b45c41.jpg"><img title="人人网 - 拖拽上传" src="http://blog.bingo929.com/wp-content/uploads/2011/12/b_large_pbOj_3004000003b45c41-500x259.jpg" alt="人人网 - 拖拽上传" width="500" height="259" /></a></p>
<p>　　言归正题，首先看看效果，大家如果有<a href="http://www.renren.com" target="_blank">人人网</a>帐号的话可以在首页试一试拖拽上传功能，下面是演示视频：</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="500" height="430" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="align" value="middle" /><param name="src" value="http://player.youku.com/player.php/sid/XMjcyNzU0ODY4/v.swf" /><param name="allowfullscreen" value="true" /><param name="quality" value="high" /><embed type="application/x-shockwave-flash" width="500" height="430" src="http://player.youku.com/player.php/sid/XMjcyNzU0ODY4/v.swf" quality="high" allowfullscreen="true" align="middle"></embed></object></p>
<p><strong>拖拽上传应用主要使用了以下<a href="http://blog.bingo929.com/tag/html5">HTML5</a>技术：</strong></p>
<ul>
<li><a href="http://dev.w3.org/html5/spec/Overview.html#dnd" target="_blank"><strong>Drag&amp;Drop</strong></a> : <a href="http://blog.bingo929.com/tag/html5">HTML5</a>基于拖拽的事件机制.</li>
<li><a href="http://www.w3.org/TR/FileAPI/" target="_blank"><strong>File API</strong></a> :  可以很方便的让Web应用访问文件对象，File API包括<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-filelist">FileList</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-Blob">Blob</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-file">File</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#dfn-filereader">FileReader</a>、<a style="font-weight: normal;" href="http://dev.w3.org/2006/webapi/FileAPI/#url">URI scheme</a>，本文主要讲解拖拽上传中用到的FileList和FileReader接口。</li>
<li><a href="http://www.w3.org/TR/XMLHttpRequest2/#the-formdata-interface" target="_blank"><strong>FormData</strong></a> : FormData是基于<a style="font-weight: normal;" href="http://www.w3.org/TR/XMLHttpRequest2/" target="_blank">XMLHttpRequest Level 2</a>的新接口，可以方便web应用模拟Form表单数据，最重要的是它支持文件的二进制流数据，这样我们就能够通过它来实现AJAX向后端发送文件数据了。</li>
</ul>
<h3><strong>HTML5 Drag&amp;Drop 拖拽事件</strong></h3>
<p>　　关于Drag&amp;Drop拖拽事件，之前我写过一篇专门介绍的文章<a style="font-weight: bold;" href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html" target="_blank">《给力的 Google HTML5 训练营(HTML5 Drag&amp;Drop 拖拽、FileReader实例教程)》</a>，那篇文章详细讲解了Drag &amp; Drap事件的原理和代码实例，这里的拖拽上传实现原理基本上是一样的，大家有兴趣或不太了解的话可以先看看那篇文章，我在这里就不再过多啰嗦了~下面直接出拖拽上传简要代码实例：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> oDragWrap <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//拖进</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragenter'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//拖离</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragleave'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　dragleaveHandler<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//拖来拖去 , 一定要注意dragover事件一定要清除默认事件</span><br />
<span style="color: #006600; font-style: italic;">//不然会无法触发后面的drop事件</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//扔</span><br />
oDragWrap.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'drop'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　dropHandler<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> dropHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #006600; font-style: italic;">//将本地图片拖拽到页面中后要进行的处理都在这</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>获取文件数据 HTML5 File API</h3>
<p>　　在之前<a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html" target="_blank">那篇文章</a>中我也有介绍过关于File API中的FileReader接口，作为 <a href="http://www.w3.org/TR/FileAPI/" target="_blank">File API</a> 的一部分，<a href="http://www.w3.org/TR/FileAPI/#dfn-filereader" target="_blank">FileReader</a> 专门用于读取文件，根据 W3C 的定义，FileReader 接口 “提供一些读取文件的方法与一个包含读取结果的事件模型”。关于FileReader的详细介绍和代码实例大家可以先去看看那篇<a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html" target="_blank">文章</a>。</p>
<p>　　今天我着重介绍一下File API中的FileList接口,它主要通过两个途径获取本地文件列表，一是<strong>&lt;input type=&#8221;file&#8221;&gt;</strong>的表单形式，另一种则是<strong>e.dataTransfer.files</strong>拖拽事件传递的文件信息。很显然，我们这里会用到后者。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> fileList <span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span></div></div>
<p>　　使用<strong>files方法</strong>将会获取到拖拽文件的数组形势的数据，每个文件占用一个数组的索引，如果该索引不存在文件数据，将返回null值。可以通过<strong>length</strong>属性获取文件数量.</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> fileNum <span style="color: #339933;">=</span> fileList.<span style="color: #660066;">length</span><span style="color: #339933;">;</span></div></div>
<p>　　拖拽上传需要注意的是需要判断两个条件，1:拖拽的是文件不是页面中的元素; 2:拖拽的是图片而不是其它文件,可以通过file.type属性获取文件的类型</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//检测是否是拖拽文件到页面的操作</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList.<span style="color: #660066;">length</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">//检测文件是不是图片</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></div></div>
<p>　　下面让我们来看看如何结合之前的拖拽事件来实现拖拽图片并在页面中进行预览：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> dropHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//获取文件列表</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> fileList <span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测是否是拖拽文件到页面的操作</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测文件是不是图片</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//实例化file reader对象</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　reader.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span><br />
　　oDragWrap.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #009900;">&#125;</span><br />
　reader.<span style="color: #660066;">readAsDataURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　这里有一个简单的<a href="http://www.bingo929.com/mytest/dragtest.html" target="_blank">拖拽图片预览的Demo</a></p>
<p>　　这时你如果用FireBug等类似调试工具查看DOM的话，会看到&lt;img&gt;标签的src属性是一个超长的文件二进制数据，所以如果DOM有很多这类图片，那就要当心浏览器性能了，因为这些数据极大地扩充的页面的代码量，而每次页面的reflow都会对浏览器形成很大的负担，So,如果这些图片还在DOM中，那就尽量不要做动画或任何重绘操作，如果真的要做就尽量让图片脱离文档流，让其绝对定位比较靠谱。</p>
<p style="background:#FFF7D1;padding:10px;color:#D6006D;">
补充：可以使用<strong><a href="https://developer.mozilla.org/en/DOM/window.URL.createObjectURL" target="_blank">window.URL.createObjectURL(file)</a></strong>来获取文件的URL(Chrome下用<strong>window.webkitURL.createObjectURL(file)</strong>)，这种方式获取的URL要比上面说的<strong>readAsDataURL</strong>简短很多。而且可以省去使用FileReader。下面是使用readAsDataURL与createObjectURL生成的&lt;img&gt;代码对比：
</p>
<p><a href="http://blog.bingo929.com/wp-content/uploads/2011/12/readasdataurl-vs-createobjecturl.png"><img src="http://blog.bingo929.com/wp-content/uploads/2011/12/readasdataurl-vs-createobjecturl-500x241.png" alt="readasdataurl-vs-createobjecturl" title="readAsDataURL与createObjectUrl对比" width="500" height="241" class="alignnone size-medium wp-image-3247" /></a></p>
<p>优化后的代码：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> dropHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #003366; font-weight: bold;">var</span> fileList <span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span>　　<span style="color: #006600; font-style: italic;">//获取文件列表</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测是否是拖拽文件到页面的操作</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
　<span style="color: #006600; font-style: italic;">//检测文件是不是图片</span><br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">type</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'image'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><br />
　<br />
　<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">URL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　<span style="color: #006600; font-style: italic;">//FF4+</span><br />
　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">URL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">webkitURL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　<span style="color: #006600; font-style: italic;">//Chrome8+</span><br />
　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">webkitURL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
　　<span style="color: #006600; font-style: italic;">//实例化file reader对象</span><br />
　　<span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　　reader.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　　img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span><br />
　　　oDragWrap.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　　<span style="color: #009900;">&#125;</span><br />
　　reader.<span style="color: #660066;">readAsDataURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　需要注意的是，<strong>window.URL.createObjectURL</strong>是有生命周期的，也就意味着你每用此方法获取URL，其生命周期都会和DOM一样，它会单独占用内存，所以当删除图片或不再需要它是，记得用<strong>window.URL.revokeObjectURL(file)</strong>来释放其内存。当然，如果你没有释放，刷新页面也是可以释放的。</p>
<h3>AJAX上传图片(file.getAsBinary &amp; FormData)</h3>
<p>　　既然已经获取到了拖拽到web页面中图片的数据，下一步就是将其发送到服务器端了。</p>
<p>　　话说HTML5时代之前，AJAX传输文件二进制流数据是不可能完成的事情，而现在我们完全可以通过<strong>file.getAsBinary</strong>获取文件的二进制数据流，进而将其当做XHR的data数据传送到后端，8过由于Chrome不支持file的getAsBinary方法，FF3.6+支持此方法。所以Chrome就要另寻它法了，这时我们发现<a href="http://www.w3.org/TR/XMLHttpRequest2/" target="_blank">XMLHttpRequest Level 2</a>中的FormData接口完美解决了这个问题，它可以很快捷的模拟Form表单数据并通过AJAX发送至后端，FormData的支持情况是FF5及以上支持，Chrome12及以上支持。</p>
<p>　　<strong> file.getAsBinary</strong>获取文件流很简单，但是要想上传数据，就要模拟一下表单的数据格式了，首先看看模拟表单的js代码, <strong>FormData</strong>模拟表单数据时更是简洁，不用麻烦的去拼字符串，而是直接将数据append到formdata对象中即可：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://upload.renren.com/......'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> boundary <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-----------------------'</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> fileName <span style="color: #339933;">=</span> file.<span style="color: #000066;">name</span><span style="color: #339933;">;</span><br />
<br />
xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;post&quot;</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'multipart/form-data; boundary='</span> <span style="color: #339933;">+</span> boundary<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">FormData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　<span style="color: #006600; font-style: italic;">//Chrome12+</span><br />
　<span style="color: #003366; font-weight: bold;">var</span> formData <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FormData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　formData.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'file'</span><span style="color: #339933;">,</span> file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　formData.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hostid'</span><span style="color: #339933;">,</span> userId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　formData.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'requestToken'</span><span style="color: #339933;">,</span> t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　data <span style="color: #339933;">=</span> formData<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>file.<span style="color: #660066;">getAsBinary</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　<span style="color: #006600; font-style: italic;">//FireFox 3.6+</span><br />
　data <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Disposition: form-data; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;name=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">'file'</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;filename=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　unescape<span style="color: #009900;">&#40;</span>encodeURIComponent<span style="color: #009900;">&#40;</span>file.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Type: image/jpeg&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　file.<span style="color: #660066;">getAsBinary</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Disposition: form-data; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;name=<span style="color: #000099; font-weight: bold;">\&quot;</span>hostid<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　userId <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;Content-Disposition: form-data; &quot;</span> <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;name=<span style="color: #000099; font-weight: bold;">\&quot;</span>requestToken<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　t <span style="color: #339933;">+</span><br />
　crlf <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">&quot;--&quot;</span> <span style="color: #339933;">+</span><br />
　boundary <span style="color: #339933;">+</span><br />
　<span style="color: #3366CC;">'--'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>首先表单数据headers头信息需要以下两项：</p>
<ul>
<li>Content-Type ： 设置其为multipart/form-data来模拟表单数据</li>
<li>boundary ： 表单数据中的分隔符，用于分隔不同的文件或表单项，这是服务器端设置的格式。</li>
</ul>
<p>发送时的post数据类似这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">-------------------------</span><span style="color: #CC0000;">1323611763556</span><br />
Content<span style="color: #339933;">-</span>Disposition<span style="color: #339933;">:</span> form<span style="color: #339933;">-</span>data<span style="color: #339933;">;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;file&quot;</span><span style="color: #339933;">;</span> filename<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;4.jpg&quot;</span><br />
Content<span style="color: #339933;">-</span>Type<span style="color: #339933;">:</span> image<span style="color: #339933;">/</span>jpeg<br />
<br />
ÿØÿà�JFIF�...这里是文件二进制流...~iúoî­5P<span style="color: #339933;">%-</span>vãîHü 4QHgÿÙ<br />
<span style="color: #339933;">-------------------------</span><span style="color: #CC0000;">1323611763556</span><br />
Content<span style="color: #339933;">-</span>Disposition<span style="color: #339933;">:</span> form<span style="color: #339933;">-</span>data<span style="color: #339933;">;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;hostid&quot;</span><br />
<br />
<span style="color: #CC0000;">229421603</span><br />
<span style="color: #339933;">-----------------------------</span><span style="color: #CC0000;">1323612996486</span><br />
<br />
Content<span style="color: #339933;">-</span>Disposition<span style="color: #339933;">:</span> form<span style="color: #339933;">-</span>data<span style="color: #339933;">;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;requestToken&quot;</span><br />
<br />
<span style="color: #CC0000;">369009193</span><br />
<span style="color: #339933;">-------------------------</span><span style="color: #CC0000;">1323611763556</span><span style="color: #339933;">--</span></div></div>
<p>好了，现在文件上传成功后你就可以按照平常AJAX的操作来进行后续处理了。</p>
<p>最后，再来总结一下拖拽上传的技术要点：</p>
<ol>
<li><strong>监听拖拽</strong>：监听页面元素的拖拽事件，包括：<strong>dragenter、dragover、dragleave和drop</strong>，一定要将dragover的默认事件取消掉，不然无法触发drop事件。如需拖拽页面里的元素，需要给其添加属性draggable=&#8221;true&#8221;；</li>
<li><strong>获取拖拽文件</strong>：在drop事件触发后通过<strong>e.dataTransfer.files</strong>获取拖拽文件列表，<strong>.length</strong>属性获取文件数量，.type属性获取文件类型。</li>
<li><strong>读取图片数据并添加预览图</strong>：实例化<strong>FileReader</strong>对象，通过其<strong>readAsDataURL(file)</strong>方法获取文件二进制流，并监听其<strong>onload事件</strong>，将<strong>e.result</strong>赋值给img的src属性，最后将图片append到DOM中。</li>
<li><strong>发送图片数据</strong>：使用<strong>file.getAsBinary</strong> 和 <strong>FormDat</strong>a分别模拟表单数据AJAX提交文件流。</li>
</ol>
<p>　　OK,拖拽上传就讲到这里，欢迎大家一起探讨。也在这里推荐一下<a href="http://fed.renren.com" target="_blank"><strong>人人网FED的Blog</strong></a>，大家多多捧场，哈~</p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看HTML5最新资源、教程。</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——关注前端开发 &amp; Web UI设计<br />
本文链接:<a title="人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)" href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html">http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html</a><br />
<a title="人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)" href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html"> </a><a title="人人网首页拖拽上传详解(HTML5 Drag&amp;Drop、FileReader API、formdata)" href="http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html"></a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/renren-drag-drop-photo-filereader-formdata.html/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>给力的 Google HTML5 训练营(HTML5 Drag&amp;Drop 拖拽、FileReader实例教程)</title>
		<link>http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html</link>
		<comments>http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html#comments</comments>
		<pubDate>Tue, 11 Jan 2011 07:36:35 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Drag&Drop]]></category>
		<category><![CDATA[FileReader]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=3120</guid>
		<description><![CDATA[　　上周六有幸同我们人人网的几个同事一起去Google总部参加了《给力HTML5 —— 2011 Google HTML5 训练营》，从下午1点多到晚上9点多8个小时左右的时间，收获还是很大的，8个小时的时间把以前一... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html"><img src="http://blog.bingo929.com/wp-content/uploads/2011/01/HTML5-drag-drop-filereader.jpg" alt="HTML5 Drag&amp;Drop 、FileReader实例教程" title="HTML5-drag-drop-filereader" width="500" height="150" class="alignnone size-full wp-image-3176" /></a></p>
<p>　　上周六有幸同我们人人网的几个同事一起去Google总部参加了<strong>《给力HTML5 —— 2011 Google HTML5 训练营》</strong>，从下午1点多到晚上9点多8个小时左右的时间，收获还是很大的，8个小时的时间把以前一直想学或想做的事情都给搞了一些。好久没有写博客了，今天也该分享些给力的东西了，正好借此次机会跟大家分享些HTML5相关的给力的技术（sessionStorage、localStorage、Web SQL DateBase、Indexed DB、Canvas绘图、FileReader、Drag&#038;Drop&#8230;），何止是给力，简直就是给力！<span id="more-3120"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下HTML5相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/html5-websockets.html">HTML5 WebSockets 基础使用教程</a>》<br />
《<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">关于HTML 5 canvas 的基础教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-ie-enabling-script.html">让所有IE支持HTML5的解决方案</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》</p>
<h3>Google HTML5 训练营</h3>
<p style="color:#666;font-size:12px">先简单介绍一下这个训练营的活动流程：<br />
　　　1:00 &#8211; 1:30 注册<br />
　　　1:30 &#8211; 2:30 HTML5 技术剖析<br />
　　　技术讲座1 —— Google 工程师<br />
　　　技术讲座2 —— 特邀嘉宾（李靖威、李继成，就职于人人网）<br />
　　　2:30 &#8211; 3:00 宣布竞赛规则，开始组队<br />
　　　3:00 &#8211; 6:30 作品创作 / 提交<br />
　　　6:30 &#8211; 7:00 作品展示<br />
　　　7:00 &#8211; 7:30 评奖、颁奖</p>
<p>　　讲座部分，先是Google工程师寒蕊MM跟我们分享了些HTML5的新技术概况，其中大致包括：<a href="http://dev.w3.org/html5/webstorage/#dom-sessionstorage" target="_blank">sessionStorage</a>、<a href="http://dev.w3.org/html5/webstorage/#dom-localstorage" target="_blank">localStorage</a>、<a href="http://dev.w3.org/html5/webdatabase/#web-sql" target="_blank">Web SQL DateBase</a> 、<a href="http://www.chromium.org/developers/design-documents/indexeddb" target="_blank">Indexed DB</a>，寒蕊MM还专门写了篇关于IndexedDB的文章<a href="http://enjoyhtml5.com/blogs/storage.html" target="_blank">《客户端数据存储》</a>，文章用实例的方式讲了Web Storage(localStorage)、Web SQL Database 和 Indexed Database的用法。</p>
<p>　　其中Web SQL Database虽是HTML5的技术，但由于某些原因，W3C组织(Web Applications Working Group)已不再维护这项技术，也就是说虽然目前Chrome、FireFox新版本浏览器支持这项技术，但不一定未来的版本还会支持。以下为w3c官网的声明：
<p style="background: black; color: white; font: 900 2em serif; padding: 0.5em 1em; border: dotted yellow 0.5em; text-align: center">Beware. This specification is no longer in active maintenance and the Web<br />
Applications Working Group does not intend to maintain it further.</p>
<p>　　Indexed DataBase最初是由Oracle提出的数据库API，没记错的话起初叫做Simple DataBase，之后演变成Indexed DataBase，而且已被Chrome和FireFox(版本4)所支持。</p>
<p>　　这次活动规模比较小，由于人数限制在了30人，所以组队Coding阶段，每队5人，分成了6组~接下来就是短短3个小时左右的Coding了，我们人人网的四个人和另一位有Canvas经验的邱亮同学一起开发一个纯HTML5的可涂鸦日记本，其中有个插曲:一开始项目名字没多想就直接叫做《我的日记本》了，到最后提交项目的时候，Google MM说要起一个简洁且酷一些的名字，好吧，那我们就起一个简洁酷一些的名字，最后就叫《我日》！哇咔咔~让大家&#8221;贱笑&#8221;了~</p>
<p><a href="http://blog.bingo929.com/wp-content/uploads/2011/01/html5-daily-book.jpg"><img src="http://blog.bingo929.com/wp-content/uploads/2011/01/html5-daily-book-500x406.jpg" alt="html5-daily-book" title="html5-daily-book" width="500" height="406" class="alignnone size-medium wp-image-3140" /></a></p>
<p>　　言归正传，《我日》项目之所以叫纯HTML5，其实只是使用了些HTML5技术，在短短的3个小时时间，从功能到分工开发，时间很紧~所以只是做了个小小的Demo版，基本功能实现的差不多了，但愿以后还能有时间把它完善一下~其中使用了这些HTML5技术：</p>
<p>　　1.Web SQL DataBase ：存取日记数据<br />
　　2.HTML5 Drag&#038;Drop 事件 ：监听网页内图片拖拽及本地图片直接拖拽到日记中的事件<br />
　　3.FileReader ：获取本地图片拖拽到日记本里的图片数据<br />
　　4.<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">Canvas</a> ：实现日记涂鸦功能(画笔、调色、文字、图片绘制到画布等&#8230;)<br />
　　5.HTML5新标签 ：如:&lt;input type=&#8221;range&#8221;&#8230; /&gt; 、 &lt;section&gt; 、&lt;article&gt;等&#8230;<br />
　　6.<a href="http://blog.bingo929.com/tag/css3">CSS3</a></p>
<p>　　今天我只详细讲讲我的分工部分：HTML5 Drag &#038; Drop 和 FireReader这两个东西，以后有机会在和大家分享其他技术。</p>
<h3><a href="http://dev.w3.org/html5/spec/dnd.html" target="_blank">HTML5 Drag &#038; Drop 事件</a></h3>
<p>　　过去我们想实现网页中的拖拽效果，基本上都是使用DOM事件模型中的mousedown、mousemove、mouseup的事件监听来模拟拖拽效果，为了实现实时的拖拽移动效果，还要不停地获取鼠标的坐标，还要不停的修改元素的位置，代码要堆很多，而且性能上也很不好(不停地修改元素位置会导致页面reflow，除非绝对定位)，现在有了html5原生的Drag &#038; Drop 拖拽事件，真的是方便了许多，用&#8221;事半功倍&#8221;来形容绝不为过。</p>
<p><strong>Drag &#038; Drop 包括以下事件</strong>：
<ul>
<li><strong>dragstart</strong>：要被拖拽的元素开始拖拽时触发，这个事件对象是被拖拽元素</li>
<li><strong>dragenter</strong>：拖拽元素进入目标元素时触发，这个事件对象是目标元素</li>
<li><strong>dragover</strong>：拖拽某元素在目标元素上移动时触发，这个事件对象是目标元素</li>
<li><strong>dragleave</strong>：拖拽某元素离开目标元素时触发，这个事件对象是目标元素</li>
<li><strong>dragend</strong>：在drop之后触发，就是拖拽完毕时触发，这个事件对象是被拖拽元素</li>
<li><strong>drop</strong>：将被拖拽元素放在目标元素内时触发，这个事件对象是目标元素</li>
</ul>
<p>完成一次成功页面内元素拖拽的行为事件过程应该是： dragstart &#8211;> dragenter &#8211;> dragover &#8211;> drop &#8211;> dragend</p>
<h4 style="font-size:16px;">Drag &#038; Drop 网页内的元素拖拽：</h4>
<p>　　HTML5为元素新增了用于拖拽的属性<strong>draggable</strong>，这个属性决定了元素是否能被拖拽，如果draggable=&#8221;true&#8221;，则元素可被拖拽，否则只能选择元素的文本。</p>
<p>　　值得一提的是HTML5支持拖拽数据存储，使用<strong>dataTransfer</strong>接口，作用于元素的拖拽基础上，dataTransfer对象包含以下属性和方法：
<ul>
<li><strong>dataTransfer.dropEffect [ = value ]</strong>：返回已选择的拖放效果，如果该操作效果与起初设置的effectAllowed效果不符，则拖拽操作失败。可以设置修改，包含这几个值：<i>&#8220;none&#8221;, &#8220;copy&#8221;, &#8220;link&#8221; 和 &#8220;move&#8221;</i>。</li>
<li><strong>dataTransfer.effectAllowed [ = value ]</strong>：返回允许执行的拖拽操作效果，可以设置修改，包含这些值：<i>&#8220;none&#8221;, &#8220;copy&#8221;, &#8220;copyLink&#8221;, &#8220;copyMove&#8221;, &#8220;link&#8221;, &#8220;linkMove&#8221;, &#8220;move&#8221;, &#8220;all&#8221; 和 &#8220;uninitialized&#8221;</i></li>
<li><strong>dataTransfer.types</strong>：返回在dragstart事件出发时为元素存储数据的格式，如果是外部文件的拖拽，则返回&#8221;files&#8221;</li>
<li><strong>dataTransfer.clearData ( [ format ] )</strong>：删除指定格式的数据，如果未指定格式，则删除当前元素的所有携带数据</li>
<li><strong>dataTransfer.setData(format, data)</strong>：为元素添加指定数据</li>
<li><strong>dataTransfer.getData(format)</strong>：返回指定数据，如果数据不存在，则返回空字符串</li>
<li><strong>dataTransfer.files</strong>：如果是拖拽文件，则返回正在拖拽的文件列表FileList</li>
<li><strong>dataTransfer.setDragImage(element, x, y)</strong>：制定拖拽元素时跟随鼠标移动的图片，x、y分别是相对于鼠标的坐标(据测试，Chrome暂不支持)</li>
<li><strong>dataTransfer.addElement(element)</strong>：添加一起跟随拖拽的元素，如果你想让某个元素跟随被拖拽元素一同被拖拽，则使用此方法(据测试，Chrome暂不支持)</li>
</ul>
<p>在dragstart事件触发时可以为被拖拽元素存储数据，就是使用上面说到的<strong>dataTransfer.setData</strong>，setData的数据格式一般有两种：&#8221;text/plain&#8221;(用于文本数据)和&#8221;text/uri-list&#8221;(用于url)，你可以先为某个可拖拽元素设置微数据，然后为它设置draggable属性为true，之后在其dragstart事件触发时存储数据：<br />
html部分：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;dragMe&quot;</span> builddate<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2011-1-10&quot;</span> draggable<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;true&quot;</span>&gt;</span>拖拽我！<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;dropHere&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></div>
<p>javascript部分：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> oDragMe <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragMe'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
oDragMe.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragstart'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/plain'</span><span style="color: #339933;">,</span> e.<span style="color: #660066;">target</span>.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'builddate'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span></div></div>
<p>在拖拽结束的时候便可以获取相应元素的数据:</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> oDropBox <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dropHere'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
　　tmpData<span style="color: #339933;">;</span><br />
<br />
oDropBox .<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'drop'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　tmpData <span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span></div></div>
<p>创建拖拽事件监听的时候记得要把默认的行为事件去掉，毕竟浏览器是有默认拖拽行为的,尤其是dragover事件一定要使用e.preventDefault()，不然drop事件可能不会被触发：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">oDropBox .<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　e.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span></div></div>
<p>上面讲的这些基本上就能完成页面内元素的拖拽操作了，下面我们来说说从本地拖拽图片到页面元素中用到的技术：</p>
<h4 style="font-size:16px;">FileReader获取本地拖拽文件</h4>
<p><a href="http://www.bingo929.com/mytest/dragtest.html" title="彬Go - HTML5 Drag &#038; Drop 、FileReader 实例">点击查看实例Demo</a></p>
<p>　　作为 <a href="http://www.w3.org/TR/FileAPI/" target="_blank">File API</a> 的一部分，<a href="http://www.w3.org/TR/FileAPI/#dfn-filereader" target="_blank">FileReader</a> 专门用于读取文件，根据 W3C 的定义，FileReader 接口 &#8220;提供一些读取文件的方法与一个包含读取结果的事件模型&#8221;。</p>
<p>来看看FileReader支持的方法、属性和事件：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#91;</span>Constructor<span style="color: #009900;">&#93;</span><br />
interface FileReader <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// 异步读取方法</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">void</span> readAsArrayBuffer<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">in</span> Blob blob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">void</span> readAsBinaryString<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">in</span> Blob blob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">void</span> readAsText<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">in</span> Blob blob<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>Optional<span style="color: #009900;">&#93;</span> <span style="color: #000066; font-weight: bold;">in</span> DOMString encoding<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">void</span> readAsDataURL<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">in</span> Blob blob<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #000066; font-weight: bold;">void</span> abort<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// 状态</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">const</span> unsigned short EMPTY <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">const</span> unsigned short LOADING <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">const</span> unsigned short DONE <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <br />
&nbsp; readonly attribute unsigned short readyState<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// 文件或二进制数据</span><br />
&nbsp; readonly attribute any result<span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; readonly attribute FileError error<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// 事件处理</span><br />
&nbsp; attribute <span style="color: #003366; font-weight: bold;">Function</span> onloadstart<span style="color: #339933;">;</span><br />
&nbsp; attribute <span style="color: #003366; font-weight: bold;">Function</span> onprogress<span style="color: #339933;">;</span><br />
&nbsp; attribute <span style="color: #003366; font-weight: bold;">Function</span> <span style="color: #000066;">onload</span><span style="color: #339933;">;</span><br />
&nbsp; attribute <span style="color: #003366; font-weight: bold;">Function</span> onabort<span style="color: #339933;">;</span><br />
&nbsp; attribute <span style="color: #003366; font-weight: bold;">Function</span> <span style="color: #000066;">onerror</span><span style="color: #339933;">;</span><br />
&nbsp; attribute <span style="color: #003366; font-weight: bold;">Function</span> onloadend<span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
FileReader implements EventTarget<span style="color: #339933;">;</span></div></div>
<p>今天只说说关于拖拽本地文件到页面的几个相关FileReader属性和事件：</p>
<ul>
<li><strong>readAsDataURL方法</strong>：参数为要读取的文件对象，将文件读取为DataUrl</li>
<li><strong>onload事件</strong>：当读取文件成功完成的时候触发此事件</li>
</ul>
<p>在FileReader onload事件触发后，你可以通过<strong>this.result</strong>来获取读取的文件数据，如果是图片，将返回base64格式的图片数据。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//一定要在dragover事件中取消默认拖拽行为，不然drop事件会失效</span><br />
oDropBox.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
oDropBox.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'drop'</span><span style="color: #339933;">,</span> handleDrop<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> handleDrop<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　e.<span style="color: #660066;">stopPropagation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　　e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　　<span style="color: #003366; font-weight: bold;">var</span> fileList &nbsp;<span style="color: #339933;">=</span> e.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">,</span>　　<span style="color: #006600; font-style: italic;">//获取拖拽文件</span><br />
　　　　oImg <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
　　　　reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
　　reader.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
　　　　oImg.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span><br />
　　　　oDropBox.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>oImg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
　　<span style="color: #009900;">&#125;</span><br />
　　reader.<span style="color: #660066;">readAsDataURL</span><span style="color: #009900;">&#40;</span>fileList<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>　　<span style="color: #006600; font-style: italic;">//这里只取拖拽的第一个，实际中你可以遍历处理file列表</span><br />
｝</div></div>
<p>OK，通过以上代码你就可以完成基本的本地图片拖拽到页面指定元素内的功能了。<br />
<a href="http://www.bingo929.com/mytest/dragtest.html" title="彬Go - HTML5 Drag &#038; Drop 、FileReader 实例">点击查看实例Demo</a></p>
<p><a href="http://www.enjoyhtml5.com/hackathons/20110108/04/index.html" title="我的日记本">《我日》Demo 预览</a></p>
<p>注意，以上demo仅可使用新版Chrome和FireFox查看，IE不支持。</p>
<p>好了，今天先讲这些，今后会更新更多<a href="http://blog.bingo929.com/category/technology/html5">HTML5</a>相关的技术的，希望大家多多关注~</p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看HTML5最新资源、教程。 </p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——关注前端开发 &#038; Web UI设计<br />
本文链接:<a title="HTML5 WebSockets 基础使用教程" href="http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereader.html">http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereader.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/google-enjoy-html5-drag-drop-filereaderenren.html/feed</wfw:commentRss>
		<slash:comments>51</slash:comments>
		</item>
		<item>
		<title>规范自己的JavaScript书写 &#8211; Dojo Javascript 编程规范</title>
		<link>http://blog.bingo929.com/dojo-javascript-style-guide.html</link>
		<comments>http://blog.bingo929.com/dojo-javascript-style-guide.html#comments</comments>
		<pubDate>Mon, 25 Oct 2010 07:46:49 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=3082</guid>
		<description><![CDATA[前言 良好的JavaScript书写习惯的优点不言而喻，今天彬Go向大家推荐Dojo Javascript 编程规范，相当不错的 Javascript 编程风格规范，建议大家可以借鉴一下此规范编写 Javascript。感谢i.feelinglucky的翻... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/dojo-javascript-style-guide.html"><img src="http://blog.bingo929.com/wp-content/uploads/2010/10/title3.jpg" alt="Dojo Javascript 编程规范" title="规范自己的JavaScript书写" width="500" height="150" class="alignnone size-full wp-image-3112" /></a></p>
<h3>前言</h3>
<p>良好的JavaScript书写习惯的优点不言而喻，今天彬Go向大家推荐Dojo <a href="http://blog.bingo929.com/category/technology/javascript">Javascript</a> 编程规范，相当不错的 Javascript 编程风格规范，建议大家可以借鉴一下此规范编写 Javascript。感谢i.feelinglucky的翻译。<br />
<span id="more-3082"></span></p>
<h3>序</h3>
<p>Any violation to this guide is allowed if it enhances readability.</p>
<p>所有的代码都要变成可供他人容易阅读的。</p>
<h3>快读参考</h3>
<p>核心 API 请使用下面的风格：</p>
<table style="border:1px solid #ccc">
<tbody>
<tr>
<td><strong>结构</strong></td>
<td><strong>规则</strong></td>
<td><strong>注释</strong></td>
</tr>
<tr>
<td>模块</td>
<td>小写</td>
<td>不要使用多重语义（Never multiple words）</td>
</tr>
<tr>
<td>类</td>
<td>骆驼</td>
<td></td>
</tr>
<tr>
<td>公有方法</td>
<td>混合</td>
<td>其他的外部调用也可以使用 lower_case()，这样的风格</td>
</tr>
<tr>
<td>公有变量</td>
<td>混合</td>
<td></td>
</tr>
<tr>
<td>常量</td>
<td>骆驼 或 大写</td>
<td></td>
</tr>
</tbody>
</table>
<p>下面的虽然不是必要的，但建议使用：</p>
<table style="border:1px solid #ccc">
<tbody>
<tr>
<td><strong>结构</strong></td>
<td><strong>规则</strong></td>
</tr>
<tr>
<td>私有方法</td>
<td>混合，例子：<tt>_mixedCase</tt></td>
</tr>
<tr>
<td>私有变量</td>
<td>混合，例子：<tt>_mixedCase</tt></td>
</tr>
<tr>
<td>方法（method）参数</td>
<td>混合，例子：<tt>_mixedCase, mixedCase</tt></td>
</tr>
<tr>
<td>本地（local）变量</td>
<td>混合，例子：<tt>_mixedCase, mixedCase</tt></p>
<p><tt> </tt></td>
</tr>
</tbody>
</table>
<h3>命名规范</h3>
<p>1.变量名称 必须为 小写字母。<br />
2.类的命名使用骆驼命名规则，例如：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;Account<span style="color: #339933;">,</span> EventHandler</div></div>
<p>3.常量 必须 在对象（类）或者枚举变量的前部声明。枚举变量的命名必须要有实际的意义，并且其成员 必须 使用骆驼命名规则或使用大写：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> NodeTypes <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Element <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; DOCUMENT<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>4.简写单词 不能使用 大写名称作为变量名：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getInnerHtml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> getXml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> XmlDocument</div></div>
<p>5.方法的命令 必须 为动词或者是动词短语：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">obj.<span style="color: #660066;">getSomeValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></div>
<p>6.公有类的命名 必须 使用混合名称（mixedCase）命名。<br />
7.CSS 变量的命名 必须 使用其对应的相同的公共类变量。<br />
8.私有类的变量属性成员 必须 使用混合名称（mixedCase）命名，并前面下下划线（_）。例如：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> MyClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> _buffer<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">doSomething</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>9.变量如果设置为私有，则前面 必须 添加下划线。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">this</span>._somePrivateVariable <span style="color: #339933;">=</span> statement<span style="color: #339933;">;</span></div></div>
<p>10.通用的变量 必须 使用与其名字一致的类型名称：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">setTopic<span style="color: #009900;">&#40;</span>topic<span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// 变量 topic 为 Topic 类型的变量</span></div></div>
<p>11.所有的变量名 必须 使用英文名称。<br />
12.变量如有较广的作用域（large scope），必须使用全局变量；此时可以设计成一个类的成员。相对的如作用域较小或为私有变量则使用简洁的单词命名。<br />
13.如果变量有其隐含的返回值，则避免使用其相似的方法：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 避免使用 getEventHandler()</span></div></div>
<p>14.公有变量必须清楚的表达其自身的属性，避免字义含糊不清，例如：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MouseEventHandler</div></div>
<p>，而非 MseEvtHdlr。<br />
　　请再次注意这条规定，这样做得的好处是非常明显的。它能明确的表达表达式所定义的含义。例如：<br />
　　　</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dojo.<span style="color: #660066;">events</span>.<span style="color: #660066;">mouse</span>.<span style="color: #660066;">Handler</span> <span style="color: #006600; font-style: italic;">// 而非 dojo.events.mouse.MouseEventHandler</span></div></div>
<p>15.类/构造函数 可以使用 扩展其基类的名称命名，这样可以正确、迅速的找到其基类的名称：<br />
　EventHandler<br />
　UIEventHandler<br />
　MouseEventHandler<br />
　　基类可以在明确描述其属性的前提下，缩减其命名：<br />
　　　MouseEventHandler as opposed to MouseUIEventHandler.</p>
<h3>特殊命名规范</h3>
<ol>
<li>术语 &#8220;get/set&#8221; 不要和一个字段相连，除非它被定义为私有变量。</li>
<li>前面加 &#8220;is&#8221; 的变量名 应该 为布尔值，同理可以为 &#8220;has&#8221;, &#8220;can&#8221; 或者 &#8220;should&#8221;。</li>
<li>术语 &#8220;compute&#8221; 作为变量名应为已经计算完成的变量。</li>
<li>术语 &#8220;find&#8221; 作为变量名应为已经查找完成的变量。</li>
<li>术语 &#8220;initialize&#8221; 或者 &#8220;init&#8221; 作为变量名应为已经实例化（初始化）完成的类或者其他类型的变量。</li>
<li>UI （用户界面）控制变量应在名称后加控制类型，例如： leftComboBox, TopScrollPane。</li>
<li>复数必须有其公共的名称约定（原文：Plural form MUST be used to name collections）。</li>
<li>带有 &#8220;num&#8221; 或者 &#8220;count&#8221; 开头的变量名约定为数字（对象）。</li>
<li>重复变量建议使用 &#8220;i&#8221;, &#8220;j&#8221;, &#8220;k&#8221; （依次类推）等名称的变量。</li>
<li>补充用语必须使用补充词，例如： get/set, add/remove, create/destroy, start/stop, insert/delete, begin/end, etc.</li>
<li>能缩写的名称尽量使用缩写。</li>
<li>避免产生歧义的布尔变量名称，例如：<br />
　　isNotError, isNotFound 为非法</li>
<li>错误类建议在变量名称后加上 &#8220;Exception&#8221; 或者 &#8220;Error&#8221;。</li>
<li>方法如果返回一个类，则应该在名称上说明返回什么；如果是一个过程，则应该说明做了什么。</li>
</ol>
<h3>文件</h3>
<p>缩进请使用 4 个空白符的制表位。<br />
如果您的编辑器支持 文件标签_（file tags），请加添如下的一行使我们的代码更容易阅读：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// vim:ts=4:noet:tw=0:</span></div></div>
<p>译注：老外用 VIM 编辑器比较多，此条可以选择遵循。</p>
<p>代码折叠必须看起来是完成并且是合乎逻辑的：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> someExpression <span style="color: #339933;">=</span> Expression1<br />
&nbsp; &nbsp; <span style="color: #339933;">+</span> Expression2<br />
&nbsp; &nbsp; <span style="color: #339933;">+</span> Expression3<span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> someObject.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; Expression1<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; Expression2<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; Expression3<br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>注：表达式的缩进与变量声明应为一致的。<br />
注：函数的参数应采用明确的缩进，缩进规则与其他块保持一致。</p>
<h3>变量</h3>
<ol>
<li>变量必须在声明初始化以后才能使用，即便是 NULL 类型。</li>
<li>变量不能产生歧义。</li>
<li>相关的变量集应该放在同一代码块中，非相关的变量集不应该放在同一代码块中。</li>
<li>变量应该尽量保持最小的生存周期。</li>
<li>循环/重复变量的规范：</li>
<ol>
<li>只有循环控制块的话，则必须使用 FOR 循环。</li>
<li>循环变量应该在循环开始前就被初始化；如使用 FOR 循环，则使用 FOR 语句初始化循环变量。</li>
<li>&#8220;do &#8230; while&#8221; 语句是被允许的。</li>
<li>&#8220;break&#8221; 和 &#8220;continue&#8221; 语句仍然允许使用（但请注意）。</li>
</ol>
<li>条件表达式</li>
<ol>
<li>应该尽量避免复杂的条件表达式，如有必要可以使用临时布尔变量。</li>
<li>The nominal case SHOULD be put in the &#8220;if&#8221; part and the exception in the &#8220;else&#8221; part of an &#8220;if&#8221; statement.</li>
<li>应避免在条件表达式中加入块。</li>
</ol>
<li>杂项</li>
<ol>
<li>尽量避免幻数（Magic numbers），他们应该使用常量来代替。</li>
<li>浮点变量必须指明小数点后一位（即使是 0）。</li>
<li>浮点变量必须指明实部，即使它们为零（使用 0. 开头）。</li>
</ol>
</ol>
<h3>布局</h3>
<h4>块</h4>
<ol>
<li>
普通代码段 应该 看起来如下：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isDone<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; isDone <span style="color: #339933;">=</span> moreToDo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
IF 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>someCondition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>someOtherCondition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
FOR 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>initialization<span style="color: #339933;">;</span> condition<span style="color: #339933;">;</span> update<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
WHILE 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isDone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; isDone <span style="color: #339933;">=</span> moreToDo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
DO &#8230; WHILE 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
</li>
<li>
SWITCH 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">case</span> ABC<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;fallthrough</span><br />
<span style="color: #000066; font-weight: bold;">case</span> DEF<span style="color: #339933;">:</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
TRY &#8230; CATCH 语句 应该 看起来像这样：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; statements<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
</li>
<li>
单行的 IF &#8211; ELSE，WHILE 或者 FOR 语句也 必须 加入括号，不过他们可以这样写：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> statement<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> statement<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>intialization<span style="color: #339933;">;</span> condition<span style="color: #339933;">;</span> update<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> statement<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></div></div>
</li>
</ol>
<h3>空白</h3>
<ol>
<li>操作符 建议 使用空格隔开（包括三元操作符）。</li>
<li>下面的关键字 避免使用 空白隔开：
<ul>
<li>break</li>
<li>catch</li>
<li>continue</li>
<li>do</li>
<li>else</li>
<li>finally</li>
<li>for</li>
<li>function （如果为匿名函数，例如：var foo = function(){}; ）</li>
<li>if</li>
<li>return</li>
<li>switch</li>
<li>this</li>
<li>try</li>
<li>void</li>
<li>while</li>
<li>with</li>
</ul>
</li>
<li>下面的关键字必须使用空白隔开：
<ul>
<li>case</li>
<li>default</li>
<li>delete</li>
<li>function （如果为申明，例如：function foo(){}; ）</li>
<li>in</li>
<li>instanceof</li>
<li>new</li>
<li>throw</li>
<li>typeof</li>
<li>var</li>
</ul>
</li>
<li>逗号（,） 建议 使用空白隔开。</li>
<li>冒号（:） 建议 使用空白隔开。</li>
<li>点（.） 在后部 建议 使用空白隔开。</li>
<li>点（.） 避免 在前部使用空白。</li>
<li>函数调用和方法 避免 使用空白，例如： doSomething(someParameter); // 而非 doSomething (someParameter)</li>
<li>逻辑块 之间使用空行。</li>
<li>声明 建议 对齐使其更容易阅读。</li>
</ol>
<h3>注释</h3>
<ol>
<li>生涩的代码就 <i>没有必要</i> 添加注释了，首先您需要 <strong>重写</strong> 它们。 </li>
<li>所有的注释请使用英文。 </li>
<li>从已解决的方案到未开发的功能，注释 <i>必须</i> 与代码相关。 </li>
<li>大量的变量申明后 <i>必须</i> 跟随一段注释。 </li>
<li>注释需要说明的是代码段的用处，尤其是接下来的代码段。 </li>
<li>注释 <i>没有必要</i> 每行都添加。 </li>
</ol>
<h3>文档</h3>
<p>下面提供了一些基本的函数或者对象的描述方法：</p>
<ul>
<li><strong>总结（summary）</strong>: 简短的表述此函数或者对象实现的目的 </li>
<li><strong>描述（description）</strong>: 对于此函数或者类的简短的描述 </li>
<li><strong>返回（return）</strong>: 描述此函数返回什么（并不包括返回类型） </li>
</ul>
<h3>基本函数信息</h3>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// summary: Soon we will have enough treasure to rule all of New Jersey.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// description: Or we could just get a new roomate.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Look, you go find him. &nbsp;He don't yell at you.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;All I ever try to do is make him smile and sing around</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;him and dance around him and he just lays into me.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;He told me to get in the freezer 'cause there was a carnival in there.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// returns: &nbsp;Look, a Bananarama tape!</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>对象函数信息</h3>
<p>没有返回值描述</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// summary: Dingle, engage the rainbow machine!</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// description: </span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tell you what, I wish I was--oh my g--that beam,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;coming up like that, the speed, you might wanna adjust that.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;It really did a number on my back, there. I mean, and I don't</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wanna say whiplash, just yet, cause that's a little too far,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;but, you're insured, right?</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>函数的声明</h3>
<p>在有的情况下，对于函数的调用和声明是隐义（invisible）的。在这种情况下，我们没有办法在函数中加入说明等（供程序调用）。如果您遭遇了这种情况，您可以使用一个类来封装函数。</p>
<p>注：此此方法只能在函数没有初始化的参数情况下。如过不是，则它们会被忽略。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dojo.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// summary: Phew, this sure is relaxing, Frylock.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// description: </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Thousands of years ago, before the dawn of</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;man as we knew him, there was Sir Santa of Claus: an</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ape-like creature making crude and pointless toys out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;of dino-bones, hurling them at chimp-like creatures with</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;crinkled hands regardless of how they behaved the</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;previous year.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// returns: Unless Carl pays tribute to the Elfin Elders in space.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>参数<span style="color: #339933;">&lt;/</span>h3<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>ol<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>简单类型<br />
　　　简单的类型的参数可以直接在函数参数定义中注释说明。<br />
<span style="color: #009900;">&#91;</span>cc lang<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*String*/</span> foo<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*int*/</span> bar<span style="color: #009900;">&#41;</span>...</div></div>
</li>
<li>可变类型参数<br />
　　下面是几个修饰符供参考：
<ul>
<li>? 可选参数</li>
<li>&#8230; 说面参数范围不确定</li>
<li> 数组</li>
</ul>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*String?*/</span> foo<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*int...*/</span> bar<span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">/*String[]*/</span> baz<span style="color: #009900;">&#41;</span>...</div></div>
<li>全局参数描述<br />
　如果你想增加一个描述，你可以将它们移至初始化块。
</ol>
<p>基本信息格式为： *关键字* 描述字段 （ *key* Descriptive sentence）<br />
参数和变量的格式为： *关键字* ~*类型*~ 描述字段 （ *key* ~*type*~ Descriptive sentence）<br />
注： *关键字* 和 ~*类型*~ 可以使用任何字母和数字表述。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> bar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// foo: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;used for being the first parameter</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// bar: int</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;used for being the second parameter</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>变量</h3>
<p>由于实例变量、原型变量和外部变量的声明是一致的，所以有很多的方法声明、修改变量。具体的如何定义和定位应在变量最先出现的位置指明变量的名称、类型、作用域等信息。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// myString: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// times: int</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;How many times to print myString</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// separator: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;What to print out in between myString*</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myString</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;placeholder text&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">times</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>myString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myString</span> <span style="color: #339933;">=</span> myString<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>int i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">times</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">separator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
foo.<span style="color: #660066;">separator</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;=====&quot;</span><span style="color: #339933;">;</span></div></div>
<h4>对象中的变量注释</h4>
<p>应使用和对象值和方法一致的标注方式，比如在他们声明的时候：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// key: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A simple value</span><br />
&nbsp; &nbsp; key<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// key2: String</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Another simple value</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>返回值</h3>
<p>因为函数可以同时返回多个不同（类型）的值，所以应每个返回值之后加入返回类型的注释。注释在行内注释即可，如果所有的返回值为同一类型，则指明返回的类型；如为多个不同的返回值，则标注返回类型为&#8221;mixed&#8221;。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arguments.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;You passed argument(s)&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Boolean</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>伪代码（有待讨论）</h3>
<p>有时候您需要在函数或者类中添加对于此函数和类的功能性流程描述。如果您打算这样做，您可以使用 /*======== （= 字符最好出现 5 次或者更多），这样做的好处就是可以不用将这些东西加入代码（译注：原作者的意思可能为代码管理系统）。</p>
<p>这样看起来在 /*===== 和 =====*/ 会有非常长的一段注释，等待功能调整完毕以后就可以考虑是否删除。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/*=====<br />
module.pseudo.kwArgs = {<br />
&nbsp; &nbsp; &nbsp; &nbsp; // url: String<br />
&nbsp; &nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;The location of the file<br />
&nbsp; &nbsp; url: &quot;&quot;,<br />
&nbsp; &nbsp; // mimeType: String<br />
&nbsp; &nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text/html, text/xml, etc<br />
&nbsp; &nbsp; mimeType: &quot;&quot;<br />
}<br />
=====*/</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*module.pseudo.kwArgs*/</span> kwArgs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span>kwArgs.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dojo.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span>kwArgs.<span style="color: #660066;">mimeType</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>原文链接： http://dojotoolkit.org/developer/StyleGuide<br />
翻译（Translated by）：i.feelinglucky{at}gmail.com from http://www.gracecode.com</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/dojo-javascript-style-guide.html/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>HTML5 WebSockets 基础使用教程</title>
		<link>http://blog.bingo929.com/html5-websockets.html</link>
		<comments>http://blog.bingo929.com/html5-websockets.html#comments</comments>
		<pubDate>Mon, 09 Aug 2010 17:30:28 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[WebSocket]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=3003</guid>
		<description><![CDATA[　　HTML5之中一个很酷的新特性就是WebSockets，它可以让我们无需AJAX请求即可与服务器端对话。今天彬Go将让大家通过Php环境的服务器端运行WebSocket，创建客户端并通过WebSockets协议发送和接收服... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/html5-websockets.html"><img class="alignnone size-full wp-image-3004" title="html5-websockets-教程" src="http://blog.bingo929.com/wp-content/uploads/2010/08/html5-websockets-title.jpg" alt="html5-websockets-教程" width="500" height="150" /></a></p>
<p>　　<a href="http://blog.bingo929.com/category/technology/html5">HTML5</a>之中一个很酷的新特性就是WebSockets，它可以让我们无需AJAX请求即可与服务器端对话。今天彬Go将让大家通过Php环境的服务器端运行WebSocket，创建客户端并通过WebSockets协议发送和接收服务器端信息。</p>
<p><span id="more-3003"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下HTML5相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">关于HTML 5 canvas 的基础教程</a>》<br />
《<a href="http://blog.bingo929.com/html5-ie-enabling-script.html">让所有IE支持HTML5的解决方案</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》</p>
<h3>什么是WebSockets？</h3>
<p>　　<strong>WebSockets是在一个(TCP)接口进行双向通信的技术，PUSH技术类型。同时WebSockets仍将基于W3C标准，目前为止，Chrome和Safari的最新版本浏览器已经支持WebSockets了。</strong></p>
<h3>WebSockets将会替代什么？</h3>
<p>　　WebSockets可以替代Long Polling(PHP服务端推送技术)，这是一个有趣的概念。客户端发送一个请求到服务器，现在，服务器端并不会响应还没准备好的数据，它会保持连接的打开状态直到最新的数据准备就绪发送，之后客户端收到数据，然后发送另一个请求。这有它的好处：减少任一连接的延迟，当一个连接已经打开时就不需要创建另一个新的连接。但是Long-Polling并不是什么花俏技术，他仍有可能发生请求暂停，因此会需要建立新的连接。</p>
<p>　　一些AJAX应用使用上述技术-这经常是归因于低资源利用。</p>
<p>　　试想一下，如果服务器在早晨会自启动并发送数据到那些希望接收而不用提前建立一些连接端口的客户端，这是一件多棒的事情啊！欢迎来到PUSH技术的世界！</p>
<h3>第一步：搞定WebSocket服务器</h3>
<p>　　这篇教程会把更多的精力放在客户端的创建而不是服务器端的执行等操作。</p>
<p>　　我使用基于windows 7的<a href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a>来实现本地运行PHP。<a href="http://code.google.com/p/phpwebsockets/" target="_blank">phpwebsockets</a>是PHP WebSocket服务器。(以我的经验这个版本存在一些小问题，我已对它做了些修改并上传源文件共享给大家)下面的这些不同版本也可以实现WebSocket，如果某个不能用，你可以试试其它版本或者继续看下面的教程。</p>
<p>　　<a href="http://jwebsocket.org/" target="_blank">jWebSocket</a> (Java)<br />
　　<a href="http://github.com/gimite/web-socket-ruby" target="_blank">web-socket-ruby</a> (ruby)<br />
　　<a href="http://github.com/LearnBoost/Socket.IO-node" target="_blank">Socket IO-node</a> (node.js)</p>
<h4>启动Apache服务器</h4>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/08/1.jpg" alt="html5-websocket-apache" title="html5-websocket-apache" width="451" height="383" class="alignnone size-full wp-image-3019" /></p>
<h3>第二步：修改URLs和端口</h3>
<p>　　根据你之前的安装修改服务器，下面是setup.class.php中的例子：</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span><span style="color: #000088;">$port</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">8000</span><span style="color: #339933;">,</span><span style="color: #000088;">$max</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> &nbsp;<br />
<span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createSocket</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span><span style="color: #000088;">$port</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　浏览文件并在适当情况下进行更改。</p>
<h3>第三步：开始创建客户端</h3>
<p>　　下面来创建基本模板，这是我的client.php文件：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>WebSockets Client<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>WebSockets Client<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chatLog&quot;</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- #chatLog --&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;examples&quot;</span>&gt;</span>e.g. try 'hi', 'name', 'age', 'today'<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;disconnect&quot;</span>&gt;</span>Disconnect<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- #container --&gt;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span> &nbsp;<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span>​</div></div>
<p>　　我们已经创建里基本模板：一个chat log容器，一个input输入框和一个断开连接的按钮。</p>
<h3>第四步：添加一些CSS</h3>
<p>　　没什么花俏代码，只是处理一下标签的样式。</p>
<div class="codecolorer-container css blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">body <span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #3333ff;">:Arial</span><span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #cc00cc;">#container</span><span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span> <span style="color: #993333;">solid</span> grey<span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">800px</span><span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #cc00cc;">#chatLog</span><span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #cc00cc;">#chatLog</span> p <span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #6666ff;">.event</span> <span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span> &nbsp;<br />
<span style="color: #6666ff;">.warning</span><span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span> &nbsp;<br />
<span style="color: #00AA00;">&#125;</span></div></div>
<h3>第五步：WebSocket事件</h3>
<p>首先让我们尝试并理解WebSocket事件的概念：<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/events-500x250.jpg" alt="WebSocket-事件" title="WebSocket-事件" width="500" height="250" class="alignnone size-medium wp-image-3028" /></p>
<p><strong>WebSocket事件：</strong><br />
我们将使用三个WebSocket事件：<br />
onopen: 当接口打开时<br />
onmessage: 当收到信息时<br />
onclose: 当接口关闭时</p>
<p>我们如何来实现呢？<br />
首先创建WebSocket对象</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>然后向下面这样检测事件</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">socket.<span style="color: #660066;">onopen</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Socket has been opened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>当我们收到信息时这样做：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">socket.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Awesome! &nbsp;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>但我们还是尽量避免使用alert，现在我们可以把我们学的东西整合到客户端页面中了。</p>
<h3>第六步：JavaScript</h3>
<p>　　首先我们将代码放到<a href="http://blog.bingo929.com/tag/jquery">jQuery</a> 的 document.ready函数中，然后我们还要检查用户的浏览器是否支持WebSocket。如果不支持，我们就添加一个链向Chrome浏览器页面的链接。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;WebSocket&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> window<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog, input, button, #examples'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fast&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Oh no, you need a browser that supports WebSockets. How about &lt;a href=&quot;http://www.google.com/chrome&quot;&gt;Google Chrome&lt;/a&gt;?&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//The user has WebSockets &nbsp;</span><br />
&nbsp; <br />
&nbsp; &nbsp; connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//the connect function code is below &nbsp;</span><br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>　　如你所见，如果用户浏览器支持WebSocket，我们将执行connect()函数。这里是核心功能，我们将开始创建open、close和receive事件。</p>
<p>我们将在我们的服务器定义URL。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> socket<span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #003366; font-weight: bold;">var</span> host <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #339933;">;</span></div></div>
<p>　　你可能会发现URL中怎么没有http？恩，是的，这是一个WebSocket URL，使用了不同的协议。下面是URL分解图示：<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/url-500x125.jpg" alt="html5-websocket-url" title="html5-websocket-url" width="500" height="125" class="alignnone size-medium wp-image-3035" /></p>
<p>　　下面让我们继续完成connect()函数，我们将代码放入try/catch块，这样如果代码出现问题，我们能让用户知道。我们创建WebSocket，并将信息传递到message()函数，之后会做讲解。我们创建我们的onopen、onmessage和onclose函数. 需要注意的是我们为用户提供了端口状态，这并不是必需的，但我们把它放进来主要是为了方便调试。</p>
<p>CONNECTING = 0<br />
OPEN = 1<br />
CLOSED = 2</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket<span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> host <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span>host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onopen</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (open)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;message&quot;&gt;Received: '</span><span style="color: #339933;">+</span>msg.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onclose</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (Closed)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Error'</span><span style="color: #339933;">+</span>exception<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>　　message()函数很简单, 它将我们想展现给用户的文本填入chat log容器内。 我们在socket事件函数中为段落(&lt;p>)标签创建适当的class，我们在message函数中只有一个段落结束标签。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> message<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<h3>目前的成果</h3>
<p>　　如果你已按上面教程按部就班的做的话，很好，我们已经创建了 <a href="http://blog.bingo929.com/tag/divcss">HTML/CSS</a> 模板、创建并建立Websocket连接、通过创建连接保持用户的进展更新。</p>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/08/page.jpg" alt="HTML5-websocket" title="HTML5-websocket" width="369" height="209" class="alignnone size-full wp-image-3042" /></p>
<h3>第七步：发送数据</h3>
<p>　　现在我们已经有了提交按钮，但我们还需要监听用户按下键盘的事件，并运行send函数，下面的&#8217;13&#8242;便是回车键对应的ASCII码。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keypress</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'13'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>下面是send()函数：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt;Please enter a message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Sent: '</span><span style="color: #339933;">+</span>text<span style="color: #009900;">&#41;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt; Error:'</span> <span style="color: #339933;">+</span> exception<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>下面我们需要：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">socket.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>　　那些额外的代码做了以下工作：检测用户是否什么都没输入却仍点击返回、清空input输入框、执行message()函数。</p>
<h3>关闭Socket</h3>
<p>　　关闭Socket操作相当简单，添加对断开连接按钮的click事件监听就可以。</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#disconnect'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; socket.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/08/close.jpg" alt="websocket-close" title="websocket-close" width="324" height="77" class="alignnone size-full wp-image-3053" /></p>
<h3>完整<a href="http://blog.bingo929.com/category/technology/javascript">JavaScript</a>代码</h3>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;WebSocket&quot;</span> <span style="color: #000066; font-weight: bold;">in</span> window<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog, input, button, #examples'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fast&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Oh no, you need a browser that supports WebSockets. How about &lt;a href=&quot;http://www.google.com/chrome&quot;&gt;Google Chrome&lt;/a&gt;?&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//The user has WebSockets &nbsp;</span><br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket<span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> host <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ws://localhost:8000/socket/server/startDaemon.php&quot;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> WebSocket<span style="color: #009900;">&#40;</span>host<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onopen</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (open)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;message&quot;&gt;Received: '</span><span style="color: #339933;">+</span>msg.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">onclose</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Socket Status: '</span><span style="color: #339933;">+</span>socket.<span style="color: #660066;">readyState</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' (Closed)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;Error'</span><span style="color: #339933;">+</span>exception<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>text<span style="color: #339933;">==</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt;Please enter a message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;event&quot;&gt;Sent: '</span><span style="color: #339933;">+</span>text<span style="color: #009900;">&#41;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>exception<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p class=&quot;warning&quot;&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> message<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#chatLog'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keypress</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'13'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#disconnect'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;socket.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">//End connect &nbsp;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">//End else &nbsp;</span><br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>第九步：运行WebSocket服务器</h3>
<p>我们要输入一些命令行，XAMPP提供了比较方便的shell选项。点击XAMPP控制面板的&#8217;shell&#8217;按钮并输入：<br />
php -q path\to\server.php<br />
现在你已经运行了WebSocket服务器！<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/2-500x166.jpg" alt="HTML5-webSocket" title="HTML5-webSocket" width="500" height="166" class="alignnone size-medium wp-image-3056" /></p>
<h3>大功告成！</h3>
<p>　　当页面读取后，将尝试创建一个WebSocket连接，然后用户可以输入信息并从服务器接收信息。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/08/example.jpg" alt="html5-websocket-教程实例" title="html5-websocket-教程实例" width="484" height="433" class="alignnone size-full wp-image-3057" /></p>
<p>　　感谢大家耐心阅读本教程，希望你能从中学到有用的知识，HTML5 WebSocket的确令人兴奋不已！大家可以通过<a href="http://dev.w3.org/html5/websockets/" target="_blank">The WebSocket API</a>了解HTML5 WebSocket的最新动态。</p>
<p><strong><a href="http://www.bingo929.com/mytest/websocket_source_files.rar">点击这里下载源文件</a></strong></p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看HTML5最新资源、教程。 </p>
<p>英文原文:<a title="Start Using HTML5 WebSockets Today" href="http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/" target="_blank">Start Using HTML5 WebSockets Today</a><br />
翻译原文:<a title="HTML5 WebSockets 基础使用教程" href="http://blog.bingo929.com/html5-websockets.html">HTML5 WebSockets 基础使用教程</a>(<a href="http://blog.bingo929.com/">彬Go</a>)</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="HTML5 WebSockets 基础使用教程" href="http://blog.bingo929.com/html5-websockets.html">http://blog.bingo929.com/html5-websockets.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/html5-websockets.html/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>HTML5和CSS3工具资源汇总</title>
		<link>http://blog.bingo929.com/html5-and-css3-tools-list.html</link>
		<comments>http://blog.bingo929.com/html5-and-css3-tools-list.html#comments</comments>
		<pubDate>Mon, 28 Jun 2010 12:37:05 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[DIV+CSS]]></category>
		<category><![CDATA[前端开发]]></category>
		<category><![CDATA[工具]]></category>
		<category><![CDATA[资源]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2971</guid>
		<description><![CDATA[　　HTML5和CSS3已经开始彻底变革全世界的Web前端开发和Web设计发展格局，因为他们给这个领域带来了太多太多令人惊奇的新特性！我用大量时间学习了HTML5和CSS3技术，它们的动画、圆角等特效... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/html5-and-css3-tools-list.html"><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-title.jpg" width="500" height="140" alt="HTML5和CSS3工具资源汇总" /></a></p>
<p>　　<a href="http://blog.bingo929.com/category/technology/html5">HTML5</a>和<a href="http://blog.bingo929.com/tag/css3">CSS3</a>已经开始彻底变革全世界的Web前端开发和Web设计发展格局，因为他们给这个领域带来了太多太多令人惊奇的新特性！我用大量时间学习了HTML5和CSS3技术，它们的动画、圆角等特效会令你震惊&#8230;在后面的内容中彬Go将向大家推荐一些能帮助你更好的掌握甚至精通HTML5和CSS3的终极工具资源集合。虽然还有很多特性不被浏览器所支持，但我们作为前端开发从业者，有必要掌握它们。彬Go希望这些HTML5和CSS3工具资源能对大家的学习和工作有所帮助。<br />
<span id="more-2971"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下HTML5和CSS3相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html">关于HTML 5 canvas 的基础教程</a>》<br />
《<a href="http://blog.bingo929.com/50-awesome-css3-animations.html">用CSS3制作50个超棒动画效果教程</a>》<br />
《<a href="http://blog.bingo929.com/5-css3-design-enhancements-div-css.html">5个CSS3技术实现设计增强</a>》<br />
《<a href="http://blog.bingo929.com/html5-ie-enabling-script.html">让所有IE支持HTML5的解决方案</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》<br />
《<a href="http://blog.bingo929.com/div-css-web-design-to-next-level-with-css3.html">提升你设计水平的CSS3新技术</a>》</p>
<h3><a href="http://html5readiness.com/" target="_blank">HTML5 &#038; CSS3 准备就绪</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-01.jpg" width="485" height="139" alt="CSS3" /><br />
　　该网站通过扇形图表的形式展现了从08年到10年以来各大浏览器对HTML5和CSS3的支持情况。发展势头还是很可观的。</p>
<h3><a href="http://heideri.ch/jso/" target="_blank">HTML5安全手册</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-02.jpg" width="485" height="139" alt="HTML5" /></p>
<h3><a href="http://css-tricks.com/examples/ButtonMaker/" target="_blank">CSS3按钮生成器</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-03.jpg" width="485" height="139" alt="HTML5资源" /><br />
　　所见即所得的为你生成CSS3按钮，你仅需通过基本的鼠标点击便可生成令人满意的CSS3按钮，点击生成好的按钮即可查看相应的CSS3代码。</p>
<h3><a href="http://caniuse.com/" target="_blank">When can I use…（HTML5 &#038; CSS3 浏览器兼容性）</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-04.jpg" width="485" height="139" alt="HTML5工具" /><br />
　　很详细的关于HTML5, CSS3, SVG等技术的浏览器兼容性列表。</p>
<h3><a href="http://www.codenique.com/web_color/css3_color_names.php" target="_blank">CSS3颜色值/名称速查手册</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-05.jpg" width="485" height="139" alt="HTML5+CSS3" /><br />
　　CSS3颜色值查询手册，包括颜色英文名、16进制值、rgb百分比、rgb整数值、颜色缩写和颜色效果等&#8230;</p>
<h3><a href="http://css3generator.com/" target="_blank">CSS3生成器</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-06.jpg" width="485" height="139" alt="HTML5" /><br />
　　在线测试CSS3新特性，即时生成效果。</p>
<h3><a href="http://www.modernizr.com/" target="_blank">Modernizr</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-07.jpg" width="485" height="139" alt="CSS3" /></p>
<h3><a href="http://www.html5test.com/" target="_blank">当前浏览器对HTML5支持情况测试</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-08.jpg" width="485" height="139" alt="HTML5资源" /><br />
　　该工具可以帮助你测试当前你使用的浏览器对HTML5的兼容情况，很详细，很有用！</p>
<h3><a href="http://border-radius.com/" target="_blank">CSS 边框半径(代码生成器)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-09.jpg" width="485" height="139" alt="HTML5工具" /><br />
　　通过输入数值即可生成CSS3代码的CSS 边框代码生成器。使用很简便。</p>
<h3><a href="http://www.findmebyip.com/litmus/#target-selector" target="_blank">FindMeByIP（HTML5 &#038; CSS3 浏览器兼容性手册）</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-10.jpg" width="485" height="139" alt="HTML5+CSS3" /><br />
　　很详细的HTML5和CSS3浏览器兼容性手册。</p>
<h3><a href="http://gradients.glrzad.com/" target="_blank">CSS3渐变生成器</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-11.jpg" width="485" height="139" alt="HTML5" /></p>
<h3><a href="http://css3watch.com/" target="_blank">CSS3 Watch</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-12.jpg" width="485" height="139" alt="CSS3" /></p>
<h3><a href="http://westciv.com/tools/" target="_blank">前端开发工具&#038;资源小集合</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-13.jpg" width="485" height="139" alt="HTML5资源" /></p>
<h3><a href="http://css3please.com/" target="_blank">CSS3, please!(实时体验CSS3代码)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-14.jpg" width="485" height="139" alt="HTML5工具" /></p>
<h3><a href="http://labs.thecssninja.com/font_dragr/" target="_blank">font dragr</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-15.jpg" width="485" height="139" alt="HTML5+CSS3" /></p>
<h3><a href="http://www.widgetpad.com/694/" target="_blank">CSS3生成器</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-16.jpg" width="485" height="139" alt="HTML5" /></p>
<h3><a href="http://cssdesk.com/" target="_blank">CSS桌面</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-17.jpg" width="485" height="139" alt="CSS3" /><br />
　　CSS桌面可以让你在线练习CSS3代码，并实时将渲染效果呈现在右侧，很棒的CSS3在线练习工具。</p>
<h3><a href="http://html5.org/tools/web-apps-tracker" target="_blank">HTML5修订版本跟踪器</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-18.jpg" width="485" height="139" alt="HTML5资源" /></p>
<h3><a href="http://www.fontsquirrel.com/fontface/generator" target="_blank">font-face 生成器</a></h3>
<p><a href="http://blog.bingo929.com/"><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-19.jpg" width="485" height="139" alt="HTML5工具" /></a></p>
<h3><a href="http://tools.css3.info/selectors-test/test.html" target="_blank">CSS3 选择器测试工具</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-20.jpg" width="485" height="139" alt="HTML5+CSS3" /></p>
<h3><a href="http://www.iecss.com/print-protector/" target="_blank">IE 打印保险器</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-21.jpg" width="485" height="139" alt="HTML5" /></p>
<h3><a href="http://www.webdevout.net/browser-support-css" target="_blank">详细CSS 浏览器支持情况列表</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/html5-css3-tools/html5-css3-22.jpg" width="485" height="139" alt="CSS3" /></p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看HTML5及CSS3最新资源、教程。 </p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="HTML5和CSS3工具资源汇总" href="http://blog.bingo929.com/html5-and-css3-tools-list.html">http://blog.bingo929.com/html5-and-css3-tools-list.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/html5-and-css3-tools-list.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>关于HTML 5 canvas 的基础教程</title>
		<link>http://blog.bingo929.com/html-5-canvas-the-basics-html5.html</link>
		<comments>http://blog.bingo929.com/html-5-canvas-the-basics-html5.html#comments</comments>
		<pubDate>Tue, 15 Jun 2010 17:31:30 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2947</guid>
		<description><![CDATA[HTML 5 规范引进了很多新特性，其中最令人期待的之一就是 canvas 元素。HTML 5 canvas 提供了通过 JavaScript 绘制图形的方法，此方法使用简单但功能强大。每一个canvas 元素都有一个&#8221;上下文( conte... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/html-5-canvas-the-basics-html5.html"><img src="http://blog.bingo929.com/wp-content/uploads/2010/06/1.jpg" alt="" title="HTML5 Canvas 基础教程" width="500" height="150" class="alignnone size-full wp-image-2969" /></a></p>
<p id="introduction"><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/">HTML 5 规范</a>引进了很多新特性，其中最令人期待的之一就是 <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">canvas 元素</a>。<a href="http://blog.bingo929.com/category/technology/html5">HTML 5</a> canvas 提供了通过 <a href="http://blog.bingo929.com/category/technology/javascript">JavaScript</a> 绘制图形的方法，此方法使用简单但功能强大。每一个canvas 元素都有一个&#8221;上下文( context )&#8221; (想象成绘图板上的一页)，在其中可以绘制任意图形。浏览器支持多个 canvas 上下文，并通过不同的<abbr title="Application Programmable         Interfaces"> API</abbr> 提供图形绘制功能。</p>
<p><span id="more-2947"></span></p>
<p>大部分的浏览器都支持 2D canvas 上下文——包括 <a href="http://opera.com/">Opera</a>, <a href="http://mozilla.com/">Firefox</a>, <a href="http://konqueror.org/">Konqueror</a> 和 <a href="http://apple.com/safari">Safari</a>。而且某些版本的 Opera 还支持 3D canvas ，Firefox 也可以通过插件形式支持 3D canvas ：</p>
<ul>
<li><a href="http://labs.opera.com/news/2008/11/25/">下载支持 3D canvas, HTML video 和 File I/O 的 Opera</a></li>
<li><a href="http://my.opera.com/timjoh/blog/2007/11/13/taking-the-canvas-to-another-dimension">关于 Opera 3D canvas 上下文的文章</a></li>
<li><a href="http://blog.vlad1.com/2007/11/26/canvas-3d-gl-power-web-style/">关于 Firefox 3D canvas<br />
        上下文的文章</a></li>
</ul>
<p>本文介绍 2D canvas<br />
    基础以及如何使用基本 canvas 函数，如线条、形状、图像和文字等。为了理解此文章，你最好了解 JavaScript 基础知识。</p>
<p class="note"><a href="http://dev.opera.com/articles/view/html-5-canvas-the-basics/canvas-primer.zip">可以点击此处批量下载本文实例代码</a></p>
<h3>目录</h3>
<ul>
<li><a href="#introduction">简述</a></li>
<li><a href="#thebasicsofusingcanvas">canvas 基础</a></li>
<li><a href="#the2dcontextapi"> 2D context API</a>
<ul>
<li><a href="#basiclinesandstrokes">基本线条</a></li>
<li><a href="#paths">路径</a></li>
<li><a href="#insertingimages">插入图像</a></li>
<li><a href="#pixelbasedmanipulation">像素级操作</a></li>
<li><a href="#text">文字</a></li>
<li><a href="#shadows">阴影</a></li>
<li><a href="#gradients">颜色渐变</a></li>
</ul>
</li>
<li><a href="#summary">小节</a></li>
</ul>
<h3 id="thebasicsofusingcanvas">canvas 基础</h3>
<p>创建 canvas  的方法很简单，只需要在 HTML 页面中添加 &lt;canvas&gt; 元素：</p>
<div class="codecolorer-container html4strict blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;canvas <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myCanvas&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;150&quot;</span>&gt;</span><br />
Fallback content, in case the browser does not support Canvas.<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span></div></div>
<p>为了能在 JavaScript 中引用元素，最好给元素设置 ID ；也需要给 canvas 设定高度和宽度。</p>
<p>创建好了画布后，让我们来准备画笔。要在画布中绘制图形需要使用 JavaScript 。首先通过 getElementById 函数找到 canvas<br />
    元素，然后初始化上下文。之后可以使用上下文 API 绘制各种图形。下面的脚本在 canvas 中绘制一个矩形 (<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-using-canvas.html">点击此处查看效果</a>)：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Get a reference to the element.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myCanvas'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Always check for properties 和 methods, to make sure your code doesn't break </span><br />
<span style="color: #006600; font-style: italic;">// in other browsers.</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> elem.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Get the 2d context.</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Remember: you can only initialize one context per element.</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> elem.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// You are done! Now you can draw your first rectangle.</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// You only need to provide the (x,y) coordinates, followed by the width and </span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// height dimensions.</span><br />
&nbsp; &nbsp; context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>可以把上面代码放置在文档 head 部分中，或者放在外部文件中。</p>
<h3 id="the2dcontextapi">2D context API</h3>
<p>介绍了如何创建 canvas 后，让我们来看看 2D canvas API，看看能用这些函数做些什么。</p>
<h4 id="basiclinesandstrokes">基本线条</h4>
<p>上面的例子中展示了绘制矩形是多么简单。</p>
<p>通过 <var>fillStyle</var> 和 <var>strokeStyle</var> 属性可以轻松的设置矩形的填充和线条。颜色值使用方法和 <abbr title="Cascading Style Sheet">CSS </abbr>一样：十六进制数、<abbr title="red, green, blue">rgb</abbr>()、<abbr title="red, green, blue, alpha">rgba</abbr>() 和 <abbr title="hue, saturation, light, alpha">hsla</abbr>()（ 若浏览器支持，如<a href="http://dev.opera.com/articles/view/presto-2-2-and-opera-10-a-first-look/"> Opera 10</a> 和 Firefox 3)。</p>
<p>通过 fillRect 可以绘制带填充的矩形。使用 strokeRect 可以绘制只有边框没有填充的矩形。如果想清除部分 canvas 可以使用 clearRect。上述三个方法的参数相同：<var>x</var>, <var>y</var>, <var>width</var>, <var>height</var>。前两个参数设定 (x,y) 坐标，后两个参数设置矩形的高度和宽度。</p>
<p>可以使用 <var>lineWidth</var> 属性改变线条粗细。让我们看看<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-rects.html">使用了fillRect,strokeRect clearRect 和其他的例子</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">context.<span style="color: #660066;">fillStyle</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// blue</span><br />
context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red</span><br />
context.<span style="color: #660066;">lineWidth</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Draw some rectangles.</span><br />
context.<span style="color: #660066;">fillRect</span> &nbsp;<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">clearRect</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>此例子效果图见图1.</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-rects.html"><img alt="Example render of fillRect, strokeRect and clearRect." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example-.jpg" /><br /> <br />
</a></p>
<p class="comment">图 1: fillRect, strokeRect 和<br />
    clearRect效果图</p>
<h3 id="paths">路径</h3>
<p>通过 canvas 路径（path）可以绘制任意形状。可以先绘制轮廓，然后绘制边框和填充。创建自定义形状很简单，使用beginPath()开始绘制，然后使用直线、曲线和其他图形绘制你的图形。绘制完毕后调用 fill 和 stroke 即可添加填充或者设置边框。调用 closePath() 结束自定义图形绘制。</p>
<p>下面是一个绘制<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-triangle.html">三角形的例子</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Set the style properties.</span><br />
context.<span style="color: #660066;">fillStyle</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#f00'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">lineWidth</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// Start from the top-left point.</span><br />
context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// give the (x,y) coordinates</span><br />
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Done! Now fill the shape, 和 draw the stroke.</span><br />
<span style="color: #006600; font-style: italic;">// Note: your shape will not be visible until you call any of the two methods.</span><br />
context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">closePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>其效果图见图2.</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-triangle.html"><img alt="Triangle" src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example0.jpg" /><br />
</a></p>
<p class="comment">图 2: 三角形</p>
<p>另一个较负责的例子中<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-paths.html">使用了直线、曲线和圆弧</a>。</p>
<h3 id="insertingimages">插入图像</h3>
<p>drawImage 方法允许在 canvas 中插入其他图像<br />
    ( img 和 canvas 元素) 。在 Opera 中可以再 canvas 中绘制 SVG 图形。此方法比较复杂，可以有3个、5个或9个参数：</p>
<ul>
<li>3个参数：最基本的 drawImage 使用方法。一个参数指定图像位置，另两个参数设置图像在 canvas中的位置。</li>
<li>5个参数：中级的 drawImage 使用方法，包括上面所述3个参数，加两个参数指明插入图像宽度和高度 (如果你想改变图像大小)。</li>
<li>9个参数：最复杂drawImage 杂使用方法，包含上述5个参数外，另外4个参数设置源图像中的位置和高度宽度。这些参数允许你在显示图像前动态裁剪源图像。</li>
</ul>
<p>下面是上述<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-drawimage.html">三个使用方法</a>的例子：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Three arguments: the element, destination (x,y) coordinates.</span><br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img_elem<span style="color: #339933;">,</span> dx<span style="color: #339933;">,</span> dy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Five arguments: the element, destination (x,y) coordinates, and destination </span><br />
<span style="color: #006600; font-style: italic;">// width and height (if you want to resize the source image).</span><br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img_elem<span style="color: #339933;">,</span> dx<span style="color: #339933;">,</span> dy<span style="color: #339933;">,</span> dw<span style="color: #339933;">,</span> dh<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Nine arguments: the element, source (x,y) coordinates, source width and </span><br />
<span style="color: #006600; font-style: italic;">// height (for cropping), destination (x,y) coordinates, and destination width </span><br />
<span style="color: #006600; font-style: italic;">// and height (resize).</span><br />
context.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img_elem<span style="color: #339933;">,</span> sx<span style="color: #339933;">,</span> sy<span style="color: #339933;">,</span> sw<span style="color: #339933;">,</span> sh<span style="color: #339933;">,</span> dx<span style="color: #339933;">,</span> dy<span style="color: #339933;">,</span> dw<span style="color: #339933;">,</span> dh<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>其效果见图3.</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-drawimage.html"><img alt="Example rendering of drawImage." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example1.jpg" /><br />
</a></p>
<p class="comment">图 3: drawImage 效果图。</p>
<h3 id="pixelbasedmanipulation">像素级操作</h3>
<p>2D Context API 提供了三个方法用于像素级操作：createImageData, getImageData, 和<br />
    putImageData。</p>
<p>ImageData对象保存了图像像素值。每个对象有三个属性: <var>width</var>, <var>height</var> 和<br />
    <var>data</var>。<var>data</var> 属性类型为<span class="Type">CanvasPixelArray</span>，用于储存<var>width</var>*<var>height</var>*4个像素值。每一个像素有RGB值和透明度alpha值(其值为 0 至 255，包括alpha在内!)。像素的顺序从左至右，从上到下，按行存储。</p>
<p>为了更好的理解其原理，让我们来看一个例子——<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html">绘制红色矩形</a></p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Create an ImageData object.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> imgd <span style="color: #339933;">=</span> context.<span style="color: #660066;">createImageData</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> pix <span style="color: #339933;">=</span> imgd.<span style="color: #660066;">data</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Loop over each pixel 和 set a transparent red.</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> n <span style="color: #339933;">=</span> pix.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i &nbsp;<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red channel</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">127</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alpha channel</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Draw the ImageData object at the given (x,y) coordinates.</span><br />
context.<span style="color: #660066;">putImageData</span><span style="color: #009900;">&#40;</span>imgd<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>注意: 不是所有浏览器都实现了 createImageData。在支持的浏览器中，需要通过 getImageData 方法获取 ImageData 对象。请参考<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html">示例代码</a>。</p>
<p>通过 ImageData 可以完成很多功能。如可以实现图像滤镜，或可以实现数学可视化 (如分形和其他特效)。下面特效实现了<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata.html">简单的颜色反转滤镜</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Get the CanvasPixelArray from the given coordinates and dimensions.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> imgd <span style="color: #339933;">=</span> context.<span style="color: #660066;">getImageData</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>x<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>y<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>width<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>height<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> pix <span style="color: #339933;">=</span> imgd.<span style="color: #660066;">data</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Loop over each pixel and invert the color.</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> pix.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i &nbsp;<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">-</span> pix<span style="color: #009900;">&#91;</span>i &nbsp;<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">-</span> pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// green</span><br />
&nbsp; pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">-</span> pix<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// blue</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// i+3 is alpha (the fourth element)</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Draw the ImageData at the given (x,y) coordinates.</span><br />
context.<span style="color: #660066;">putImageData</span><span style="color: #009900;">&#40;</span>imgd<span style="color: #339933;">,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>x<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>y<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>图 4 显示了使用此滤镜后的 Opera<br />
    图像 (图 3是原图)。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata.html"><img alt="Example rendering of the invert color filter." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example2.jpg" /><br />
</a></p>
<p class="comment">图 4: 颜色反转滤镜</p>
<h3 id="text">文字</h3>
<p>虽然最近的 WebKit 版本和 Firefox 3.1 nightly build 才开始支持 Text API ，为了保证文章完整性我决定仍在这里介绍文字 API 。</p>
<p>context 对象可以设置以下 text 属性：</p>
<ul>
<li>font：文字字体，同<abbr title="Cascading Style Sheet"> CSS</abbr> font-family 属性</li>
<li>textAlign：文字水平对齐方式。可取属性值: start, end, left,right, center。默认值:start.</li>
<li>textBaseline：文字竖直对齐方式。可取属性值：top, hanging, middle,alphabetic, ideographic, bottom。默认值：alphabetic.</li>
</ul>
<p>有两个方法可以绘制文字：  fillText 和 strokeText。第一个绘制带 <var>fillStyle</var> 填充的文字，后者绘制只有 <var>strokeStyle</var> 边框的文字。两者的参数相同：要绘制的文字和文字的位置(x,y) 坐标。还有一个可选选项——最大宽度。如果需要的话，浏览器会缩减文字以让它适应指定宽度。</p>
<p>文字对齐属性影响文字与设置的<br />
    (x,y) 坐标的相对位置。</p>
<p>下面是一个<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-text.html">在 canvas 中绘制&#8221;hello world&#8221; 文字的例子</a></p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">context.<span style="color: #660066;">fillStyle</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">font</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'italic 30px sans-serif'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">textBaseline</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">fillText</span> &nbsp;<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello world!'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">font</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bold 30px sans-serif'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">strokeText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello world!'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>图 5 是其效果图。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-text.html"><img alt="Example text render." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example3.jpg" /><br />
</a></p>
<p class="comment">图 5: 文字效果</p>
<h3 id="shadows">阴影</h3>
<p>目前只有 Konqueror 和 Firefox 3.1 nightly build 支持 Shadows API 。API 的属性为：</p>
<ul>
<li>shadowColor：阴影颜色。其值和 CSS 颜色值一致。</li>
<li>shadowBlur：设置阴影模糊程度。此值越大，阴影越模糊。其效果和 Photoshop 的高斯模糊滤镜相同。</li>
<li>shadowOffsetX 和 shadowOffsetY：阴影的 x 和 y 偏移量，单位是像素。</li>
</ul>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-shadows.html">下面是 canvas 阴影的例子</a>：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">context.<span style="color: #660066;">shadowOffsetX</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">shadowOffsetY</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">shadowBlur</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">shadowColor</span> &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'rgba(255, 0, 0, 0.5)'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">fillStyle</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#00f'</span><span style="color: #339933;">;</span><br />
context.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>其效果见图 6。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-shadows.html"><img alt="Example canvas shadow - a blue rectangle with a red shadow." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example4.jpg" /><br /> <br />
</a></p>
<p class="comment">图 6: canvas 阴影效果——蓝色矩形，红色阴影</p>
<h3 id="gradients">颜色渐变</h3>
<p>除了 CSS 颜色， fillStyle 和 strokeStyle 属性可以设置为 CanvasGradient 对象。——通过 CanvasGradient可以为线条和填充使用颜色渐变。</p>
<p>欲创建 CanvasGradient 对象，可以使用两个方法：createLinearGradient 和 createRadialGradient。前者创建线性颜色渐变，后者创建圆形颜色渐变。</p>
<p>创建颜色渐变对象后，可以使用对象的 addColorStop 方法添加颜色中间值。</p>
<p>下面的代码演示了颜色渐变使用方法：</p>
<div class="codecolorer-container javascript blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// You need to provide the source 和 destination (x,y) coordinates </span><br />
<span style="color: #006600; font-style: italic;">// for the gradient (from where it starts 和 where it ends).</span><br />
<span style="color: #003366; font-weight: bold;">var</span> gradient1 <span style="color: #339933;">=</span> context.<span style="color: #660066;">createLinearGradient</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Now you can add colors in your gradient.</span><br />
<span style="color: #006600; font-style: italic;">// The first argument tells the position for the color in your gradient. The </span><br />
<span style="color: #006600; font-style: italic;">// accepted value range is from 0 (gradient start) to 1 (gradient end).</span><br />
<span style="color: #006600; font-style: italic;">// The second argument tells the color you want, using the CSS color format.</span><br />
gradient1.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #3366CC;">'#f00'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// red</span><br />
gradient1.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0.5</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#ff0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// yellow</span><br />
gradient1.<span style="color: #660066;">addColorStop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #3366CC;">'#00f'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// blue</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// For the radial gradient you also need to provide source</span><br />
<span style="color: #006600; font-style: italic;">// 和 destination circle radius.</span><br />
<span style="color: #006600; font-style: italic;">// The (x,y) coordinates define the circle center points (start 和 </span><br />
<span style="color: #006600; font-style: italic;">// destination).</span><br />
<span style="color: #003366; font-weight: bold;">var</span> gradient2 <span style="color: #339933;">=</span> context.<span style="color: #660066;">createRadialGradient</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>sr<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dx<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dy<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;,</span> <span style="color: #339933;">&lt;</span>var<span style="color: #339933;">&gt;</span>dr<span style="color: #339933;">&lt;/</span>var<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #006600; font-style: italic;">// Adding colors to a radial gradient is the same as adding colors to linear </span><br />
<span style="color: #006600; font-style: italic;">// gradients.</span></div></div>
<p>我也准备了一个更复杂的例子，使用了<a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-gradients.html">线性颜色渐变、阴影和文字</a>。其效果见图 7。</p>
<p><a href="http://www.robodesign.ro/coding/canvas-primer/20081208/example-gradients.html"><img alt="Example rendering using a linear gradient." src="http://dev.opera.com/articles/view/html-5-canvas-the-basics/example5.jpg" /><br /> <br />
</a></p>
<p class="comment">图 7: 使用线性颜色渐变的例子</p>
<h3>canvas 演示</h3>
<p>如果你想知道使用 Canvas可以做些什么，可以参看以下的工程：</p>
<ul>
<li>Opera Widget：
<ul>
<li><a href="http://widgets.opera.com/widget/5040/">SimAquarium</a></li>
<li><a href="http://widgets.opera.com/widget/4647/">Artist&#8217;s Sketchbook</a></li>
<li><a href="http://widgets.opera.com/widget/5218/">Spirograph</a></li>
</ul>
</li>
<li>在线工程和演示
<ul>
<li><a href="http://www.benjoffe.com/code/demos/interpolate/">Newton polynomial</a></li>
<li><a href="http://www.benjoffe.com/code/demos/canvascape/">Canvascape &#8211; &#8220;3D Walker&#8221;</a></li>
<li><a href="http://code.google.com/p/paintweb">Paint.Web &#8211; painting demo, open-source</a></li>
<li><a href="http://arapehlivanian.com/wp-content/uploads/2007/02/canvas.html">Star-field flight</a></li>
<li><a href="http://www.blobsallad.se/">Interactive blob</a></li>
</ul>
</li>
</ul>
<h3 id="summary">小节</h3>
<p>Canvas 是<a href="http://blog.bingo929.com/category/technology/html5"> HTML 5</a>最让人期待的特性之一，目前已获得大部分 Web 浏览器支持。Canvas 可以帮助创建游戏、增强图形用户界面。2D context API 提供大量图形绘制功能——我希望通过本文你了解了 canvas 使用，并且你有兴趣了解更多！</p>
<p>本文转自：<a href="http://kb.operachina.com/node/190" target="_blank">operachina</a></p>
<p>本文翻译自 <a href="http://dev.opera.com/articles/view/html-5-canvas-the-basics/">HTML 5 canvas &#8211; the basics</a><br />
作者 <a href="http://dev.opera.com/author/112520">Mihai Sucan</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/html-5-canvas-the-basics-html5.html/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>用CSS3制作50个超棒动画效果教程</title>
		<link>http://blog.bingo929.com/50-awesome-css3-animations.html</link>
		<comments>http://blog.bingo929.com/50-awesome-css3-animations.html#comments</comments>
		<pubDate>Mon, 15 Mar 2010 06:20:28 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2857</guid>
		<description><![CDATA[　　CSS3为我们带来了令人惊叹的新特性，而最有趣的就是CSS动画。今天彬Go向大家推荐这50个CSS动画集合可以让你通过使用JavaScript函数来让动画更生动。为了能够预览到这些惊人的CSS3技术带来... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/50-awesome-css3-animations.html"><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/css3-animations-title.jpg" alt="50个css3技术教程" title="css3-动画-教程" width="500" height="150" class="alignnone size-full wp-image-2872" /></a></p>
<p>　　CSS3为我们带来了令人惊叹的新特性，而最有趣的就是CSS动画。今天彬Go向大家推荐这50个CSS动画集合可以让你通过使用JavaScript函数来让动画更生动。为了能够预览到这些惊人的CSS3技术带来的动画特效，请大家使用如<strong>Safari</strong>和<strong>Chrome</strong>这类基于WebKit内核的浏览器。(<strong>IE浏览器谢绝观赏</strong>~)<br />
<span id="more-2857"></span></p>
<p><strong><span style="color: #ff6600;">您还可以参考以下CSS3相关文章:</span></strong><br />
《<a href="http://blog.bingo929.com/5-css3-design-enhancements-div-css.html">5个CSS3技术实现设计增强</a>》<br />
《<a href="http://blog.bingo929.com/power-of-html5-css3-div-css.html">一起感受HTML5和CSS3的能量</a>》<br />
《<a href="http://blog.bingo929.com/div-css-web-design-to-next-level-with-css3.html">提升你设计水平的CSS3新技术</a>》</p>
<h3>1.<a href="http://css-tricks.com/examples/CSS3Clock/" target="_blank">CSS3实现钟表效果(基于jQuery)</a></h3>
<p>　　使用CSS3的基本变形特性:rotate，并结合了jQuery这类javaScript框架制作的CSS3时钟效果。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/clock-jQuery-css3-animations.jpg" alt="表-jQuery-css3" title="表-jQuery-css3" width="500" height="228" class="alignnone size-full wp-image-2869" /></p>
<h3>2.<a href="http://www.fofronline.com/experiments/clock/#clock" target="_blank">模拟时钟</a></h3>
<p>　　模拟时钟基于过渡webkit和CSS3变形，javascript用于将其按当前时间转动。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/analogue-clock-css3-animations.jpg" alt="时钟效果-css3" title="时钟效果-css3" width="500" height="228" class="alignnone size-full wp-image-2863" /></p>
<h3>3.<a href="http://www.fofronline.com/experiments/cube-3d/" target="_blank">可使用箭头键旋转的3D立方体</a></h3>
<p>　　你可以使用上、下、左、右方向键控制这个3D立方体，它是基于webkit-perspective, -webkit-transform 和 -webkit-transition 创建。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/3D-cube-rotates-arrow-keys-css3-animations.jpg" alt="css3-3D立方体" title="css3-教程-3D" width="500" height="228" class="alignnone size-full wp-image-2859" /></p>
<h3>4.<a href="http://www.fofronline.com/experiments/cube/multiCubes.html" target="_blank">多种3D立方体(淡入淡出)</a></h3>
<p>　　多种3D立方体使用CSS3和它的属性‘transform’ 和 ‘transition’ ，我个人觉得效果很震撼，你能看到写在3D立方体上的透视感极强的文字。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/multiple-3d-cubes-slide-in-out-css3-animations.jpg" alt="多种3D立方体(淡入淡出)" title="多种3D立方体(淡入淡出)" width="500"  /></p>
<h3>5.<a href="http://www.fofronline.com/experiments/accordion/#two" target="_blank">CSS3手风琴效果</a></h3>
<p>　　使用CSS3实现的手风琴菜单效果，基于webkit浏览器专有属性。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/accordion-css3-animations.jpg" alt="CSS3手风琴效果" title="CSS3手风琴效果" width="500"  /></p>
<h3>6.<a href="http://www.fofronline.com/experiments/parallax/" target="_blank">自动滚动视差效果</a></h3>
<p>　　自动滚动视差效果使用WebKit的 CSS transition 属性。该效果无需JavaScript。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/auto-scrolling-parallax-css3-animations.jpg" alt="自动滚动视差效果" title="自动滚动视差效果" width="500"  /></p>
<h3>7.<a href="http://www.zachstronaut.com/lab/isocube.html" target="_blank">Isocube</a></h3>
<p> 　　Isocube有些类似于3D立方体，但也有不同之处，它可以让他片贴在立方体面。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/isocube-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>8.<a href="http://devfiles.myopera.com/articles/1041/image-gallery.html" target="_blank">CSS3图片集</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/image-gallery-2-css3-animations.jpg" alt="" title="" width="500"  /></p>
<h3>9.<a href="http://girliemac.com/sandbox/matrix.html" target="_blank">CSS3 Matrix(黑客帝国效果)</a></h3>
<p>　　黑客帝国是最棒的科幻小说改编电影之一，现在使用纯CSS就能实现电影中的矩阵效果。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/matrix-css3-animations.jpg" alt="CSS3 Matrix(黑客帝国效果)" title="CSS3 Matrix(黑客帝国效果)" width="500"  /></p>
<h3>10.<a href="http://webdeveloperjuice.com/demos/css/css3effects.html" target="_blank">7种CSS3结合javascript技术的特效</a></h3>
<p>　　7种CSS3结合javascript技术的特效实例. 所包含的效果有淡入淡出, 震动, 轻移, 扩大, 跳动, 旋转 和 手风琴效果。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/7-javascript-effect-css3-animations.jpg" alt="7种CSS3结合javascript技术的特效" title="7种CSS3结合javascript技术的特效" width="500"  /></p>
<h3>11.<a href="http://dinolatoga.com/demo/webkit-image-hover-effects/" target="_blank">各种鼠标悬停图片特效</a></h3>
<p>　　六个由CSS3替代JavaScript而实现的鼠标悬停图片效果，CSS3实现这种悬停效果依然很炫！<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/image-hover-effects-css3-animations.jpg" alt="各种鼠标悬停图片特效" title="各种鼠标悬停图片特效" width="500"  /></p>
<h3>12.<a href="http://www.romancortes.com/blog/pure-css-coke-can/" target="_blank">旋转的可口可乐易拉罐(使用滚动条控制)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/turning-coke-can-control-with-scrollbar-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>13.<a href="http://www.romancortes.com/blog/css-3d-meninas/" target="_blank">3D 《宫女》</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/3D-meninas-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>14.<a href="http://media.24ways.org/2009/14/5/index.html" target="_blank">宝利来画廊</a></h3>
<p>　　宝利来画廊是由CSS3技术实现的将一些照片堆在一起，有趣的是鼠标悬停照片是，照片会旋转并缓慢放大~<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/polaroid-gallery-2-css3-animations.jpg" alt="CSS3教程" title="CSS3教程" width="500"  /></p>
<h3>15.<a href="http://media.24ways.org/2009/15/space.html" target="_blank">太空</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/space-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>16.<a href="http://www.zurb.com/playground/osx-dock" target="_blank">Mac Dock</a></h3>
<p>　　CSS3模拟的mac操作系统菜单&#8230;<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/mac-dock-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>17.<a href="http://www.zurb.com/playground/drop-in-modals" target="_blank">Drop-In Modals</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/drop-in-modals-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>18.<a href="http://www.zurb.com/playground/sliding-vinyl" target="_blank">滑动的唱片</a></h3>
<p>　　该效果使用了CSS3过渡和些许HTML，在它上面放一个唱片专辑会看上去更生动。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/sliding-vinyl-css3-animations.jpg" alt="CSS3实例" title=""CSS3实例 width="500"  /></p>
<h3>19.<a href="http://www.zurb.com/playground/css3-polaroids" target="_blank">CSS3 Zooming Polaroids</a></h3>
<p>　　该效果会将一个照片列表按顺序排列，并把它们按不同的角度旋转，使用了CSS2和CSS3技术，无JavaScript。它从图片的alt属性中读取出照片描述并放到了照片的下方。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/zooming-polaroids-css3-animations.jpg" alt="CSS3 Zooming Polaroids" title="CSS3 Zooming Polaroids" width="500"  /></p>
<h3>20.<a href="http://www.the-art-of-web.com/css/css-animation/" target="_blank">CSS3 火箭动画</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/animated-rocket-css3-animations.jpg" alt="CSS3 火箭动画" title="CSS3 火箭动画" width="500"  /></p>
<h3>21.<a href="http://webkit.org/blog-files/3d-transforms/poster-circle.html" target="_blank">Poster Circle</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/poster-circle-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>22.<a href="http://webkit.org/blog-files/3d-transforms/morphing-cubes.html" target="_blank">变形的立方体</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/morphing-cubes-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>23.<a href="http://www.3site.eu/examples/gallery/" target="_blank">动画版宝丽来画廊</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/polaroid-gallery-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>24.<a href="http://www.zachstronaut.com/lab/text-shadow-box/text-shadow-box.html" target="_blank">聚光灯下的投影效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/spotlight-cast-shadow-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>25.<a href="http://demo.tutorialzine.com/2009/12/colorful-clock-jquery-css/demo.html" target="_blank">色彩鲜艳的时钟</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/colorful-clock-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>26.<a href="http://demo.tutorialzine.com/2009/11/hovering-gallery-css3-jquery/demo.php" target="_blank">Lightbox Gallery</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/lightbox-gallery-draggable-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>27.<a href="http://buildinternet.com/live/elasticthumbs/" target="_blank">弹性缩略图菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/elastic-thumbnail-menu-css3-animations.jpg" alt="弹性缩略图菜单" title="弹性缩略图菜单" width="500"  /></p>
<h3>28.<a href="http://paulbakaus.com/lab/js/coverflow/" target="_blank">Coverflow</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/coverflow-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>29.<a href="http://demo.marcofolio.net/jquery_dj/" target="_blank">jQuery DJ Hero</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/jQuery-dj-hero-css3-animations.jpg" alt="jQuery DJ Hero" title="jQuery DJ Hero" width="500"  /></p>
<h3>30.<a href="http://designlovr.com/examples/dynamic_stack_of_index_cards/" target="_blank">Dynamic Stacking Cards</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/dynamic-stacking-cards-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>31.<a href="http://www.alexandtheweb.com/demos/transitions-test.html" target="_blank">另一个CSS3图片画廊效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/image-gallery-css3-animations.jpg" alt="" title="" width="500"  /></p>
<h3>32.<a href="http://www.satine.org/research/webkit/snowleopard/snowstack.html" target="_blank">雪堆(使用方向键控制)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/snow-stack-control-with-arrow-keys-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>33.<a href="http://stuffandnonsense.co.uk/content/demo/cannybill/21-10-2009/pricing.html" target="_blank">CSS3动画版价格栏</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/pricing-column-css3-animations.jpg" alt="CSS3动画版价格栏" title="CSS3动画版价格栏" width="500"  /></p>
<h3>34.<a href="http://demo.marcofolio.net/slick_jquery_css3_menu/default.html" target="_blank">平滑jQuery+CSS3菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/slick-jQuery-menu-css3-animations.jpg" alt="平滑jQuery+CSS3菜单" title="平滑jQuery+CSS3菜单" width="500"  /></p>
<h3>35.<a href="http://www.kamikazemusic.com/demo/csstabs.html" target="_blank">CSS标签(无JavaScript)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/tabs-without-javascript-css3-animations.jpg" alt="CSS标签(无JavaScript)" title="CSS标签(无JavaScript)" width="500"  /></p>
<h3>36.<a href="http://development.tobypitman.com/css/menu.html" target="_blank">CSS标签菜单(无JavaScript)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/tab-menus-without-javascript-css3-animations.jpg" alt="CSS标签菜单(无JavaScript)" title="CSS标签菜单(无JavaScript)" width="500"  /></p>
<h3>37.<a href="http://o.sitepen.com/labs/code/cssDock/" target="_blank">SVG+CSS3鱼眼菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/svg-fisheye-menu-css3-animations.jpg" alt="VG+CSS3鱼眼菜单" title="VG+CSS3鱼眼菜单" width="500"  /></p>
<h3>38.<a href="http://webkit.org/blog-files/leaves/index.html" target="_blank">CSS3落叶效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/falling-leaves-css3-animations.jpg" alt="CSS3落叶效果" title="CSS3落叶效果" width="500"  /></p>
<h3>39.<a href="http://samples.webmatze.de/rotating_image_gallery.htm" target="_blank">CSS3旋转图集效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/rotating-gallery-css3-animations.jpg" alt="CSS3旋转图集效果" title="CSS3旋转图集效果" width="500"  /></p>
<h3>40.<a href="http://www.newmediacampaigns.com/files/http://www.webdesignerwall.com/wp-content/uploads/2010/02/nicer-navigation-with-css-transitions/demo.html" target="_blank">CSS3下拉菜单</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/dropdown-menu-css3-animations.jpg" alt="CSS3下拉菜单" title="CSS3下拉菜单" width="500"  /></p>
<h3>41.<a href="http://blog.gesteves.com/post/261593774/im-done-star-wars-opening-crawl-using-only-html" target="_blank">星球大战风格爬行文字(纯HTML+CSS)</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/star-wars-crawl-css3-animations.jpg" alt="星球大战风格爬行文字(纯HTML+CSS)" title="星球大战风格爬行文字(纯HTML+CSS)" width="500"  /></p>
<h3>42.<a href="http://webkit.org/demos/sticky-notes/" target="_blank">CSS3即时贴</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/sticky-notes-css3-animation.jpg" alt="CSS3即时贴" title="CSS3即时贴" width="500"  /></p>
<h3>43.<a href="http://natbat.net/code/clientside/css/snowflakes/" target="_blank">css3雪花</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/Snowflakes-css3-animations.jpg" alt="css3雪花" title="css3雪花" width="500"  /></p>
<h3>44.<a href="http://www.niquelao.net/wp-content/uploads/2009/12/example1.html" target="_blank">又一个鱼眼效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/another-fisheye-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>45.<a href="http://www.cssplay.co.uk/menu/css3-animation.html" target="_blank">CSS3逐帧动画</a></h3>
<p>　　第一个CSS3逐帧动画实例需要你持续使用鼠标点击图片，当播放到最后一帧时它会自动播放第一帧，如此循环，但第一个实例只能在FireFox和Opera下实现。第二个Css3逐帧动画实例需要你使用鼠标在图片上移动，依旧是mousemove这个动作，只要鼠标在移动图片动画就会持续播放，但动画的播放速度是与你的鼠标移动速度有关的，该实例支持Safari, Google Chrome, IE8, Firefox 和 Opera。<br />
<img src="http://blog.bingo929.com/wp-content/uploads/2010/03/Frame-by-Frame-css3-animations.jpg" alt="CSS3逐帧动画" title="CSS3逐帧动画" width="500"  /></p>
<h3>46.<a href="http://anthonycalzadilla.com/css3-ATAT/index-bones.html" target="_blank">全地域装甲载具</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/at-walker-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>47.<a href="http://www.thecssninja.com/demo/css_accordion/#Section2" target="_blank">又一个CSS3手风琴效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/Another-Accordion-css3-animations.jpg" alt="又一个CSS3手风琴效果" title="又一个CSS3手风琴效果" width="500"  /></p>
<h3>48.<a href="http://files.myopera.com/FataL/pres/pres.xml#name" target="_blank">无Flash版动态展示</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/dynamic-presentation-without-flash-css3-animations.jpg" alt="CSS3实例" title="CSS3实例" width="500"  /></p>
<h3>49.<a href="http://elliottrothman.com/files/smoothie.html" target="_blank">平滑菜单栏</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/smoothie-css3-animations.jpg" alt="平滑菜单栏" title="平滑菜单栏" width="500"  /></p>
<h3>50.<a href="http://24ways.org/2009/css-animations" target="_blank">loading旋转效果</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2010/03/spinner-css3-animations.jpg" alt="loading旋转效果" title="loading旋转效果" width="500"  /></p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看CSS3最新教程。 </p>
<p>英文原文:<a title="50 Awesome Animations made with CSS3" href="http://www.1stwebdesigner.com/development/50-awesome-css3-animations/" target="_blank">50 Awesome Animations made with CSS3</a><br />
翻译原文:<a title="用CSS3制作50个超棒动画效果教程" href="http://blog.bingo929.com/50-awesome-css3-animations.html">用CSS3制作50个超棒动画效果教程</a>(<a href="http://blog.bingo929.com/">彬Go</a>)</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="用CSS3制作50个超棒动画效果教程" href="http://blog.bingo929.com/50-awesome-css3-animations.html">http://blog.bingo929.com/50-awesome-css3-animations.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/50-awesome-css3-animations.html/feed</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>15款提高表格操作的jQuery插件</title>
		<link>http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html</link>
		<comments>http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html#comments</comments>
		<pubDate>Mon, 21 Dec 2009 06:40:55 +0000</pubDate>
		<dc:creator>彬Go</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[前端开发]]></category>

		<guid isPermaLink="false">http://blog.bingo929.com/?p=2824</guid>
		<description><![CDATA[　　table表格由于它的浏览器兼容性和复杂的标签嵌套方式，可以算是添加样式最困难的对象之一了。大多数前端er都把网页中的table标签替换为div，主要就是因为div要比table更容易添加CSS样式。... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html"><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-table-plugins-title.jpg" alt="jquery-table-表格-插件" title="jquery-table-表格-插件" width="500" height="150" class="alignnone size-full wp-image-2825" /></a></p>
<p>　　table表格由于它的浏览器兼容性和复杂的标签嵌套方式，可以算是添加样式最困难的对象之一了。大多数前端er都把网页中的table标签替换为div，主要就是因为div要比table更容易添加CSS样式。但是我们在日常应用中仍然要用到table表格，其中最好的例子就是对照表。今天彬Go将向大家推荐<strong>15个jQuery表格插件</strong>让你对数据表格进行显示、排序、筛选和操控。如jQuery表格排序插件、jQuery表格拖拽插件、jQuery树形表格插件、设置颜色、点击、替换等效果。<br />
<span id="more-2824"></span><br />
<strong><span style="color: #ff6600;">您还可以参考以下javascript相关资源:</span></strong><br />
《<a href="http://blog.bingo929.com/useful-javascript-plot-charts-web.html">推荐9款很棒的网页绘制图表JavaScript框架脚本</a>》<br />
《<a href="http://blog.bingo929.com/jquery-javascript-tips-to-improve-code.html">了解jQuery技巧来提高你的代码</a>》<br />
《<a href="http://blog.bingo929.com/10-ajax-effects-website-fanciness.html">10种JavaScript特效实例让你的网站更吸引人</a>》<br />
《<a href="http://blog.bingo929.com/300-jquery-css-mootools-js-navigation-menus.html">300+Jquery, CSS, MooTools 和 JS的导航菜单资源</a>》</p>
<h3>1.<a href="http://www.datatables.net/" target="_blank">DataTables</a>-强大的jQuery表格插件</h3>
<p><a href="http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html"><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/datatables.jpg" alt="datatables-jquery-插件" title="datatables-jquery-插件" width="500" height="194" class="alignnone size-full wp-image-2827" /></a><br />
　　DataTables是提供了大量特性的强大jQuery表格插件。例如:你可以自动轻松筛选、Ajax预读取数据、分页、列排序、高亮排序列、扩展插件支持、使用CSS或jQuery UI ThemeRoller 定制主题和完整文档。</p>
<h3>2.<a href="http://gregweber.info/projects/uitablefilter" target="_blank">uiTableFilter</a>-jQuery表格过滤插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/ui-table-filter.jpg" alt="ui-table-filter-jquery-插件" title="ui-table-filter-jquery-插件" width="500" height="415" class="alignnone size-full wp-image-2829" /><br />
　　uiTableFilter是一个用于表格行筛选的jQuery插件。插件作者提供了详细的例子来告诉大家如何将自己的表格和插件整合在一起并完成表格筛选。</p>
<h3>3.<a href="http://www.webtoolkit.info/scrollable-html-table-plugin-for-jquery.html" target="_blank">Scrollable HTML Table</a>-jQuery表格滚动插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/scrollable-html-table.jpg" alt="scrollable-html-table-jquery" title="scrollable-html-table-jquery" width="500" height="171" class="alignnone size-full wp-image-2830" /><br />
　　Scrollable HTML Table <a href="http://blog.bingo929.com/category/technology/jquery-technology">jQuery</a>插件可以让你的表格变得可以滚动控制。</p>
<h3>4.<a href="http://tablesorter.com/docs/" target="_blank">Tablesorter</a>-jQuery表格排序插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/tablesorter.jpg" alt="tablesorter-jquery-插件" title="tablesorter-jquery-插件" width="500" height="120" class="alignnone size-full wp-image-2831" /><br />
　　Tablesorter这个jQuery插件是用来将一个包含thead和tbody标签的标准HTML表格转变为无页面刷新的可排序表格。它不但支持多列排序，而且跨浏览器兼容并且能通过widget系统进行扩展。</p>
<h3>5.<a href="http://flexigrid.info/" target="_blank">Flexigrid</a>-Web2.0 jQuery表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/flexigrid.jpg" alt="flexigrid-jquery" title="flexigrid-jquery" width="500" height="224" class="alignnone size-full wp-image-2833" /><br />
　　Flexigrid 是一个轻量级的Web2.0 jQuery插件。它包含很多非常Cool的特性，如主题定制、分页、工具栏、搜索、排序、ajax读取数据源、调整列宽高尺寸等。</p>
<h3>6.<a href="http://www.jnathanson.com/blog/client/jquery/heatcolor/index.cfm" target="_blank">HeatColor</a>-jQuery表格颜色插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/heatcolor.jpg" alt="heatcolor-jquery" title="heatcolor-jquery" width="500" height="340" class="alignnone size-full wp-image-2834" /><br />
　　HeatColor可以让你根据元素的值来为元素设置颜色。 它的值的比对时根据预设的一个范围来进行比较，最后对他们自动进行颜色值的设置。</p>
<h3>7.<a href="http://www.hanpau.com/index.php?page=jqtreetable" target="_blank">JQTreeTable</a>-jQuery树形表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jqtreetable.jpg" alt="jqtreetable-jquery" title="jqtreetable-jquery" width="500" height="220" class="alignnone size-full wp-image-2835" /><br />
　　使用JQTreeTable插件你可以得到一个树形表格，如果用户的浏览器禁用了JavaScript，那么他们也能看到普通形式的表格，并不影响可访问性。</p>
<h3>8.<a href="http://reconstrukt.com/ingrid/" target="_blank">Ingrid</a>-jQuery表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/ingrid.jpg" alt="ingrid-jquery-表格-插件" title="ingrid-jquery-表格-插件" width="500" height="224" class="alignnone size-full wp-image-2836" /><br />
　　Ingrid是另一个数据表jQuery插件。它也有很多特性，比如列调整尺寸、排序、行列添加样式等。在作者网站提供了很棒的文档来告诉大家如何从头使用这个jQuery表格插件。 </p>
<h3>9.<a href="http://p.sohei.org/jquery-plugins/columnmanager/" target="_blank">jQuery ColumnManager plugin</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-column-manager.jpg" alt="jquery-column-manager-jquery-表格插件" title="jquery-column-manager-jquery-表格插件" width="500" height="150" class="alignnone size-full wp-image-2837" /><br />
　　columnManager是可以控制任意表格列显示或隐藏的jQuery插件，而且它可以保存当前表格的状态至你下次访问。这个jQuery插件非常小巧轻量，仅3.6kb。 </p>
<h3>10.<a href="http://blog.cubicphuse.nl/2008/11/12/jquery-treetable-2-0" target="_blank">jQuery treeTable</a>-jQuery树形表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-treetable.jpg" alt="jquery-treetable-树形表格插件" title="jquery-treetable-树形表格插件" width="500" height="192" class="alignnone size-full wp-image-2844" /><br />
　　jQuery treeTable是小型的JQTreeTable, 它也能让你的table显示成树形结构. 这个插件也可以在浏览器禁用JavaScript时保持文档整洁并让原始table可以使用.</p>
<h3>11.<a href="http://plugins.jquery.com/project/csv2table" target="_blank">CSV2Table</a>-CSV格式读取表格插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/csv2table.jpg" alt="csv2table-jQuery表格插件" title="csv2table-jQuery表格插件" width="500" height="232" class="alignnone size-full wp-image-2845" /><br />
　　CSV2Table 可以读取CSV文件，它从CSV文件中读取内容并创建成table表格.</p>
<h3>12.<a href="http://neoalchemy.org/tablePagination.html" target="_blank">Table Pagination</a>-jQuery表格分页插件</h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/table-pagination.jpg" alt="table-pagination-jQuery表格插件" title="table-pagination-jQuery表格插件" width="500" height="227" class="alignnone size-full wp-image-2846" /><br />
　　这个jQuery表格分页插件可以在表格下面创建分页元素，你还可以通过各种设置来定制分页。</p>
<h3>13.<a href="http://pure-essence.net/stuff/webTips/jqueryTableRowCheckboxToggle.html" target="_blank">jQuery TableRowCheckboxToggle</a></h3>
<p><img src="http://blog.bingo929.com/wp-content/uploads/2009/12/jquery-table-row-checkbox-toggle.jpg" alt="jquery-table-插件" title="jquery-table-插件" width="500" height="182" class="alignnone size-full wp-image-2848" /><br />
　　这个<a href="http://blog.bingo929.com/category/technology/jquery-technology">jQuery</a>表格插件可以让你再点击表格行的时候改变多选框状态并修改该行CSS样式。</p>
<h3>14.<a href="http://www.isocra.com/2008/02/table-drag-and-drop-jquery-plugin/" target="_blank">Table Drag and Drop jQuery plugin</a>-jQuery表格拖拽插件</h3>
<p>　　这个简单的jQuery插件可以让用户使用拖拽行的方式重新排列表格各行，任意单独行可以设置为不可拖拽和/或不可放置。</p>
<h3>15.<a href="http://gregweber.info/projects/uitableedit" target="_blank">uiTableEdit</a></h3>
<p>　　uiTableEdit是由Greg Weber制作的很酷的jQuery表格编辑插件，它允许用户编辑表格内容。</p>
<h3>留意更新</h3>
<p><a href="http://feed.feedsky.com/bingo929" target="_blank">订阅彬Go</a>以查看网页设计最新资源。 </p>
<p>英文原文:<a title="15 Great jQuery Plugins For Better Table Manipulation" href="http://www.webdesignbooth.com/15-great-jquery-plugins-for-better-table-manipulation/" target="_blank">15 Great jQuery Plugins For Better Table Manipulation</a><br />
翻译原文:<a title="15款提高表格操作的jQuery插件" href="http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html">15款提高表格操作的jQuery插件</a>(<a href="http://blog.bingo929.com/">彬Go</a>)</p>
<div style="border: 1px solid #afedff; padding: 10px; display: block; background-color: #e9faff;"><strong><span style="color: #ff0000;">转载声明:</span></strong><br />
原载:<a href="http://blog.bingo929.com/">彬Go</a>——集前端开发/网页设计/网站可用性/用户体验于一体的趣味互联网生活<br />
本文链接:<a title="15款提高表格操作的jQuery插件" href="hhttp://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html">http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html<br />
</a>如需转载必须以链接形式注明原载或原文地址，谢谢合作</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bingo929.com/15-jquery-plugins-for-better-table-manipulation.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

