原生js和jquery分別實現(xiàn)橫向?qū)Ш讲藛涡Ч?/h1>

字號:


    這篇文章主要介紹了原生js和jquery分別實現(xiàn)橫向?qū)Ш讲藛涡Ч南嚓P(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    本文實例為大家分享了原生js和jquery橫向?qū)Ш讲藛蔚闹谱鞣椒?,供大家參考,具體內(nèi)容如下
    原生javascript實現(xiàn):
    這一次要實現(xiàn)的是鼠標(biāo)放上去以后,菜單欄被選中的那一欄水平拉伸,鼠標(biāo)離開后水平收縮。并帶有一定的時間性,使肉眼能夠看出其拉伸收縮的動畫效果。
    開始用javascript進行編寫:
    首先在之前水平方向的導(dǎo)航欄上進行操作,將第一欄和選中欄的樣式只改變?yōu)楸尘白兒谏淖肿儼咨?BR>    .on,a:hover{background:#000000;color:#FFFFFF;} 
    之后開始寫javascript腳本:
    <script> 
     window.onload=function(){ 
     var A=document.getElementsByTagName("a"); 
     for(var i=0;i<A.length;i++) 
     { 
      A[i].onmouseover=function(){ 
      clearInterval(this.time); 
       var This=this; 
       This.time=setInterval(function(){ 
       if(This.offsetWidth>=200) 
        { 
        clearInterval(This.time); 
        } 
       This.style.width=This.offsetWidth+8+"px"; 
       },50) 
      } 
       A[i].onmouseout=function(){ 
       clearInterval(this.time); 
       var This=this; 
       This.time=setInterval(function(){ 
       if(This.offsetWidth<=120) 
        { 
        This.style.width="120px"; 
        clearInterval(This.time); 
        } 
       This.style.width=This.offsetWidth-8+"px"; 
       },50) 
      } 
      } 
     } 
    </script> 
    剖析一下這段代碼:
    第一層,window.onload,頁面加載的時候調(diào)用這個函數(shù)。
    第二層,for循環(huán),用document.getElementsByTagName("a")獲得導(dǎo)航欄數(shù)組,遍歷為其添加第三層的效果。
    第三層,一個onmouseover,一個onmouseout,分別實現(xiàn)鼠標(biāo)覆蓋和鼠標(biāo)離開的效果。
    第四層,setInterval和clearInterval方法,參數(shù)50ms.
    第五層,核心部分,修改this.style.width,每次50ms加減8px,增加判斷語句到達(dá)邊界。
    細(xì)節(jié)部分:采用先加減8px再進行判斷,我認(rèn)為應(yīng)該倒過來,不必要先處理再判斷,會浪費資源。還有就是在第三層開始后必須先清除時間機制,否則會容易出現(xiàn)重疊動畫的紊亂狀況。
    最后實現(xiàn)的動畫就是:鼠標(biāo)放上去某一欄后,120px的菜單欄將每50ms伸長8px,直至到達(dá)200px停下;當(dāng)鼠標(biāo)離開后,該欄又將以50ms收縮8px的速度恢復(fù)到120px.
    看一下總代碼和效果圖:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>導(dǎo)航欄</title> 
    <style> 
    *{margin:0;padding:0;font-size:20px} 
    ul{list-style:none;height:50px;border-bottom:#000000 solid;padding-left:30px}  
    li{float:left;margin-top:20px;} 
    a{text-decoration:none;display:block;height:30px;line-height:30px;width:120px;margin-bottom:1px;background:#FFFFFF;color:#000000;text-align:center} 
    .on,a:hover{background:#000000;color:#FFFFFF;} 
    </style> 
    <script> 
     window.onload=function(){ 
     var A=document.getElementsByTagName("a"); 
     for(var i=0;i<A.length;i++) 
     { 
      A[i].onmouseover=function(){ 
      clearInterval(this.time); 
       var This=this; 
       This.time=setInterval(function(){ 
       if(This.offsetWidth>=200) 
        { 
        clearInterval(This.time); 
        } 
       This.style.width=This.offsetWidth+8+"px"; 
       },50) 
      } 
       A[i].onmouseout=function(){ 
       clearInterval(this.time); 
       var This=this; 
       This.time=setInterval(function(){ 
       if(This.offsetWidth<=120) 
        { 
        This.style.width="120px"; 
        clearInterval(This.time); 
        } 
       This.style.width=This.offsetWidth-8+"px"; 
       },50) 
      } 
      } 
     } 
     </script> 
    </head> 
    <ul> 
    <li> 
    <a href="#">首 頁</a> 
    </li> 
    <li> <a href="#">今日新聞</a></li> 
    <li><a href="#">周邊故事</a></li> 
    <li><a href="#">天氣預(yù)報</a></li> 
    <li><a href="#">好書推薦</a></li> 
    </ul> 
    </html> 
    名單
    下面用jquery實現(xiàn)同樣的效果:
    先下載一個jQurey1.2.6,引用到html中去
    <script type="text/javascript" src="jquery-1.2.6.js"></script> 
    下載地址:Jquery1.2.6下載
    [html] view plain copy print?
    <script> 
    $(function(){ 
     $('a').hover( 
      function(){ 
       $(this).stop().animate({"width":"200px"},200  );}, 
      function(){ 
       $(this).stop().animate({"width":"120px"},200 
       );}  
     ) 
     }) 
    </script>
    同樣,這段代碼是包含在$(function(){})中,相當(dāng)于window.onload的作用。
    之后$('a')獲取a標(biāo)簽,其提供一個hover方法,這個方法里面要提供兩個函數(shù),一個移入一個移出,我們將其設(shè)定為移入時200ms增加到200px,移出時200ms收縮到120px.
    animate即自定義動畫的方法,在這里是設(shè)置寬度動態(tài)變化。
    要在處理前用stop(),把上個動畫清理掉。
    效果是一樣的,但代碼量少。
    以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。