-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
enhancement: #500 Flag for the ScrollSpy component that ensures the last active element remains visible + animationDuration option #504
base: main
Are you sure you want to change the base?
Conversation
0b5149b
to
2e4ec57
Compare
Nice, your video explaines it very clearly 👍 i will review the code tomorrow. |
src/scrollspy.ts
Outdated
|
||
function getDistanceToViewport(element) { | ||
const rect = element.getBoundingClientRect(); | ||
const distance = rect.top; | ||
return distance; | ||
} | ||
|
||
function smoothScrollIntoView(element, duration = 300) { | ||
const targetPosition = element.getBoundingClientRect().top + (window.scrollY || window.pageYOffset); | ||
const startPosition = (window.scrollY || window.pageYOffset); | ||
const distance = targetPosition - startPosition; | ||
const startTime = performance.now(); | ||
|
||
function scrollStep(currentTime) { | ||
const elapsed = currentTime - startTime; | ||
const progress = Math.min(elapsed / duration, 1); | ||
const scrollY = startPosition + distance * progress; | ||
|
||
if (progress < 1) { | ||
window.scrollTo(0, scrollY); | ||
requestAnimationFrame(scrollStep); | ||
} else { | ||
window.scrollTo(0, targetPosition); | ||
} | ||
} | ||
requestAnimationFrame(scrollStep); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this two functions should be inside of the class. The rest looks nice!
Can you place them as private functions, then i will merge. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔️
fixes #500
Proposed changes
Screenshots (if appropriate) or codepen:
Demo Video
Types of changes
Checklist: