DIV CSS 佈局教程網

重現 Google 塗鴉風格:粘土動畫 JS 特效
編輯:關於JavaScript     

還記得 Google 曾經推出的粘土風格塗鴉(Doodle)嗎?那些可愛的粘土角色在滑鼠移入時會做出動作,生動又有趣。這個特效利用 CSS 精靈圖與 JavaScript 就能重現,將代碼保存成 html 文件打開即可查看效果。

以下是源代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Google粘土動畫Doodle代碼-</title>
<script type="text/javascript" src="https:///keleyi/phtml/jstexiao/8/js/keleyi.niantu.js"></script>
<script type="text/javascript">
 window.onload = function () {
 new animation({ element: "Div1", url: 'https:///keleyi/phtml/jstexiao/8/images/15_gumby11-blockheads.jpg', hover: function () { this.element.style.backgroundPositionX = '-135px'; }, blur: function () { this.element.style.backgroundPositionX = '0'; } });
 new animation({ element: "Div2", url: 'https:///keleyi/phtml/jstexiao/8/images/16_gumby11-prickle.jpg', hover: function () { this.element.style.backgroundPositionX = '-335px'; }, blur: function () { this.element.style.backgroundPositionX = '-270px'; } });
 new animation({ element: "Div3", url: 'https:///keleyi/phtml/jstexiao/8/images/17_gumby11-goo.png', hover: function () { this.element.style.backgroundPositionX = '-467px'; }, blur: function () { this.element.style.backgroundPositionX = '-400px'; } });
 new animation({ element: "Div4", url: 'https:///keleyi/phtml/jstexiao/8/images/10_gumby11-gumby.jpg', hover: function () { this.element.style.backgroundPositionX = '-780px'; }, blur: function () { this.element.style.backgroundPositionX = '-682px'; } });
 new animation({ element: "Div5", url: 'https:///keleyi/phtml/jstexiao/8/images/18_gumby11-pokey.png', hover: function () { this.element.style.backgroundPositionX = '-609px'; }, blur: function () { this.element.style.backgroundPositionX = '-534px'; } });
 };
</script>
<style type="text/css">
 div{position:relative;}
</style>
</head>

<body>
<div style="width:736px;margin:10px auto;border:1px solid silver;position:relative;">
<div><h2>Google粘土動畫Doodle代碼·</h2><a href="https:///a/bjac/3iote6u9.htm" target="_blank">原文</a></div>
請點擊以下"粘土"</div>
<div style="width:436px;margin:50px auto;position:relative;">
<div style="width:135px;height:156px;background:url(https:///keleyi/phtml/jstexiao/8/images/6_gumby11-initial-sprite.png) 0 0;" id="Div1"></div>
<div style="left:138px;top:40px;width:65px;height:102px;background:url(https:///keleyi/phtml/jstexiao/8/images/6_gumby11-initial-sprite.png) -270px 0;position:absolute" id="Div2"></div>
<div style="left:196px;top:14px;width:67px;height:144px;background:url(https:///keleyi/phtml/jstexiao/8/images/6_gumby11-initial-sprite.png) -400px 0;z-index:90;position:absolute" id="Div3"></div>
<div style="left:246px;top:-6px;width:98px;height:156px;background:url(https:///keleyi/phtml/jstexiao/8/images/6_gumby11-initial-sprite.png) -682px 0;position:absolute" id="Div4"></div>
<div style="left:323px;top:50px;width:75px;height:108px;background:url(https:///keleyi/phtml/jstexiao/8/images/6_gumby11-initial-sprite.png) -534px 0;position:absolute" id="Div5"></div>
</div>
</body>
</html>

原理是將每個粘土角色的不同動作幀拼在同一張精靈圖上,透過改變元素的 backgroundPositionX 來切換顯示哪一幀:滑鼠移入(hover)時切到「動作中」的幀,移出(blur)時回到靜止幀。這種以背景定位實現動畫的手法,正是經典精靈圖動畫的應用典範。

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