当 ID 和 hash 设置相同时,页面加载时出现平滑滚动问题并且偏移量会跳跃

发布于 2024-10-31 22:50:59 字数 2003 浏览 0 评论 0原文

更新:完全像这样:但是在 jQuery 中......也许我应该花一些时间移植它。 http://davidwalsh.name/mootools-onload-smoothscroll

上下文:Wordpress 网站,带有子导航导航设置为 /page/#idname 以向下滚动页面。

我很难弄清楚如何使页面滚动到 ID(带有偏移量)并设置位置而不跳转。如果您使用哈希加载页面,我将如何让页面从顶部向下滚动?

//SMOOTH SCROLL
function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.position().top - 60;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target+"-section"
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }

请注意偏移量 -60,为了不出现抖动,我将哈希值设置为与 ID 不同,这是我根本不想要的

var targetOffset = $target.position().top - 60;

......

location.hash = target+"-section"

我希望散列保持不变,但我这样做是为了使偏移量起作用。任何帮助将不胜感激。

UPDATE: Exactly like this: but in jQuery... Maybe I should just spend sometime porting it. http://davidwalsh.name/mootools-onload-smoothscroll

The context: Wordpress site, with subnav navigation set to /page/#idname to scroll down the page.

I'm having a hard time trying to figure out how to make the page scroll to the ID (with offset) and setting location without jumping. And if you load the page with the hash, how would I get the page to scroll down from the top?

//SMOOTH SCROLL
function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.position().top - 60;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target+"-section"
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }

Notice the offset -60 and for me to not have the jitter, I set the hash to be different from the ID, which is something I don't want at all...

var targetOffset = $target.position().top - 60;

&

location.hash = target+"-section"

I'd like the hash to stay the same, but I do that so the offset is working. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携余温的黄昏 2024-11-07 22:50:59

简化你的生活: http://plugins.jquery.com/project/ScrollTo

它有一个偏移量功能,您只需将其指向元素本身即可。

这看起来与您对 MooTools 的 SmoothScroll 的更新参考完全相同。 ScrollTo 正是您所需要的。

Simplify your life: http://plugins.jquery.com/project/ScrollTo

It has an offset capability and you just point it at the element itself.

This looks to be exactly like your updated reference to MooTools' SmoothScroll. ScrollTo is exactly what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文