Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

dom

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 were added to the element, or because new elements were added around this element, or because CSS rules have changed, or because the user has changed their browser's font size. There are a huge number of reasons that an element's size or position would change.

I could hook event handlers for many of those events, but I think there are cases that aren't covered. I had hoped to use MutationObservers to just watch the element's offsetWidth or clientWidth, but it looks like those only apply to DOM attributes, not Node properties (or maybe that's just what Webkit implements). So, to be truly bulletproof, it looks like I would need to run a polling loop anyway.

Is this correct? Have I missed something important? Is polling the only bulletproof way to detect when an element's size or position has changed?

Edit

To give a little more context, this isn't tied to any one particular use case. For me, it has come up time and time again. In my current situation, I was hoping to have a canvas that is set, via CSS, to be 100% width and height of its parent. I want a script to adjust the canvas's width and height properties when its offsetWidth or clientWidth changed (this is for WebGL, where the width and height properties affect the framebuffer resolution). In another project, I wanted to have an element expand in height to fill whatever remaining space was in the user's browser window. I have come across this situation time and time again and, inevitably, the solution seems to be:

  1. Listen to window.resize
  2. Hook into any code that you write that might change the DOM (or maybe use Mutation Events or Mutation Observers)
  3. Have a periodic polling operation to clean up any missed cases

(most people stop at 1, and maybe do a little of 2)

I just wanted to make sure I wasn't missing something vital.

Incidentally, in my WebGL situation, listening to window.resize is sufficient (and is somewhat preferable, because then the canvas dimensions and rendering resolution change at the same time).

like image 461
Daniel Yankowsky Avatar asked Dec 02 '12 05:12

Daniel Yankowsky


People also ask

What is the best way to locate an element in the DOM?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable.

How do I identify a DOM element?

The easiest way to find an HTML element in the DOM, is by using the element id.

How do you check if an element is added to DOM?

To check if an element is connected or attached to the DOM or the document object ( or otherwise called the context ), you can use the isConnected property in the element's object in JavaScript. The isConnected element property returns boolean true if it connected to the DOM ( document object) and false if not.

Which is method to search a node from the DOM?

The Selectors API provides methods that make it quick and easy to retrieve Element nodes from the DOM by matching against a set of selectors. This is much faster than past techniques, wherein it was necessary to, for example, use a loop in JavaScript code to locate the specific items you needed to find.


1 Answers

You might could do it with CSS animations. Check out http://developer.streak.com/2012/11/how-to-detect-dom-changes-in-css.html and similar. I suppose it won't handle all your requirements though, now that I read through it some more.

like image 79
Pre101 Avatar answered Sep 21 '22 11:09

Pre101