簡體   English   中英

頁面加載時,錨點滾動滾動到錨點

[英]Anchor scroll is scrolling to the anchor upon page load

任何幫助,我們將不勝感激! 我的產品頁面頂部有一個按鈕,用於顯示尺碼表,錨定滾動到頁面底部的尺碼表圖像。 但是,頁面加載后,頁面會自動向下滾動到尺寸表。 我可以修改什么,以使頁面不會自動向下滾動到錨點? 這是我的JS:

// Select all links with hashes
$('a[href*="#"]')
  // Remove links that don't actually link to anything
  .not('[href="#"]')
  .not('[href="#0"]')
  .click(function(event) {
    // On-page links
    if (
      location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') 
      && 
      location.hostname == this.hostname
    ) {
      // Figure out element to scroll to
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      // Does a scroll target exist?
      if (target.length) {
        // Only prevent default if animation is actually gonna happen
        event.preventDefault();
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000, function() {
          // Callback after animation
          // Must change focus!
          var $target = $(target);
          $target.focus();
          if ($target.is(":focus")) { // Checking if the target was focused
            return false;
          } else {
            $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
            $target.focus(); // Set focus again
          };
        });
      }
    }
  });

我認為您的意思是頁面在重新加載時向下滾動。

瀏覽器的默認行為是將頁面向下滾動到用戶最后一次訪問該頁面的位置。

為了避免在關閉頁面之前滾動到頂部:

$(window).on('beforeunload', function() {
    $(window).scrollTop(0);
});

暫無
暫無

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

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