简体   繁体   English

使用 window 调整大小动态更改 div 高度

[英]Dynamically change div height with window resize

I wanted to change height of a div when window is resized.我想在调整 window 大小时更改 div 的高度。 I know it can be done with css using height:100%;我知道可以使用 css 来完成height:100%; but that doesn't work on what i need.但这对我需要的不起作用。 I wanted to make this happen in pure javascript without JQuery or any other framework.我想在没有 JQuery 或任何其他框架的情况下在纯 javascript 中实现这一点。

Here is what i have done:这是我所做的:

<div id="left">
<div id="inner">

</div>
</div>

CSS CSS

#left{

margin-top:40px;
width:200px;
height:576px;
background:white;

}
#inner{

height:100%;
overflow-y:scroll;
}

JAVASCRIPT JAVASCRIPT

window.onload=
window.onresize=function(){
var left=document.getElementById('left');
var window_height = window.innerheight;
if ( window_height > 600px ) { left.style.height = document.body.offsetHeight
+"px";    } 
else { } 
} 

The div with id left has to have the same margin. id 为 left 的 div 必须具有相同的边距。 I just wanted to change the height of div to match the inner window height (in px no %).我只是想更改 div 的高度以匹配内部 window 高度(以像素为单位)。

Thank You.谢谢你。

window.innerheight should be window.innerHeight (capital H ). window.innerheight应该是window.innerHeight (大写H )。 Note that IE doesn't support it prior to IE9. 请注意,IE在IE9之前不支持它。

Also note that you're comparing an element with a number here: 另请注意,您在此处将元素与数字进行比较:

if ( left < window_height )
//   ^--- element

You probably wanted to get the height from left instead, as that condition will never be true. 您可能希望从left获得高度,因为该条件永远不会成立。

please see jsfiddle jsfiddle 请参阅jsfiddle jsfiddle

try to use console.log to know what kind of value or object you dealing with 尝试使用console.log来了解您处理的是什么类型的值或对象

window.onload = window.onresize = function () {
    var left = document.getElementById('left');
    var window_height = window.innerHeight;
    console.log(left.offsetHeight);
    console.log(window_height);
    if (left.offsetHeight < window_height) {
        left.style.height = window_height + "px";

    } else {}
}

set 100% first to know the exact height number, then assign back to height. 首先设置100%以确定精确的高度数,然后再分配回高度。

updated code updated jsfiddle 更新的代码更新了jsfiddle

window.onload = window.onresize = function () {
    var left = document.getElementById('left');

    console.log(left.offsetHeight);
    var window_height = window.innerHeight;

    if (left.offsetHeight < window_height) {
        left.style.height = '100%';    
        left_height=left.offsetHeight;
        console.log(left_height);
        left.style.height = left_height + "px";

    } else {}
};

For those looking for old-school solution that uses debounce based on timeouts, here is what you can do:对于那些寻找基于超时使用去抖动的老式解决方案的人,您可以执行以下操作:

function debounce(func, wait = 20, immediate = true) {
  let timeout;
  return function() {
    const context = this,
      args = arguments;
    const later = function() {
      timeout = null;
      if (!immediate) { func.apply(context, args); }
    };
    const callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) { func.apply(context, args); }
  };
}

function updateHeight(div, height) {
  div.style.height = `${height}px`;
}

window.addEventListener('resize', debounce(() => {
  const div = document.querySelector('.selector');
  updateHeight(div, 50);
}));

Debounce will throttle updates down (by default update will fire every 20ms during resizing). Debounce 将限制更新(默认情况下更新将在调整大小时每 20 毫秒触发一次)。 Note you need to update updateHeight arguments and .selector to match your div.请注意,您需要更新 updateHeight arguments 和.selector以匹配您的 div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM