簡體   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