【问题标题】:scrollTop() not working after applying overflow values with jQuery?使用 jQuery 应用溢出值后,scrollTop() 不起作用?
【发布时间】:2014-08-13 21:50:50
【问题描述】:

我正在使用 Chris Coyier 的插件(实际上,它是 Devin Sturgeon 的修改版)来动画滚动到锚链接:

// Easing for links pointing to anchors
$(document).ready(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

在我使用 jQuery 应用溢出值之前,它可以完美运行。比如我关闭一个模态窗口后,这段代码负责隐藏水平溢出并将垂直溢出设置为自动:

$('.file-content a.close-reveal-modal').on('click', function() {
  $('html, body').css({
    'overflow-y' : 'auto',
    'overflow-x' : 'hidden'
  });
});

应用这些值后,动画滚动到锚链接的插件停止工作,单击它们不会产生任何结果。如果我在关闭模式时删除负责设置溢出的代码,Chris 的插件每次都会像魅力一样工作。

为什么会这样?

非常感谢任何形式的帮助!

【问题讨论】:

  • 你发现了吗?

标签: javascript jquery html css scroll


【解决方案1】:

我不知道这是否可以帮助你,但这是我发现的一个提示http://upshots.org/javascript/jquery-get-real-height-of-hidden-or-overflow-hidden-elements

您是否尝试过调试?

因为我怀疑在你的例子中

target.offset().top
返回零

【讨论】:

  • 您可能想知道,Beri,Stackoverflow Netiquette 不鼓励使用 URL 链接贬损的帖子。而是在您的帖子中包含这里的优点,一旦被引用的页面不需要永远存在以便仍然提供您想要的内容。您可能需要重新阅读 StackOverflow 的注意事项并重新编辑您的帖子,以表明您遵守社区礼仪的意愿,并避免任何可能认为有必要的版主采取任何不利措施执行上述规则。享受您决定加入的贡献成员的伟大社区。​​span>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多