简体   繁体   English

获取计算高度 - Javascript - 不是 jQuery

[英]Get Computed Height - Javascript - Not jQuery

I have two divs side by side set to height auto.我有两个并排设置为高度自动的 div。 I want them to have equal height, so i combined them as members of an array.我希望它们具有相同的高度,因此我将它们组合为数组的成员。

I recurse through the array and set the not-tallest ones to the height of the tallest.我递归遍历数组并将非最高的设置为最高的高度。 Problem is everything i have tried to get the COMPUTED height has resulted in the incorrect value.问题是我试图获得计算高度的一切都导致了不正确的值。

I have tried the following:我尝试了以下方法:

(els[x].currentStyle) ? h=els[x].currentStyle.height : h=window.getComputedStyle(els[x],null).height;

h =  el.clientHeight || el.offsetHeight || el.scrollHeight;

Both of these are yielding 640 px'ish while the computed is 751.8 in my particular showing.这两个都产生了 640 px'ish,而在我的特定展示中计算结果为 751.8。

Is there possbily a constant I can use to get the correct height.是否有一个常数可以用来获得正确的高度。 Like maybe the number im getting would be on a standard size screen (like 960 pixels high or such) then multiple that by the window size?就像我得到的数字可能会在标准尺寸的屏幕上(比如 960 像素高或这样)然后乘以窗口大小?

I have had a lot of good use of this little function I came up with我已经很好地利用了我想出的这个小功能

function getH(id)
{
    return document.getElementById(id).offsetHeight;
}
// I have a styles.js file where this function feels right at home. Access this function from anywhere.

This will return the height of any given elements given to the function (by it's ID).这将返回给定函数的任何给定元素的高度(通过它的 ID)。 So now we'r 1/3 of the way.所以现在我们走了 1/3 的路。

Then use the Math.max() function to get the height of the largest element.然后使用 Math.max() 函数获取最大元素的高度。

var maxH = Math.max(getH("ID1"),getH("ID2")); 

This is 2/3 of the way, YAY - Now set the elements height to be the same.这是方式的 2/3,是的 - 现在将元素高度设置为相同。

var x = document.getElementById("ID1");
var y = document.getElementById("ID2");

x.style.height = maxH +"px";
y.style.height = maxH +"px";  //Remember the +"px" has to be added as a string, thats the reason for the "".

DONE!!完成!! - Now put it all together - 现在把它们放在一起

I would imagine something like this我会想象这样的事情

function setElementHeight()
{
    var maxH = Math.max(getH("ID1"),getH("ID2"));
    var x = document.getElementById("ID1");
    var y = doc...("ID2");
    x.style.height = maxH +"px";
    y.style.height = maxH +"px";
}
// Don't forget to include the first function in you'r .js file

This WILL set both ID's to the same height and the height WILL be equal to the highest.这会将两个 ID 设置为相同的高度,并且高度将等于最高。 That is what you want, isn't it?这就是你想要的,不是吗?

--EDIT-- --编辑--

I would throw away the array if I were you.如果我是你,我会扔掉数组。 Just have the 2 DIV's each with a unique ID and make them equally tall based upon that.只需拥有 2 个 DIV,每个 DIV 都有一个唯一的 ID,并以此为基础使它们同样高。

  • Molle莫尔

If you have the DOM document, you can try the below code:如果你有 DOM 文档,你可以试试下面的代码:

let divElement = document.getElementById('divId');
let height = document.defaultView.getComputedStyle(divElement).height;

'height' will have the exact height of the element with 'divId' once it is computed. 'height' 将在计算后具有带有 'divId' 的元素的精确高度。

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

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