当前位置:网站首页>JS get the height and width corresponding to the box model (window.getcomputedstyle, dom.getboundingclientrect)
JS get the height and width corresponding to the box model (window.getcomputedstyle, dom.getboundingclientrect)
2022-06-25 14:27:00 【Fat goose 68】
List of articles
Article reference
obtain dom General method of node height and width
dom.style.width/height ( Only applicable to get the width and height of inline elements )
dom.currentStyle.width/height ( Get the width and height after rendering , But only IE Support )
window.getComputedStyle(dom).width/height ( And 2 Principle similarity , But compatibility , Commonality will be better )
dom.getBoundingClientRect().widht/height ( Calculate the absolute position of the element , Get four elements left,top,width,height)
Case description
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style> #xiding-header{
border: 1px solid red; } .box {
height: 500px; } .fixed-top {
position: fixed; top: 0; left: 0; right: 0; } </style>
<body>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div id="xiding-header" >
dasfdsfadsafdsaf
</div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</body>
<script> let targetDom = document.querySelector('#xiding-header') setTimeout(function () {
// Because the target element has no inline style , So it's empty console.log(targetDom.style.height) // If it is to use chrome open , You can get the height of the target control console.log(getComputedStyle(targetDom).height) }, 1000) document.addEventListener("scroll", function () {
console.log(document.documentElement.scrollTop) let boundingObj = targetDom.getBoundingClientRect() console.log(' Height of control : ' + (boundingObj.bottom - boundingObj.top)) console.log(' Width of the control : ' + (boundingObj.right - boundingObj.left)) if (boundingObj.top <= 0) {
// targetDom.classList This method is compatible , Apply to chrome browser targetDom.classList.add('fixed-top') // Conventional DOM Add attribute , standard API // targetDom.setAttribute("class", 'fixed-top' ); } }) </script>
</html>
Running results :

getBoundingClientRect() obtain DOM The height and width of the approach ?

let targetDom = document.querySelector('#xiding-header')
let boundingObj = targetDom.getBoundingClientRect()
console.log(' Height of control : ' + (boundingObj.bottom - boundingObj.top))
console.log(' Width of the control : ' + (boundingObj.right - boundingObj.left))
1. Height of control
boundingObj.bottom - boundingObj.top
2. Width of control
boundingObj.right - boundingObj.left
边栏推荐
- 深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)
- shell 运算符
- Getting started with shell variables
- Golang project dependency management tool go vendor, go Mod
- JVM uses tools to analyze classic cases of OOM
- 【深度学习】多标签学习
- Is it unsafe to make new debts
- JS determines whether two values are equal, and compares any two values, including array objects
- 测一测你的挣钱能力有多强?未来的你将从事什么职业?
- What are the red lines of open source that should not be trodden on?
猜你喜欢

Experts' suggestions | 8 measures to accelerate your innovative career planning and growth

Variables, scopes, and variable promotion

Pourquoi les programmeurs devraient - ils être plus doux?

Getting started with numpy Library

Renix Perf: IP网络性能测试工具及测试用例参数详解

程序员为什么要软一点?

How does hash eagle, the founder of equity NFT, redefine NFT and use equity to enable long-term value?

It's not easy to understand the data consistency of the microservice architecture for the first time after six years as a programmer

To make pytorch faster, you need to master these 17 methods

JS floating point multiplication and division method can not accurately calculate the problem
随机推荐
QT memory mapping
Les neuf caractéristiques de la parole et les neuf temps en anglais
shell 数组
Supplementary inheritance and strict mode
Let and const commands
New good friend Pinia, leading the new era of state management
分享自己平時使用的socket多客戶端通信的代碼技術點和軟件使用
Page 112 machine learning - review of fundamentals of mathematics pptx
Does stream even have application advanced learning? As a programmer, you know what
重磅!国产 IDE 发布,由阿里研发,完全开源!(高性能+高定制性)
Test your earning power? What will you do in the future?
Is it unsafe to make new debts
算力&NFT交易平台F3.xyz旗下独家权益NFT项目Hash Eagle将盛大发行
Nine parts of speech and nine tenses in English
k线图24种经典图解(影线篇)
哈希表、哈希冲突
sigmoid函数sigmoid求导
电脑必须打开的设置
Preventing overfitting of deep neural networks (mysteries of neural networks Part II)
买基金在哪里开户安全?求指导