2014-01-22 47 views
2

我正在嘗試爲我的網站製作幻燈片動畫。核心功能在那裏,但我遇到了問題,只要我點擊一個元素(當頁面不是頂部)頁面跳轉到頂部和頂部,然後按照預期製作動畫。我研究了最後幾天,但我的問題似乎是獨一無二的,所以我決定提出這個問題。這是我的本錢:jQuery頁面在執行動畫之前跳轉到頂部

<div class="content"> 
    <div class="inner_content"> 
     <div id="first" class="first"></div> 
     <div id="second" class="second"></div> 
     <div id="third" class="third"></div> 
    </div> 
</div> 

的想法是點擊一個DIV(第一,第二,第三),從頂部因爲導航欄的130像素的偏移滾動起來。這是我的jQuery:

<script> 
    $("div.first").click(function(){ 
     $("html, body").animate({ 
      "scrollTop": $("div.first").offset().top -130 
     }, 500); 
     $("div.second").click(function(){ 
      $("html, body").animate({ 
       "scrollTop": $("div.first").offset().top -130 
      }, 500); 
      $("div.third").click(function(){ 
       $("html, body").animate({ 
        "scrollTop": $("div.first").offset().top -130 
       }, 500); 
</script> 

問題就在於說。頁面會非常快速地跳到頁面頂部,然後返回到上一個位置。之後,滑動上下動畫非常乾淨平滑。我真的不知道問題出在哪裏。我希望有人能幫助我。 在此先感謝。

回答

0

您錯過了一些右括號。

$(document).ready(function() { 
    $("div.first").click(function() { 
     $("html, body").animate({ 
      "scrollTop": $("div.first").offset().top - 130 
     }, 500); 
    }); 
    $("div.second").click(function() { 
     $("html, body").animate({ 
      "scrollTop": $("div.first").offset().top - 130 
     }, 500); 
    }); 
    $("div.third").click(function() { 
     $("html, body").animate({ 
      "scrollTop": $("div.first").offset().top - 130 
     }, 500); 
    }); 
}); 

另外,我也注意到,你觸發div.click動畫,但如果你這樣做對anchor,這是很好的使用event.preventDefault();