簡體   English   中英

如何檢查錨點URL是否將轉到同一站點(並平滑滾動,否則離開頁面)?

[英]How to check if anchor URL will go to the same site (and smooth scroll, leave page otherwise)?

我正在嘗試創建一個防彈腳本,該腳本確實會使用jQuery動畫滾動到錨點,但前提是該錨點的href位置指向我當前站點中的現有錨點。

這是腳本:

  var assConfig;

  assConfig = {
    duration: 500,
    easing: 'swing',
    complete: null
  };

  //Smooth scrolling with links
  $('a[href*=\\#]:not([href$=\\#])').on('click', function(event) {
    var hash;
    // if the anchor is on another site or subsite simply proceed with the default action
    // which will probably be leaving the site and go to ghe href location
    if (!o(event.target).attr("href")).startsWith("#") && (##### CONDITION-PART ######)) {
      return;
    }
    event.preventDefault();
    hash = '#' + $(event.target).attr("href").split("#")[1];
    if (typeof $(hash).offset() !== 'undefined') {
      $('html,body').animate({
        scrollTop: $(this.hash).offset().top
      }, assConfig.duration, assConfig.easing, assConfig.complete);
    }
  });

  // Smooth scrolling when the document is loaded and ready
  $(document).ready(function() {
    if (typeof $(location.hash).offset() !== 'undefined') {
      $('html,body').animate({
        scrollTop: $(location.hash).offset().top
      }, assConfig.duration, assConfig.easing, assConfig.complete);
    }
  });

我的問題是要放入什么而不是##### CONDITION-PART ###### 如果錨點以井號標簽開頭,則只能位於同一頁面上。 有錨鏈接,例如:

href="/?controller=info&action=site/foo#my-beautiful-anchor-target"
href="/#my-beautiful-anchor-target"
href="http://www.example.com/?foo#my-beautiful-anchor-target"

有時,盡管它們不同,但它們可能表示相同的站點,例如:

href="http://www.example.com/?foo#my-beautiful-anchor-target"
href="/?foo#my-beautiful-anchor-target"
href="?foo#my-beatiful-anchor-target"

那么檢測$(event.target).hrefwindow.location.href是否是頁面重新加載的最佳方法是什么?

這就是我最終得到的:

event.target.pathname !== window.location.pathname 
|| event.target.hostname !== window.location.hostname 
|| event.target.protocol !== window.location.protocol 
|| event.target.search !== window.location.search

最終完整的解決方案:

var assConfig = {
    duration: 500,
    easing: 'swing',
    complete: null
};

//Smooth scrolling with links
$('a[href*=\\#]:not([href$=\\#])').on('click', function (event) {
    var hash;
    if (event.target.pathname !== window.location.pathname || event.target.hostname !== window.location.hostname || event.target.protocol !== window.location.protocol || event.target.search !== window.location.search) {
        return;
    }
    event.preventDefault();
    hash = '#' + $(event.target).attr("href").split("#")[1];
    if (typeof $(hash).offset() !== 'undefined') {
        $('html,body').animate({
            scrollTop: $(this.hash).offset().top
        }, assConfig.duration, assConfig.easing, assConfig.complete);
    } else {
        console.log("Note: Scrolling to the anchor ist not possible as the anchor does not exist.");
    }
});

// Smooth scrolling when the document is loaded and ready
$(document).ready(function () {
    if (typeof $(location.hash).offset() !== 'undefined') {
        $('html,body').animate({
            scrollTop: $(location.hash).offset().top
        }, assConfig.duration, assConfig.easing, assConfig.complete);
    } else {
        console.log("Note: Scrolling to the anchor ist not possible as the anchor does not exist.");
    }
});

暫無
暫無

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

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