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

ScrollTo is not working with body/windows container and target wrapped by a container with relative position #116

Open
Gildass opened this issue Feb 28, 2019 · 3 comments

Comments

@Gildass
Copy link

Gildass commented Feb 28, 2019

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x ] Bug report  
[ ] Feature request
[ ] Documentation issue or request

Current behavior

When I try to scroll to a target which is inside a container (which has a css position:relative, and when the scrollable container is the page's body (window), the scroll is made to the target, but relativly to the the container position.
For instance, if the container has a top position of 200px, after the scroll, the target will be positionned at 200px after the top of the screen.

Expected behavior

After the scroll, the target is positionned at the top of the page (+ the offset if an offset is configured)

Minimal reproduction of the problem with instructions

     <div>
        <h1 style="height:250px">Some content before the relative container (250px)</h1>
        <div style="position:relative; background: greenyellow">
            <div style="min-height: 200px">
                The relative container has a green background.
                <button [ngx-scroll-to]="'#scrollTarget'">Scroll to the target</button>
            </div>
            <div id="scrollTarget">The target of the scroll</div>
            <div style="min-height: 1000px; padding: 20px 0">Some content after the target</div>
        </div>
    </div>

What is the motivation / use case for changing the behavior?

Environment


Angular version: 7.0.0


Your version of `@nicky-lenaers/ngx-scroll-to`: 2.0.0


Browser:
Any browsers
 
@justindmyers
Copy link

Same here. Removing position: relative from all parent containers causes it to work, but it's completely unusable for any remotely complicated layout.

@bensgroi
Copy link

bensgroi commented Apr 7, 2019

I'm seeing this as well. Angular 6.1.10 and 2.0.0 of this package.

@grmpfhmbl
Copy link

The problem is very likely here

if (targetNode) {
to = isWindow(listenerTarget) ? targetNode.offsetTop : targetNode.getBoundingClientRect().top;
}

When using offsetTop you'll have to check if the offsetParent is actually the container you want to scroll. If there are elements with position:relative in between, this is very likely not the case. Here is a screenshot from Chrome Developer Tools.

Screenshot 2019-06-15 21 19 57

As you can see container is the document body and listenerTarget the window but offsetParent is some element in the DOM tree. The correct value to scroll to would be 425. For window the correct way to find the correct absolute position to scroll to is - according to MDN - to add window.scrollY + targetNode.getBoundingClientRect().top. You could probably also move up the DOM via offsetParent and add all offsetTopuntil you reach container but that would be a lot slower. Not sure if that has any advantages over adding scrollY.

This issue is likely also related to #104 and #111.

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

4 participants