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

feat: add lazy option to allow recalculating targets #388

Merged
merged 4 commits into from
Oct 28, 2020

Conversation

RealGoodProgrammer
Copy link
Contributor

@RealGoodProgrammer RealGoodProgrammer commented Oct 16, 2020

Hello!
I ran into a problem when I was trying to use this lib on a dynamically growing container. Figuratively speaking, at the start of the scroll, the height of the container was 500px, and at the end it could reach 5000px. Thus, the initially set position was strongly shifted down, but the scrollTo did not know about this and scrolled to the wrong place. This PR solves this problem.

@RealGoodProgrammer
Copy link
Contributor Author

Anybody is here?

@RealGoodProgrammer
Copy link
Contributor Author

Should I update documentation?

src/scrollTo.js Outdated
Comment on lines 109 to 118
if (x) {
targetX =
cumulativeOffsetElement.left - cumulativeOffsetContainer.left + offset
diffX = targetX - initialX
}
if (y) {
targetY =
cumulativeOffsetElement.top - cumulativeOffsetContainer.top + offset
diffY = targetY - initialY
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would refactor this since calculations are same as

initialY = scrollTop(container)
targetY =
cumulativeOffsetElement.top - cumulativeOffsetContainer.top + offset
initialX = scrollLeft(container)
targetX =
cumulativeOffsetElement.left - cumulativeOffsetContainer.left + offset
abort = false
diffY = targetY - initialY
diffX = targetX - initialX

Refactoring both places to use a function would likely be a cleaner approach - since if we ever need to change how the target is calculated, it needs to be done in a single spot.

Something like recalculateTargets, or something better perhaps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I do it or you will?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably won't be able to for at least a few weeks, so if you are able to do it, that would be perfect!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

README.md Outdated Show resolved Hide resolved
Copy link
Owner

@rigor789 rigor789 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added suggestions (I'm about to commit those directly - so no action required)

README.md Outdated Show resolved Hide resolved
src/scrollTo.js Outdated Show resolved Hide resolved
@rigor789 rigor789 changed the title feat: add dynamicPosition parameter (for dynamic container height) feat: add lazy option to allow recalculating targets Oct 28, 2020
@rigor789 rigor789 merged commit e3c70c5 into rigor789:master Oct 28, 2020
@rigor789
Copy link
Owner

🎉 This PR is included in version 2.20.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants