Hello, Willko

如果你觉得昨天很有成就,那么表示你今天还不够努力。

原创 google首页按钮效果收藏

之前韩国首页首先推出这个导航就引起了讨论。
实际上,这种效果非常简单,只是我们缺乏思维而已。

先看演示吧..http://www.willko.cn/js/hellobutton/demo.html

实际上只是一张图片http://www.willko.cn/js/hellobutton/button.gif

用setTimeout和backgroundPosition就可以实现动画了。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    
<head>
        
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
        
<title>Untitled Document</title>
        
<style type="text/css">
            span 
{
                width
: 52px;
                height
: 37px;
                display
: block;
                background
: url(button.gif) no-repeat 0px 0px;
            
}

        
</style>
        
<script type="text/javascript">
            
function dsp(n, playing){
                
if (n <= 6 && n >= 0{
                    
var button = document.getElementById("button");
                    
var width = 52;
                    
var offsetWidth = -width * n;
                    button.style.backgroundPosition 
= offsetWidth + "px 0px";
                    n 
= playing ? n + 1 : n - 1;
                    setTimeout(
"dsp(" + n + "," + playing + ")"45);
                }

                
            }

        
</script>
    
</head>

    
<body>
        
<span onmouseover="dsp(1, true);" onmouseout="dsp(6);" id="button">&nbsp;</span>
    
</body>
</html>

为什么要使用这种方法?
因为,这样可以减少HTTP链接,加快浏览速度,减少服务器负担。
所以对于,GOOGLE这种级别的访问,是非常有用的,不是为了酷..

2008-12-28 添加 jQuery版
  1. /**
  2.  * jQuery wko.DynImg
  3.  *
  4.  * version 0.1 (2008/8/15)
  5.  *
  6.  * @author Willko Cheng <willko@foxmail.com>
  7.  *
  8.  */
  9. (function($){
  10.     $.fn.extend({
  11.         DynImg: function(options){
  12.             options = $.extend({
  13.                 step: 0,
  14.                 width: 0,
  15.                 timeout: 60
  16.             }, options);
  17.             
  18.             return this.each(function(){
  19.                 var over, out;
  20.                 var overHandler = function(e, me){
  21.                     var me = me || $(this);
  22.                     clearTimeout(out);
  23.                     var position = me.css("backgroundPosition").split(' ');
  24.                     var x = parseInt(position[0]);
  25.                     var y = parseInt(position[1]);
  26.                     
  27.                     if (x >= -options.step * options.width) {
  28.                         me.css({
  29.                             backgroundPosition: x + -options.width + "px " + y + "px"
  30.                         });
  31.                         over = setTimeout(function(){
  32.                             overHandler(e, me)
  33.                         }, options.timeout);
  34.                     }
  35.                 };
  36.                 
  37.                 var outHandler = function(e, me){
  38.                     var me = me || $(this);
  39.                     clearTimeout(over);
  40.                     var position = me.css("backgroundPosition").split(' ');
  41.                     var x = parseInt(position[0]);
  42.                     var y = parseInt(position[1]);
  43.                     if (x <= -options.width) {
  44.                         me.css({
  45.                             backgroundPosition: x + options.width + "px " + y + "px"
  46.                         });
  47.                         out = setTimeout(function(){
  48.                             outHandler(e, me)
  49.                         }, options.timeout);
  50.                     }
  51.                 };
  52.                 $(this).hover(overHandler, outHandler);
  53.             });
  54.         }
  55.     });
  56. })(jQuery);
用法
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
  5.         <title>Untitled Document</title>
  6.         <script type="text/javascript" src="jquery.pack.js">
  7.         </script>
  8.         <script type="text/javascript" src="wko.DynImg.js">
  9.         </script>
  10.         <style type="text/css">
  11.             .toolbar {
  12.                 margin: 0;
  13.                 padding: 0;
  14.                 list-style-type: none;
  15.             }
  16.             .toolbar a{
  17.                 display: block;
  18.                 height: 37px;
  19.                 width: 50px;
  20.                 background: url(toolbar_animation_20080807.png) no-repeat;
  21.             }
  22.         </style>
  23.     </head>
  24.     <body>
  25.         <ul class="toolbar">
  26.             <li><a href="" style="background-position: 0px -35px"></a></li>
  27.         </ul>
  28.         <script type="text/javascript">
  29.         $(function(){
  30.             $(".toolbar a").DynImg({step: 3, width: 52});
  31.         });
  32.         </script>
  33.     </body>
  34. </html>
参数step为动画长度,width为图片相隔长度

发表于 @ 2008年04月01日 12:18:00|评论(loading...)|收藏

新一篇: 使用动态重写,,实现JS的d单件模式(singleton) | 旧一篇: firefox和opera不支持word-wrap:break-word的解决方法

Csdn Blog version 3.1a
Copyright © 郑伟珂