用javascript來實現動畫導航效果的代碼_javascript技巧

來源:互聯網
上載者:User
誰在用這些導航
google是個大公司,全世界都有google的腳印,韓國的google動畫效果非常不錯,藍色理想論壇裡已經有人挖過來了,可惜js寫的太多了,那自己寫一個吧?好,就這麼幹!
原理
小時候,總喜歡看動畫片吧,動畫片是怎樣實現的呢?記得媽媽說是一張畫一張畫切換過去(啊?那一部葫蘆兄弟要畫多少副畫啊? -_-! ),其實我們現在做的也是這樣,用一個圖片,這個圖片裡有很多個小圖,來顯示動畫軌跡.按時間來移動圖片,那圖片不是會動了啊?(不知道,表達清楚了沒…語文很重要啊!!)
準備
我們需要一張圖片,一個大腦,一張會笑的臉(不笑效果就出不來了….)!!下面是我準備的圖片(ps水平有限^_^)…

代碼

我們看到上面的圖片,想象下,它動起來是多麼的優美啊…

css
複製代碼 代碼如下:


.Gnb_btn_div{ 
    width:90px; 
    height:75px; 
    overflow:hidden; 
    display:block; 
    position:absolute; 
}      

.Gnb_btn_img{ 
    width:100%; 
    height:525px; 
    display:block; 
    overflow:hidden; 
    text-indent:-500px; 
} 
#gnb_btn_01 .Gnb_btn_img { 
    background-image:url(http://www.wler.cn/blog/img/friend.gif) 
}

javascript

複製代碼 代碼如下:

<script type="text/javascript"> 
// <![CDATA[ 
function GNB(_7c){ 
    //初始化一些參數 
    this.iImgNum=7;            //小圖片個數 
    this.iImgHeight=75;        //小圖片高度 
    this.iOverSpeed=50;        //滑鼠經過時候切換的時間 
    this.iOutSpeed=50;        //滑鼠離開時候切換的時間 
    this.eventObj=_7c;        //取得圖片對象      

    this.MouseOverFlag=false; 
    this.imageIndex=0; 
    if(this.eventObj==null){return;} 
    this.eventObj.parentClass=this;this.eventAssign(); 
}      

GNB.prototype.eventAssign=function(){//註冊事件 
    this.eventObj.onmouseover=this.menuMouseOver; 
    this.eventObj.onmouseout=this.menuMouseOut; 
};      

GNB.prototype.menuMouseOver=function(){//滑鼠經過 
    if(this.parentClass.MouseOverFlag!=false){return;} 
    this.parentClass.MouseOverFlag=true; 
    this.parentClass.clearTimeOut(); 
    this.parentClass.menuMouseOverTimer(); 
};      

GNB.prototype.menuMouseOut=function(){//滑鼠離開 
    this.parentClass.MouseOverFlag=false; 
    this.parentClass.clearTimeOut(); 
    this.parentClass.menuMouseOutTimer(); 
};      

GNB.prototype.menuMouseOverTimer=function(){//經過圖片位置遞增 
    var _7d=this; 
    if(this.imageIndex>=this.iImgNum){return;} 
    this.eventObj.scrollTop=this.imageIndex*this.iImgHeight; 
    this.imageIndex++; 
    this.setTimerID=setTimeout(function(){_7d.menuMouseOverTimer();},this.iOverSpeed); 
};      

GNB.prototype.menuMouseOutTimer=function(){////經過圖片位置遞減 
    var _7e=this;if(this.imageIndex<0){return;} 
    this.eventObj.scrollTop=this.imageIndex*this.iImgHeight; 
    this.imageIndex--; 
    this.setTimerID=setTimeout(function(){_7e.menuMouseOutTimer();},this.iOutSpeed); 
};      

GNB.prototype.clearTimeOut=function(){//取消定時 
    clearTimeout(this.setTimerID); 
}; 
// ]]> 
</script>

xhtml
複製代碼 代碼如下:


<div class="Gnb_btn_div" id="gnb_btn_01"> 
<a class="Gnb_btn_img" href="#1" href="#1">找朋友</a> 
</div>      

<script type="text/javascript"> 
// <![CDATA[ 
var GNB1=new GNB(document.getElementById("gnb_btn_01"));//執行個體單個按鈕,當然也可以多個 
// ]]> 
</script>

示範地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.