jquery(windows).每次调整窗口大小时,高度都会增加

jquery (windows).height increase every time I resize the window

本文关键字:小时 高度 增加 窗口 windows 调整 jquery      更新时间:2023-09-26

我想在调整窗口大小时调整div的大小,使其填充整个浏览器窗口
我在网上搜索了很多,找到了解决方案,但我尝试的每一件事都导致了同样的问题
在第一次加载页面时,它工作得很好,但当我尝试调整窗口大小时,bodyheight的值不断增加。。。

这是我的代码

$(document).ready(function() {
  body_sizer();
  $(window).resize(body_sizer);
});
function body_sizer() {
  var bodyheight = $(window).height();
  $("#contenitore_full").css('height', bodyheight);
}

编辑

好!!!正如我所想的我的错:)这个问题是由wordpress中调用jquery的错误方法引起的。很抱歉耽误了你的时间非常感谢大家ale

您不需要javascript来实现您想要的。你所需要的只是CSS。这是一把小提琴

CSS

body, html { margin:0; height:100% }
#contenitore_full { border:1px solid #FF0000; width:25%; height:99%; display:block; }

HTML

<div id="contenitore_full"></div>

问题是为了使height工作,还需要定义父对象的高度。这就是为什么bodyhtml标签的height也具有100%height

看起来您的"body_sizer"函数可能没有启动。将parens()添加到它的末尾,或者在.resize()中创建一个处理程序函数

$(window).resize(function() {
      var bodyheight = $(window).height();
      $("#contenitore_full").css('height', bodyheight);
});

我遇到了完全相同的问题。我建议使用javascript而不是jQuery。

  const width = window.innerWidth;
  const height = window.innerHeight;