You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ ] 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
The text was updated successfully, but these errors were encountered:
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.
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.
I'm submitting a...
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
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: