Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Measuring element width in a layout that uses flexbox layout #123

Open
mindplay-dk opened this issue Apr 14, 2015 · 4 comments
Open

Measuring element width in a layout that uses flexbox layout #123

mindplay-dk opened this issue Apr 14, 2015 · 4 comments

Comments

@mindplay-dk
Copy link

Working on a site that uses flexbox CSS layout, I ran into an issue where only the lowest-resolution image would get loaded, even after resizing.

I narrowed it down to image.parentNode.clientWidth always returning zero in the determineAppropriateResolution function.

Adding a second fallback to offsetWidth seems to fix the issue:

Imager.prototype.determineAppropriateResolution = function (image) {
  return Imager.getClosestValue(image.getAttribute('data-width') || image.parentNode.clientWidth || image.parentNode.offsetWidth, this.availableWidths);
};

My problem isn't completely fixed- it still doesn't initially load the correct size image, but this seems to be an important step on the way.

@mindplay-dk
Copy link
Author

Okay, it seems the parent node was an <a> tag which did not have display:block, and therefore did not have any measurable size - having fixed that, it now works without the added image.parentNode.offsetWidth fallback.

I wonder if the script ought to handle this case, and what would be the appropriate way to handle it? Throw an exception? Log the error to the console?

This sort of issue going to be common for sure, and should be handled somehow, I think?

@thom4parisot
Copy link
Contributor

Throwing an exception would be aggressive but failing silently is neither good.

So I would suggest to opt in for an explicitly written exception message, with an additional console.error (if available) message to help debugging the troublesome DOM element.

Otherwise, ideally another solution would be to walk up the DOM tree until we find an element with a width.

@mindplay-dk
Copy link
Author

ideally another solution would be to walk up the DOM tree until we find an element with a width

Won't that lead to even-harder to debug cases? where you can't figure out where the width is coming from or why. I like simple, predictable, consistent behavior - walking up the tree means any of a number of elements could end up determining the width. Using the immediate parent, in my opinion, is cleaner and more predictable.

@mindplay-dk
Copy link
Author

Maybe close this issue and open a new issue for improved error-handling?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants