2013-02-24 28 views
0

我有一個導航欄,我一旦點擊了該項目就開始動畫。我想在導航完成後關注鏈接。繼jQuery事件之後的鏈接

HTML:

<div id="project-nav"> 
    <ul> 
     <a href="" > 
      <li class="project-3">1 
       <ul> 
        <li>WAIKANAE BEACH HOUSE</li> 
       </ul> 
      </li> 
     </a> 

     <a href=""> 
      <li class="project-4">2 
       <ul> 
        <li>WAITAHEKE WETLAND HOME</li> 
       </ul> 
      </li> 
     </a> 

     <a href=""> 
      <li class="project-1">3 
       <ul> 
        <li>WAIKANAE BEACH HOUSE</li> 
       </ul> 
      </li> 
     </a> 

腳本:

$("#project-nav a").click(function(ev) 
{ 
    ev.preventDefault(); 

    var $self=$(this); 

    $(".project-1").animate({"top": "+=-500px"}, 1200, function() { showComplete() }); 
    $(".project-2").animate({"top": "+=-500px"}, 1400, function() { showComplete() }); 
    $(".project-3").animate({"top": "+=-500px"}, 1000, function() { showComplete() }); 
    $(".project-4").animate({"top": "+=-500px"}, 1000, function() { showComplete() }); 
    $(".project-5").animate({"top": "+=-500px"}, 1000, function() { showComplete() }); 
}); 

function showComplete() 
{ 
    document.location = $self.attr('href'); 
} 

這裏是我到目前爲止,動畫作品,但它並沒有跟隨鏈接。

+1

只是一個小側面說明'window.location'比'document.location'更可靠。在大多數情況下,它沒有什麼區別,但如果你有例如''在你的html代碼中,'document.location'指向'img',但是'window.location'仍然指向位置對象。 – 2013-02-24 23:49:34

回答

0

問題是$self被宣佈在showComplete範圍之外。

試試這個:

$("#project-nav a").click(function(ev) { 

    ev.preventDefault(); 
    var $self=$(this); 

    $(".project-1").animate({"top": "+=-500px"}, 1200, showComplete); 
    $(".project-2").animate({"top": "+=-500px"}, 1400, showComplete); 
    $(".project-3").animate({"top": "+=-500px"}, 1000, showComplete); 
    $(".project-4").animate({"top": "+=-500px"}, 1000, showComplete); 
    $(".project-5").animate({"top": "+=-500px"}, 1000, showComplete); 

    function showComplete(){ 
     window.location = $self.attr('href'); 
    } 
}); 

我所做的就是移動showComplete的事件處理中。

+0

謝謝你堆! – Spade 2013-02-24 23:32:07

+0

很高興能有所幫助。如果它解決了您的問題,請務必將答案標記爲已接受! – KaeruCT 2013-02-24 23:39:35

0

$ self已超出範圍showComplete()

您應該傳遞一個參數。

爲例

$(".project-5").animate({"top": "+=-500px"}, 1000, function() { showComplete($self.attr("href")) }); 

function showComplete(pUrl){ 
    document.location = pUrl; 
} 
0

我的猜測是$self是不確定的。它只存在於其創建的功能中。

您需要:showComplete($ self);

然後在創建函數:

showComplete(el){ 
    document.location = el.attr('href'); 
}