获取计算高度-Javascript-非jQuery [英] Get Computed Height - Javascript - Not jQuery

查看:96
本文介绍了获取计算高度-Javascript-非jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将两个div并排设置为height auto.我希望它们的高度相等,所以我将它们组合为数组的成员.

我递归遍历数组,并将不高的数组设置为最高的高度.问题是我尝试获得COMPUTED高度的所有操作均导致错误的值.

我尝试了以下操作:

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

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

在我的特定显示中,这两种方法都产生640像素点的亮度,而计算得出的值为751.8.

可能存在一个常数,可以用来获取正确的高度.好像即时获取的数字可能是在标准尺寸的屏幕上(例如960像素高等),然后乘以窗口大小吗?

解决方案

我很好地利用了我想到的这个小功能

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.

这将返回给定给该函数的任何给定元素的高度(通过其ID).所以现在我们走了1/3.

然后使用Math.max()函数获取最大元素的高度.

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

这是方法的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 "".

完成!! -现在放在一起

我会想像这样的东西

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

这将把两个ID设置为相同的高度,并且高度将等于最高.那就是你想要的,不是吗?

-编辑-

如果我是你,我会扔掉阵列.只需让两个DIV都具有唯一的ID,并以此为基础使其同等高.

  • 莫里

I have two divs side by side set to height auto. 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.

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?

解决方案

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). So now we'r 1/3 of the way.

Then use the Math.max() function to get the height of the largest element.

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.

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. 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.

  • Molle

这篇关于获取计算高度-Javascript-非jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆