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

parentElement vs parentNode Tip #140

Open
Kindred-Spirit opened this issue Aug 24, 2020 · 1 comment
Open

parentElement vs parentNode Tip #140

Kindred-Spirit opened this issue Aug 24, 2020 · 1 comment

Comments

@Kindred-Spirit
Copy link

Hi and kudos for the great work!

I've noticed something odd on the page "parentElement vs parentNode".

Function distanceToTop in a Tip paragraph

const distanceToTop = (ele) => {
    let x = 0;
    while (ele = ele.parentElement) {
        x += ele.offsetTop;
    }
    return x;
};

actually isn't a correct way to calculate the distance to the top, since it uses offsetTop which basically works incrementally only when we're dealing with non-static elements or table layouts.
(also, even if it could work like that, the function reassigns ele to its parent before taking its own offset and doesn't take into account a border height)

A little example: https://codepen.io/Kindred-Spirit/pen/mdPRgJB

Maybe the tip needs to be updated on the limitations of this algorithm or deleted altogether since now the function is presented as a universal way to calculate the distance to the page top.

@phuocng
Copy link
Owner

phuocng commented Aug 25, 2020

Thanks for the feedback. I'll update the content.

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

No branches or pull requests

2 participants