JS平滑滚动加载会干扰Click上的平滑滚动

时间:2015-09-19 10:33:13

标签: javascript jquery hyperlink anchor smooth-scrolling

我是JS的新手,尝试使以下代码正常工作,以便主页自动滚动加载,其他页面上的锚点链接在点击时平滑滚动...

<script>

    $(function(){
    $('html, body').animate({
    scrollTop: $('.destination').offset().top
    }, 2000);
    return false;

    $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 2000);
        return false;
      }
    }
   });
   });

</script>

非常感谢任何帮助!

谢谢, 安德烈亚斯

1 个答案:

答案 0 :(得分:0)

这是因为您之前通过返回false来关闭该函数。 正确的代码是 -

 $(function(){
    $('html, body').animate({
    scrollTop: $('.destination').offset().top
    }, 2000);

    $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 2000);
        return false;
      }
    }
   });
   });
相关问题