简体   繁体   中英

how to change the offsetHeight of a element using javascript?

Hello i'm trying to change the offsetHeight of an element. i used the following

document.getElementById('id').style.offsetHeight = 0;

but i saw no visible change. Can anyone help me please?

The offsetHeight property indicates the height of the visible area for an element. It's a shorthand that contains the sum of dimensions from padding, scrollbars and borders.

However, it can't be used to change the actual size and as noted in comments, offsetHeight is a property of an element, not style.

To modify the actual size use height , padding or border .

您应该将style.height设置为以px结尾的字符串。

You should set style.height and remember to add the unit at the end like 'px' , in the case you get it from offsetHeight for example (well you know what unit you need). It's style and you have all the different units ('px','%','em', 'vh', ...etc). Here is an example:

myHeightInPx = 200;
DomElement.style.height = myHeightInPx + 'px';

Also to note is that offsetHeight return the height as a number, an integer. The unit is px. And if you get a value using it. you need always to add the unit 'px' when setting style.height, just like in above and the bellow example:

DomElement.style.height = AnotherDOMelment.offsetHeight() + 'px';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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