2011-09-12 27 views
3

我使用jQuery.toggle和.animate作爲位於頁面底部的導航菜單。 nav由兩個div組成,一個作爲外部容器,另一個作爲容納導航按鈕的內部容器等。切換和動畫錨鏈接不再導航

我試圖實現的是;你點擊導航,它的位置動起來,並關閉導航,你只需再次點擊它,它動畫回到它的原始位置在頁面的底部。

我遇到的問題是,當您單擊導航按鈕時什麼也沒有發生,即使瀏覽器狀態欄顯示目標URL,它也不會將您帶入鏈接。

這裏的a link

的jQuery:

$('#navContainer').toggle(function() { 
    $('#navInner').animate({ 
     bottom: '+=350' 
    }, 400, 'swing', function() { 
    }); 
}, function() { 
    $('#navInner').animate({ 
     bottom: '-=350' 
    }, 400, 'swing', function() { 
    }); 
}); 

CSS:

#navContainer { 
    background-color:#0FF; 
    height:520px; 
    margin:0px 0px 0px 60px; 
    position:fixed; 
    cursor:pointer; 
    bottom: 0px; 
} 

#navInner { 
    background:url(../images/appinstructions/nav2.png); 
    background-repeat:no-repeat; 
    width:638px; 
    height:491px; 
    position:absolute; 
    bottom:-350px; 
} 

HTML:

<div id="navContainer"> 
    <div id="navInner"> 
     <a id="navhomeBtn" href="appInstuc.html"></a> 
     <a id="navhelpBtn" href="appInstuc.html"></a> 
     <a id="geBtn" href="http://www.ge.com/" target="_blank"></a> 
     <div id="pages"> 
      <a href="ipadApp1.html"><img src="images/appinstructions/page1.png" alt="page 1" class="page"/></a> 
      <a href="app1-1.html"><img src="images/appinstructions/page2.png" alt="page 2" class="page"/></a> 
      <a href="appRoots.html"><img src="images/appinstructions/page3.png" alt="page3"class="page"/></a> 
     </div><!--close pages--> 
    </div><!--close navInner--> 
</div><!--close navContainer--> 

回答

3

這個問題似乎是與.toggle功能,如果您使用.click鏈接工作。使用.toggle鏈接不起作用的原因是因爲鏈接是#navInner div的子項,因此單擊該鏈接會觸發.toggle事件。

我建議做這樣的事情,而不是:

var showing = false; 
$('#navInner').click(function(e){ 

    if(e.target != this) return; 

    // Work out which direction we're moving in 
    var topValue = ""; 
    if(showing){ 
     topValue = "+=350"; 
    } else { 
     topValue = "-=350"; 
    } 

    // Change the showing variable to opposite 
    showing = !(showing); 

    // Begin the animation 
    $(this).animate({ 
     top: topValue 
    }, 400, 'swing', function(){ 
     // Do nothing 
    }); 

}); 

這會給你切換方法的效果,但如果你點擊的子元素之一它不會動畫#navInner div

如果你希望它仍然動畫#navInner div,只是刪除以下行:

if(e.target != this) return; 

(儘管因爲它們是鏈接,帶你到不同的網頁,我懷疑動畫的#navInner div是需要)

+0

謝謝!完美工作 – jsavage980

+0

沒問題。習慣上將您用作答案的答案標記爲原始問題。 –