找回密码
 加入
搜索

[代码] 用javascript来实现动画导航

[复制链接]
发表于 2007-12-15 02:59:17 | 显示全部楼层 |阅读模式
原理

小时候,总喜欢看动画片吧,动画片是怎样实现的呢?记得妈妈说是一张画一张画切换过去(啊?那一部葫芦兄弟要画多少副画啊? -_-! ),其实我们现在做的也是这样,用一个图片,这个图片里有很多个小图,来显示动画轨迹.按时间来移动图片,那图片不是会动了啊?(不知道,表达清楚了没…语文很重要啊!!)


准备

我们需要一张图片,一个大脑,一张会笑的脸(不笑效果就出不来了….)!!下面是我准备的图片(ps水平有限^_^)…

代码

我们看到上面的图片,想象下,它动起来是多么的优美啊…

css
  1. .Gnb_btn_div{
  2.     width:90px;
  3.     height:75px;
  4.     overflow:hidden;
  5.     display:block;
  6.     position:absolute;
  7. }  
  8. .Gnb_btn_img{
  9.     width:100%;
  10.     height:525px;
  11.     display:block;
  12.     overflow:hidden;
  13.     text-indent:-500px;
  14. }
  15. #gnb_btn_01 .Gnb_btn_img {
  16.     background-image:url(http://www.wler.cn/blog/img/friend.gif)
  17. }
复制代码
javascript
  1. <script type="text/javascript">
  2. // <![CDATA[
  3. function GNB(_7c){
  4.     //初始化一些参数
  5.     this.iImgNum=7;            //小图片个数
  6.     this.iImgHeight=75;        //小图片高度
  7.     this.iOverSpeed=50;        //鼠标经过时候切换的时间
  8.     this.iOutSpeed=50;        //鼠标离开时候切换的时间
  9.     this.eventObj=_7c;        //取得图片对象  
  10.     this.MouseOverFlag=false;
  11.     this.imageIndex=0;
  12.     if(this.eventObj==null){return;}
  13.     this.eventObj.parentClass=this;this.eventAssign();
  14. }  
  15. GNB.prototype.eventAssign=function(){//注册事件
  16.     this.eventObj.onmouseover=this.menuMouseOver;
  17.     this.eventObj.onmouseout=this.menuMouseOut;
  18. };  
  19. GNB.prototype.menuMouseOver=function(){//鼠标经过
  20.     if(this.parentClass.MouseOverFlag!=false){return;}
  21.     this.parentClass.MouseOverFlag=true;
  22.     this.parentClass.clearTimeOut();
  23.     this.parentClass.menuMouseOverTimer();
  24. };  
  25. GNB.prototype.menuMouseOut=function(){//鼠标离开
  26.     this.parentClass.MouseOverFlag=false;
  27.     this.parentClass.clearTimeOut();
  28.     this.parentClass.menuMouseOutTimer();
  29. };  
  30. GNB.prototype.menuMouseOverTimer=function(){//经过图片位置递增
  31.     var _7d=this;
  32.     if(this.imageIndex>=this.iImgNum){return;}
  33.     this.eventObj.scrollTop=this.imageIndex*this.iImgHeight;
  34.     this.imageIndex++;
  35.     this.setTimerID=setTimeout(function(){_7d.menuMouseOverTimer();},this.iOverSpeed);
  36. };  
  37. GNB.prototype.menuMouseOutTimer=function(){////经过图片位置递减
  38.     var _7e=this;if(this.imageIndex<0){return;}
  39.     this.eventObj.scrollTop=this.imageIndex*this.iImgHeight;
  40.     this.imageIndex--;
  41.     this.setTimerID=setTimeout(function(){_7e.menuMouseOutTimer();},this.iOutSpeed);
  42. };  
  43. GNB.prototype.clearTimeOut=function(){//取消定时
  44.     clearTimeout(this.setTimerID);
  45. };
  46. // ]]>
  47. </script>
复制代码
xhtml
  1. <div class="Gnb_btn_div" id="gnb_btn_01">
  2. <a class="Gnb_btn_img" href="#1" mce_href="#1">找朋友</a>
  3. </div>  

  4. <script type="text/javascript">
  5. // <![CDATA[
  6. var GNB1=new GNB(document.getElementById("gnb_btn_01"));//实例单个按钮,当然也可以多个
  7. // ]]>
  8. </script>
复制代码
具体演示请看http://www.wler.cn/blog/?p=8  http://www.wler.cn/blog/lab/nav.html

Archiver|手机版|小黑屋|乱弹琴|重庆网址之家|多来米发 |

GMT+8, 2026-10-14 12:56 AM

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表