如何检查锚 URL 是否会转到同一站点(并平滑滚动,否则离开页面)



>我正在尝试创建一个防弹脚本,该脚本确实使用 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.");
    }
});

最新更新