2013-02-24 64 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'); 
}