当前位置:网站首页>Get the exact offset of the element
Get the exact offset of the element
2022-07-07 22:21:00 【Belden wind】
adopt getBoundingClientRect obtain ( recommend )
const getOffset = (element: HTMLElement) => {
const elemRect = element.getBoundingClientRect();
const offsetY = elemRect.top + window.scrollY;
const offsetX = elemRect.left + window.scrollX;
return {
left: offsetX,
top: offsetY,
};
};
adopt offsetParent obtain ,( If the element has translate(xx,xx), It will fail
const getOffset = (
element: HTMLElement,
type: 'offsetTop' | 'offsetLeft'
): number => {
let actualOffset = element[type];
let current = element.offsetParent as HTMLElement;
while (current !== null) {
actualOffset += current[type];
current = current.offsetParent as HTMLElement;
}
return actualOffset;
};
边栏推荐
- Embedded development: how to choose the right RTOS for the project?
- Song list 11111
- Implementation method of data platform landing
- Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
- Tsconfig of typescript TS basics JSON configuration options
- ByteDance Android interview, summary of knowledge points + analysis of interview questions
- Reinforcement learning - learning notes 8 | Q-learning
- Reptile combat (VII): pictures of the king of reptiles' heroes
- OpeGL personal notes - lights
- Use blocconsumer to build responsive components and monitor status at the same time
猜你喜欢
Overseas agent recommendation
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
Vs custom template - take the custom class template as an example
Jerry's manual matching method [chapter]
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Blender exchange group, welcome to the water group ~
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
【JDBC Part 1】概述、获取连接、CRUD
Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
随机推荐
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
Implementation method of data platform landing
The maximum number of meetings you can attend [greedy + priority queue]
Build your own website (18)
DBSync新增对MongoDB、ES的支持
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
Px4 autonomous flight
【JDBC Part 1】概述、获取连接、CRUD
TCP/IP 协议栈
How to integrate Google APIs with Google's application system (1) -introduction to Google APIs
Dbsync adds support for mongodb and ES
OpenGL job coordinate system
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
建立自己的网站(18)
Matplotlib drawing interface settings
Ternary expressions, generative expressions, anonymous functions
How polardb-x does distributed database hotspot analysis
L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?
使用 CustomPaint 绘制基本图形
Jerry's test box configuration channel [chapter]