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

Error description with element hidden check in "Element size and scrolling" #1955

Open
chanjsq opened this issue Jun 10, 2020 · 4 comments
Open

Comments

@chanjsq
Copy link
Contributor

@chanjsq chanjsq commented Jun 10, 2020

The tutorial said:

Geometry properties are calculated only for displayed elements.

If an element (or any of its ancestors) has display:none or is not in the document, then all geometry properties are zero (or null for offsetParent).

For example, offsetParent is null, and offsetWidth, offsetHeight are 0 when we created an element, but haven’t inserted it into the document yet, or it (or it’s ancestor) has display:none.

We can use this to check if an element is hidden, like this:

function isHidden(elem) {
  return !elem.offsetWidth && !elem.offsetHeight;
}

But, what if the hidden element is hidden by setting the width to 0, not the height? 😀

@iliakan
Copy link
Member

@iliakan iliakan commented Jun 10, 2020

@chanjsq try it?

@chanjsq
Copy link
Contributor Author

@chanjsq chanjsq commented Jun 10, 2020

image

Look above, my test code.

@iliakan
Copy link
Member

@iliakan iliakan commented Jun 10, 2020

Guess, you're right, we can make || instead of && there.

@chanjsq
Copy link
Contributor Author

@chanjsq chanjsq commented Jun 10, 2020

What about this?

function isHidden(elem) {
  return !elem.offsetWidth && (!elem.offsetHeight || !elem.offsetWidth);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.