Skip to content

Commit

Permalink
Fix interference of scroll command and CSS (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbendebiene committed Dec 29, 2021
1 parent 438c8e2 commit 1e9b197
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/core/bundle/content.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function getClosestElement (startNode, testFunction) {
* Smooth scrolling to a given y position
* duration: scroll duration in milliseconds; default is 0 (no transition)
* element: the html element that should be scrolled; default is the main scrolling element
* Note: The "instant" property is not part of the w3c spec any more (https://github.com/w3c/csswg-drafts/issues/3497)
**/
function scrollToY (y, duration = 0, element = document.scrollingElement) {
// clamp y position between 0 and max scroll position
Expand All @@ -127,8 +128,15 @@ function scrollToY (y, duration = 0, element = document.scrollingElement) {
if (oldTimestamp !== null) {
// if duration is 0 scrollCount will be Infinity
scrollCount += Math.PI * (newTimestamp - oldTimestamp) / duration;
if (scrollCount >= Math.PI) return element.scrollTop = y;
element.scrollTop = cosParameter + y + cosParameter * Math.cos(scrollCount);
if (scrollCount >= Math.PI) return element.scrollTo({
top: y,
behavior: 'instant'
});

element.scrollTo({
top: cosParameter + y + cosParameter * Math.cos(scrollCount),
behavior: 'instant'
});
}
oldTimestamp = newTimestamp;
window.requestAnimationFrame(step);
Expand Down
12 changes: 10 additions & 2 deletions src/core/utils/commons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export function getClosestElement (startNode, testFunction) {
* Smooth scrolling to a given y position
* duration: scroll duration in milliseconds; default is 0 (no transition)
* element: the html element that should be scrolled; default is the main scrolling element
* Note: The "instant" property is not part of the w3c spec any more (https://github.com/w3c/csswg-drafts/issues/3497)
**/
export function scrollToY (y, duration = 0, element = document.scrollingElement) {
// clamp y position between 0 and max scroll position
Expand All @@ -307,8 +308,15 @@ export function scrollToY (y, duration = 0, element = document.scrollingElement)
if (oldTimestamp !== null) {
// if duration is 0 scrollCount will be Infinity
scrollCount += Math.PI * (newTimestamp - oldTimestamp) / duration;
if (scrollCount >= Math.PI) return element.scrollTop = y;
element.scrollTop = cosParameter + y + cosParameter * Math.cos(scrollCount);
if (scrollCount >= Math.PI) return element.scrollTo({
top: y,
behavior: 'instant'
});

element.scrollTo({
top: cosParameter + y + cosParameter * Math.cos(scrollCount),
behavior: 'instant'
});
}
oldTimestamp = newTimestamp;
window.requestAnimationFrame(step);
Expand Down

0 comments on commit 1e9b197

Please sign in to comment.