【问题标题】:How to scroll down to 90% of your document or body's height?如何向下滚动到文档或正文高度的 90%?
【发布时间】:2015-08-14 18:01:04
【问题描述】:

我想向下滚动到文档或正文高度的 90%,不是设备的屏幕高度。我知道下面的这个示例会向下滚动到我的页面或文档的某个元素。

$("html, body").animate({ scrollTop: $("#emptydisplay").offset().top }, 500);

但这不是我需要的。我需要自动向下滚动到 HTML 文档高度的 90%。我试过这样的事情

 $('html,body').animate({ scrollTop: $(document).height() * 0.90 });

但它一直向下滚动到底部。

我如何在 jquery 中做到这一点?谢谢...

【问题讨论】:

  • 使用 90% 的 (bodyhieght-windowheight )
  • 你能设置一个 fiddle/jsbin 吗?我看到jsbin.com/mugopebora/edit?html,css,js,output 工作正常
  • @mohamedrias 我不确定为什么它在你的小提琴上工作,但在我的页面上它不会像我期望的那样工作。不过现在已经解决了。感谢所有帮助...干杯

标签: javascript jquery html css


【解决方案1】:

scrollTop 表示窗口的顶部。 scrollTop: 100% 表示 full height of the page - the height of the window。如果你想要90%,你需要这个:

$('html,body').animate({ scrollTop: $(document).height() * 0.9 - $(window).height() }, 500);

【讨论】:

    【解决方案2】:

    您应该从文档高度中删除窗口高度

    $('html,body').animate({ scrollTop: ($(document).height() * 0.90)-$(window).height() });
    

    【讨论】:

    • 只在这个页面打开控制台并测试一下
    【解决方案3】:

    你应该减去窗口的高度

     $('html,body').animate({ scrollTop: $(document).height() * 0.90 - $(window).height() });
    

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 2016-03-21
      • 2021-10-15
      • 2011-01-28
      • 2018-06-05
      • 2022-01-26
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      相关资源
      最近更新 更多