代码之家  ›  专栏  ›  技术社区  ›  Blackbam

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

  •  0
  • Blackbam  · 技术社区  · 6 年前

    我正在尝试创建一个防弹脚本,它可以通过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 ###### . 如果锚以hashtag开头,则它只能位于同一页上。有锚链接,如:

    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).href window.location.href 页面是否重新加载?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Blackbam    6 年前

    这就是我最后得出的结论:

    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.");
        }
    });