簡體   English   中英

平滑滾動不起作用jQuery

[英]Smooth scrolling not working jQuery

使用https://css-tricks.com/snippets/jquery/smooth-scrolling/中的代碼,對於我這類人來說,無法弄清楚為什么它不起作用。

腳本:

$('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
        }, 1000);
        return false;
    }
}
});

網站: www.soelite.net/layout/

有什么想法嗎? 該站點跳到div容器,但沒有動畫。

看到這個答案: https : //stackoverflow.com/a/2234749/606104

我在Safari和Chrome(Mac)中遇到了這個問題,發現.scrollTop可以在$(“ body”)上工作,但不能在$(“ html,body”)上工作

要使其在Safari中起作用,請將您的腳本更改為:

$('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) {
      var bodyElement = $("html,body");
      if ($.browser.safari) { 
        bodyElement = $("body")
      }

      bodyElement.animate({
        scrollTop: target.offset().top
      }, 1000);

      return false;
    }
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM