Google主页导航动画效果演示、下载及源代码解析

来源:互联网 发布:午后淡茶 4js 微博 编辑:程序博客网 时间:2024/06/09 18:12

view

 

src

 

link: http://www.yangfu.name/Article/GoogleBar.aspx

参考Google主页的效果做的,文件中有图片和链接修改说明。

DEMO: http://www.yangfu.name/Attach/GoogleBar/index.html

Down: http://www.yangfu.name/Attach/GoogleBar/GoogleBar.rar 

该效果的代码解析如下:

  1. (function()...{  
  2.     //注册事件侦听  
  3.     function register(c,d,b)...{  
  4.         var a="on"+d;  
  5.         if(c.addEventListener)...{  
  6.             c.addEventListener(d,b,false);  
  7.         }else if(c.attachEvent)...{  
  8.             c.attachEvent(a,b);  
  9.         }else...{  
  10.             var f=c[a];  
  11.             c[a]=function()...{  
  12.                 var g=f.apply(this,arguments),e=b.apply(this,arguments);return g==undefined?e:(e==undefined?g:e&&g)  
  13.             }  
  14.         }  
  15.     };  
  16.     //n=每帧画面的高,m=每帧画面的宽  
  17.     var l="images/toolbar_animation_20080807.png",m=52,n=37,o=75,p=100;  
  18.     //工具栏对象数据对象  
  19.     function q(c,d,b,a,f)...{  
  20.         return...{url:c,name:d,tooltip:b,color:a,yAdjust:f}  
  21.     }  
  22.     //工具栏对象数据数组  
  23.     var r=[q("http://video.google.cn/","视频","搜索热门网络视频","#54a70d",[2,5]),  
  24.         q("http://images.google.cn/","图片","搜索全球的图片","#3b79e7",[2,5]),  
  25.         q("http://news.google.cn/","资讯","跟踪社会热点,搜索最新资讯","#f3e76e",[3,7]),  
  26.         q("http://ditu.google.cn/","地图","查询地址、捜索周边和规划路线","#96cfec",[4,9]),  
  27.         q("http://blogsearch.google.cn/","博客搜索","在博客中搜索感兴趣的主题","#e6af36",[3,7]),  
  28.         q("http://www.google.cn/rebang/","热榜","查看分类热门搜索排行","#ff9202",[2,5]),  
  29.         q("http://daohang.google.cn/","网站导航","网址大全,快速直达常用网站","#d93c08",[2,5])];  
  30.     var objectList=[];//动画对象维护列表  
  31.     function t(c,d)...{  
  32.         while(d&&c!=d)d=d.parentNode;  
  33.         return d==c  
  34.     }  
  35.     /**//**  
  36.      *事件响应特殊处理  
  37.      *  
  38.      *@param string eventtype 事件类型  
  39.      *@param int index 动画对象维护列表索引  
  40.      */ 
  41.     function v(eventtype,index)...{  
  42.         return function(b)...{  
  43.             b=b||window.event;  
  44.             var a=objectList[index],f=eventtype=="mouseover";  
  45.             var g=b.target||b.srcElement,e=b.relatedTarget||(f?b.fromElement:b.toElement),h=!e||t(a.element,e),i=t(a.element,g);  
  46.             if(f&&!h||!f&&(!e||i&&!h))...{  
  47.                 var j=a.icon;  
  48.                 j.mouseTimeout=window.clearTimeout(j.mouseTimeout);  
  49.                 var u=f?1:-1;  
  50.                 if(j.popup != u)j.mouseTimeout=window.setTimeout(j.doPlay(u,undefined),f?p/3:p)  
  51.             }  
  52.         }  
  53.     }  
  54.     /**//**  
  55.      *注册事件响应  
  56.      *  
  57.      *@param object element 元素  
  58.      *@param string eventtype 事件名  
  59.      *@param int index   
  60.      */ 
  61.     function addListener(element, eventtype, index)...{register(element, eventtype, v(eventtype, index))}  
  62.     /**//**  
  63.      *工具栏提示  
  64.      *  
  65.      *@param object tbItem 工具栏项  
  66.      *@param string url 链接地址  
  67.      *@param string tipText 提示文字  
  68.      */ 
  69.     function ToobarTip(tbItem, url, tipText)...{  
  70.         this.toolbaritem = tbItem;  
  71.         var a=document.createElement("div");  
  72.         var f=c.color;  
  73.         var g="background-color:"+f;  
  74.         var e="width:1px;height:1px;"+g+";float:";  
  75.         var h='<div style="height:1px;overflow:hidden"><div style="'+e+'left"></div><div style="'+e+'right"></div></div>';  
  76.         var i="margin:0 1px;height:1px;overflow:hidden;"+g;  
  77.           
  78.         //画圆角边框,以及下部尖角  
  79.         a.innerHTML='<div style="display:none;position:absolute;top:0;left:0;z-index:2;background:#fff;cursor:pointer;cursor:hand">' 
  80.                     +'<a href="'+ url +'" style="color:#444;text-decoration:none" target=_blank>' 
  81.                         +'<div style="'+i+'"></div>' 
  82.                         +'<div style="text-align:center;border-left:1px solid;border-right:1px solid;border-color:'+f+'">' 
  83.                             +h  
  84.                             +'<div style="margin:0 auto;white-space:nowrap;padding:.2em 0 0">' 
  85.                             +'<font size=-1><span>'+tipText+'</span></font>' 
  86.                             +'</div>' 
  87.                             +h  
  88.                         +'</div>' 
  89.                         +'<div style="'+i+'"></div>' 
  90.                         +'<div style="height:4px" align="center">' 
  91.                         +'<div style="position:relative;top:-1px;z-index:3;width:8px;overflow:hidden;margin:0 auto;height:4px;background:no-repeat -260px '+ -c.spriteCoordinateList[0].y +'px"></div>' 
  92.                         +'</div>' 
  93.                     +'</a></div>';  
  94.         this.h=a.getElementsByTagName("span")[0];  
  95.         document.body.appendChild(a);  
  96.         a.firstChild.firstChild.lastChild.firstChild.style.backgroundImage="url("+l+")";//尖角  
  97.         this.element=a.firstChild;  
  98.         var j=objectList.length;  
  99.         objectList.push(...{icon:tbItem,element:this.element});  
  100.         addListener(this.element,"mouseover",j);  
  101.         addListener(this.element,"mouseout",j)  
  102.     }  
  103.       
  104.     /**//**  
  105.      *渲染指定帧的tip  
  106.      *  
  107.      *@param int framenum 帧数  
  108.      */ 
  109.     ToobarTip.prototype.play=function(framenum)...{  
  110.         var d=this.toolbaritem.element.firstChild,b=...{left:-d.offsetLeft,top:0};  
  111.         while(d)...{  
  112.             b.left+=d.offsetLeft;  
  113.             b.top+=d.offsetTop;  
  114.             d=d.offsetParent  
  115.         }  
  116.         var a=this.element.style;  
  117.         if(a.display=="none")  
  118.         ...{  
  119.             a.visibility="hidden";  
  120.             a.display="block" 
  121.         }  
  122.         var f=this.element.offsetHeight,g=this.h.offsetWidth+14;  
  123.         if(a.visibility=="hidden")  
  124.         ...{  
  125.             a.display="none";  
  126.             a.visibility="visible" 
  127.         }  
  128.         a.width=g+"px";  
  129.         b.left -= (g - this.toolbaritem.element.parentNode.offsetWidth)/2;  
  130.         b.top -= f - 3;  
  131.         b.top += 20 * Math.pow(1-framenum/this.toolbaritem.frames,3);  
  132.         a.left = b.left+"px";  
  133.         a.top = b.top+"px";  
  134.         if(framenum == 0||this.toolbaritem.popup<0)//moveout时,tip层直接隐藏  
  135.             a.display="none";  
  136.         else...{  
  137.             a.display="";  
  138.             var e = framenum/this.toolbaritem.frames;  
  139.             if("opacity" in a)  
  140.                 a.opacity=e;  
  141.             else if("MozOpacity" in a)  
  142.                 a.MozOpacity=e;  
  143.             else if("KhtmlOpacity" in a)  
  144.                 a.KhtmlOpacity=e;  
  145.             else if("filter" in a)  
  146.                 a.filter="alpha(opacity="+e*100+")" 
  147.         }  
  148.     };  
  149.     /**//**  
  150.      *标签  
  151.      *  
  152.      *  
  153.      */ 
  154.     function ToolbarItem(element, coordinates, color, url, tooltip)...{  
  155.         this.element = element;  
  156.         this.mouseTimeout = null;//当前执行  
  157.         this.frames = coordinates.length-1;//帧数  
  158.         this.spriteCoordinateList = coordinates;//各帧图片坐标位置  
  159.         this.color = color;//提示文字的颜色  
  160.         this.d = null;  
  161.         this.g = element.getElementsByTagName("div")[1];//动画帧显示区域  
  162.         this.tip = new ToobarTip(this, url, tooltip);//初始化动画场景  
  163.         this.popup = -1;//1=弹出,-1=收回  
  164.         this.currentFrameNum = 1;//当前动画播放帧  
  165.         var itemIndex = objectList.length;  
  166.         objectList.push(...{icon:this,element:element});  
  167.         addListener(element, "mouseover", itemIndex);  
  168.         addListener(element, "mouseout", itemIndex);  
  169.     }  
  170.       
  171.     ToolbarItem.prototype.doPlay=function(c,d)...{  
  172.         var b=this;  
  173.         return function()...{b.play(c,d)}  
  174.     };  
  175.       
  176.     /**//**  
  177.      *渲染指定帧  
  178.      *  
  179.      *@param int ispopup 弹出或收回:1=弹出,-1=收回  
  180.      *@param int framenum 帧数  
  181.      */ 
  182.     ToolbarItem.prototype.play=function(ispopup, framenum)...{  
  183.         if(ispopup)...{  
  184.             this.popup=ispopup;  
  185.             this.mouseTimeout=window.clearTimeout(this.mouseTimeout);  
  186.             this.d=window.clearTimeout(this.d)  
  187.         }  
  188.         if(typeof framenum != "undefined")this.currentFrameNum = framenum;  
  189.           
  190.         //当前帧图片  
  191.         var b=this.spriteCoordinateList[this.currentFrameNum];//当前帧图片坐标位置  
  192.         this.g.style.backgroundPosition=-b.x+"px "+-b.y+"px";//得到当前帧的图片  
  193.         //当前帧提示层渲染  
  194.         this.tip.play(this.currentFrameNum);  
  195.           
  196.         //当前帧文字链接样式  
  197.         var a=this.element.style;  
  198.         if(this.popup<0)...{//mouseout  
  199.             a.color="#444";  
  200.             a.textDecoration="none" 
  201.         }else...{//mouseover  
  202.             a.color=this.color;  
  203.             a.textDecoration="underline" 
  204.         }  
  205.           
  206.         this.currentFrameNum += this.popup;  
  207.         if(this.currentFrameNum > this.frames)  
  208.             this.currentFrameNum = this.frames-1;  
  209.         else if(this.currentFrameNum<0)  
  210.             this.currentFrameNum = 1;  
  211.         else 
  212.             this.d=window.setTimeout(this.doPlay(undefined,undefined),o);//动画计时器  
  213.     };  
  214.     //页面加载完毕时,初始化标签  
  215.     register(window,"load",function()...{  
  216.         try...{  
  217.             document.execCommand("BackgroundImageCache",false,true)  
  218.         }catch(c)...{}  
  219.           
  220.         var d='<table style="margin:2em auto;border-collapse:collapse;line-height:1.4em" cellpadding="3" cellspacing="2" border="0"><tr>';  
  221.         //初始化各标签数据,及其页面代码  
  222.         for(var b=0;b<r.length;++b)...{  
  223.             var a=r[b];  
  224.             a.url="/url?ct=pro&cd="+b+"&source=cwh&q="+encodeURIComponent(a.url);  
  225.             var f=[],g=n*b;//g=当前标签的top  
  226.             //计算各帧图片坐标位置  
  227.             for(var e=0;e<7;++e)if(e>4)f.push(...{x:m*4,y:g-a.yAdjust[e-5]});else f.push(...{x:m*e,y:g});  
  228.             a.coordinates=f;  
  229.             d+='<td style="text-align:center;padding:0 .35em 0 .4em;margin:0;cursor:pointer;cursor:hand"><a style="color:#444;text-decoration:none;" href="'+a.url+'" target=_blank><div><div style="width:52px;height:37px;margin:.5em auto;cursor:pointer;cursor:hand;background: no-repeat 0 '+-g+'px"></div><span style="white-space:nowrap"><font size=-1>'+a.name+"</font></span></div></a></td>" 
  230.         }  
  231.         d+="</tr></table>";  
  232.           
  233.         //获取插入点  
  234.         var h=document.getElementById("tb");  
  235.         h.innerHTML=d;  
  236.         var i=h.getElementsByTagName("a");  
  237.         for(var b=0;b<i.length;++b)...{  
  238.             i[b].firstChild.firstChild.style.backgroundImage="url("+l+")";  
  239.             new ToolbarItem(i[b],r[b].coordinates,r[b].color,r[b].url,r[b].tooltip)  
  240.         }  
  241.     }  
  242.     );  
  243. })();