Is there any non-polling way to detect when a DOM element's size or position have changed?

后端 未结 3 676
逝去的感伤
逝去的感伤 2021-02-01 17:01

For a long time, I\'ve been looking for a way to detect when a DOM element\'s size or position has changed. It could be because the window resized, or because new child elements

3条回答
  •  Happy的楠姐
    2021-02-01 17:18

    As of 2019, you have access to:

    • ResizeObserver to fire callback on size change. Be careful here, it's not supported everywhere yet.
    • MutationObserver that could be used for position, but I guess that's a bit overkill. Well supported though.
    • IntersectionObserver as a bonus, that allows you to check for intersection with ancestors. Extremly useful to optimize stuff, especially in WebGL. This is pretty well supported.

提交回复
热议问题