繁体   English   中英

jQuery平滑滚动不滚动

[英]jQuery Smooth Scroll not scrolling

我试图将平滑滚动添加到站点中的命名锚点。 我有一个jQuery插件,它仅调用内部命名的锚并为html和body设置动画。 我以前在其他站点上使用过它,但是效果很好,但是我不知道为什么它在这里不起作用。 奇怪的是,相同的代码在Apple Safari中可以正常工作,但在Google Chrome中却不能。 我在Chrome上可以正常使用的所有其他网站。 我无法弄清楚这一点。

以下是我的HTML。 它只是一个带有标签的div,并且在下面的div中有匹配的锚点。

<div id="slide1" class="slide">
<a href="#section">
<div id="scrollbtn">
<img src="images/downarrow.png" alt="Down Arrow">
</div>
</a>
</div>


<div id="main">
<a id="section" class="link"></a>
<p>Test</p>
</div>

以下是jQuery插件。 在我使用过的其他站点上,它运行良好,我不明白为什么它在这里不起作用。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function() {
  $('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
        }, 500);
        return false;
      }
    }
  });
});
</script>

任何帮助将不胜感激。

    $(function() {
    $('a').bind('click',function(event){
        var $anchor = $(this);

        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 800);
        event.preventDefault();
    });
});

是实现它的另一种方法。

码笔

让Codepen工作。 让我知道是什么给您带来了问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM