Some attributes about elements In the daily development of the front-end, we often inevitably need to obtain or monitor the properties of some pages, so we need to often understand the meaning of some properties in order to better use these properties. In particular, these:
Definition of Attributes About size-related attribute definitions: offsetHeight: Element.offsetHeight is a read-only property that returns the value of the height px of the element. It is an integer value and there is no decimal.
clientHeight: Element.clientHeight is a read-only property that returns the height px value of the element. It is an integer value and there is no decimal.
scrollHeight: is a read-only property. It returns the height px value of the element. It is an integer value without decimals.
window.innerHeight: (browser window height, excluding toolbars, menus, etc., only the height of the visible area DOM) About offset: offsetTop: a read-only property that returns the top distance of an element from the inner edge of the nearest relatively positioned parent element. In actual use, there may be compatibility issues due to inconsistent relatively positioned parent elements caused by different styles.
window.scrollY, alias: window.pageYOffset, the distance the root node has scrolled vertically Relevant data required for development Get the height of the visible area of the entire page: [No need for the height outside the visible area] const height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; Get the height of the entire page: [including outside the visible area] const height = document.documentElement.offsetHeight || document.body.offsetHeight; Get the vertical scroll height of the entire page: const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; Get the distance of the element relative to the top of the root node: // For elements positioned relative to the root node const top = Element.offsetTop; // For elements that are not positioned relative to the root node, you need to loop to getElementTop(element) { let actualTop = element.offsetTop let current = element.offsetParent while (current !== null) { actualTop += current.offsetTop current = current.offsetParent } return actualTop } // Another method is scroll distance + distance from the top margin of the viewport const top = Element.getBoundingClientRect().top + window.scrollY; Get the element's top distance relative to the visible area: const top = Element.getBoundingClientRect().top; Set the vertical scroll position of the entire page: const isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); if (isCSS1Compat) { document.documentElement.scrollTop = 100; } else { document.body.scrollTop = 100; } This is the end of this article about the detailed explanation of HTML elements' height, offsetHeight, clientHeight, scrollTop, etc. For more relevant height, offsetHeight, clientHeight, scrollTop content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: Interview questions: The difference between the Holy Grail layout and the double-wing layout
>>: Ideas for creating wave effects with CSS
Rational ClearCase is a software configuration ma...
What is load balancing? When a domain name points...
Skip the Docker installation steps 1. Pull the po...
Table of contents introduce Link start Continue t...
#String concatenation concat(s1,s2); concatenate ...
1. Multi-header table code Copy code The code is a...
The test is passed in the nodejs environment. The...
JSON data is displayed and formatted on the HTML ...
Preface This article mainly introduces the releva...
It is mainly the configuration jump of the if jud...
Preface: In some previous articles, we often see ...
Table of contents 1. Create a table 1.1. Basic sy...
Table of contents Layout part: <div id="a...
Cerebro is an evolution of the Elasticsearch Kopf...
Table of contents Preface 1. Background 2. Simula...