【问题标题】:How do I get document height using vh units in jQuery?如何在 jQuery 中使用 vh 单位获取文档高度?
【发布时间】:2017-04-02 01:08:25
【问题描述】:
$(window).scroll(function() {
   $scrollingDiv.css("display", (($(window).scrollTop() / $(document).height()) > 0.1) ? "block" : "");
});

如何将单位 $(document).height()) > 0.1) 更改为 100vh?我没有太多 jQuery 经验。

【问题讨论】:

  • 请检查$(document).height()) > 0.1)中的括号
  • 应该是$(document).height() 还是0.1 应该是100vh? ...我也很难理解你想做什么,所以请说清楚一点?
  • vh 不是一个单位。视口的高度 (vh) 也以像素为单位定义。 (如果这是问题。)
  • 100vh 基本上是视口的高度,即$(window).height()

标签: javascript jquery viewport-units


【解决方案1】:

VH:视口高度类似于$(window).height();Source

所以你可以检查条件if (windowHeight > 0.1)然后

$("body").height(windowHeight);

代码:

$(document).ready(function() {
  var windowHeight = $(window).height();

  if (windowHeight > 0.1) {
    alert("height is greater than 0.1");
    $("body").height(windowHeight);
  } else {
    alert("height is less than 0.1");
  }

});

Codepen 链接:https://codepen.io/anon/pen/GWzVwO

【讨论】:

    【解决方案2】:

    我相信正确的语法是 $(selector).attr(attribute,value) 所以对于你的具体例子$(document).attr('height','100vh')

    这是一个希望有帮助的链接 --> https://www.w3schools.com/jquery/html_attr.asp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2014-09-30
      • 2010-11-11
      • 1970-01-01
      • 2017-09-20
      • 2014-10-08
      相关资源
      最近更新 更多