2014-01-22 50 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();