如何在 jQuery 中使用 vh 单位获取文档高度


$(window).scroll(function() {
   $scrollingDiv.css("display", (($(window).scrollTop() / $(document).height()) > 0.1) ? "block" : "");
});

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

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

因此,您可以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");
  }
});

代码笔链接:https://codepen.io/anon/pen/GWzVwO

我相信

正确的语法是 $(selector).attr(attribute,value)因此,对于您的具体示例$(document).attr('height','100vh')

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

最新更新