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

Logic broken for tall elements and partial = true #49

Open
waveywhite opened this issue Mar 27, 2018 · 1 comment
Open

Logic broken for tall elements and partial = true #49

waveywhite opened this issue Mar 27, 2018 · 1 comment

Comments

@waveywhite
Copy link

Hi,

Where getBoundingClientRect is available, the current algorithm checks to see whether the top OR the bottom are visible to consider the element partially visible. However, for tall elements the top of the element can be above the viewport and the bottom of the element below. This causes the function to return "false" in spite of the element being visible.

The partial case can't be driven from top OR bottom being visible. Instead you need to check whether the top is above the bottom of the viewport AND the bottom is below the top of the viewport.

The same logic also needs to apply to left & right.

The legacy browser code is OK.

        if (typeof t.getBoundingClientRect === 'function'){

            // Use this native browser method, if available.
            var rec = t.getBoundingClientRect(),
                tViz = rec.top    >= 0 && rec.top    <  vpHeight,
                bViz = rec.bottom >  0 && rec.bottom <= vpHeight,
                lViz = rec.left   >= 0 && rec.left   <  vpWidth,
                rViz = rec.right  >  0 && rec.right  <= vpWidth,
                tBounds = rec.top    <  vpHeight,
                bBounds= rec.bottom >  0,
                lBounds = rec.left   <  vpWidth,
                rBounds = rec.right  >  0,
                vVisible   = partial ? tBounds && bBounds : tViz && bViz,
                hVisible   = partial ? lBounds && rBounds : lViz && rViz;

            if(direction === 'both')
                return clientSize && vVisible && hVisible;
            else if(direction === 'vertical')
                return clientSize && vVisible;
            else if(direction === 'horizontal')
                return clientSize && hVisible;
        }
@ghost
Copy link

ghost commented Mar 29, 2018

Just had this problem today. You are doing god's work.

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

1 participant