Skip to content

Commit

Permalink
Merge pull request #55 from robere2/scroll_hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
robere2 committed Jul 20, 2023
2 parents 327909e + c5e0054 commit 58291ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
23 changes: 16 additions & 7 deletions client/components/RepeatingList/RepeatingList.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class RepeatingList extends HTMLElement {
* @type {number}
*/
#lastKnownMouseX = -1;
/**
* scrollLeft cannot take fractional values in some browsers, so we store the next scrollLeft value here and
* attempt to add it's non-fractional value to the actual scrollLeft value every frame. E.g., if this value
* is 4.25, then on the next frame, 4 will be added to scrollLeft and this will be set back to 0.25.
* @type {number}
*/
#nextScrollLeft = 0;

constructor() {
super();
Expand All @@ -45,10 +52,6 @@ class RepeatingList extends HTMLElement {
requestAnimationFrame(this.render.bind(this));
}

attributeChangedCallback() {
this.setup();
}

/**
* Get the speed attribute, or the default if one wasn't provided.
* @returns {number} The parsed float value of the data-speed attribute, or the default of 1 if one wasn't provided.
Expand Down Expand Up @@ -120,7 +123,7 @@ class RepeatingList extends HTMLElement {
!this.matches(":active") &&
this.#lastRenderTimestamp !== null
) {
this.scrollLeft += normalScrollSpeed;
this.#nextScrollLeft += normalScrollSpeed;
} else {
// Scroll manually if the user is hovering
const leftPos = this.getBoundingClientRect().left;
Expand Down Expand Up @@ -148,12 +151,18 @@ class RepeatingList extends HTMLElement {
)) *
3;
if (this.#lastKnownMouseX < centerPos) {
this.scrollLeft -= adjustedScrollSpeed;
this.#nextScrollLeft -= adjustedScrollSpeed;
} else {
this.scrollLeft += adjustedScrollSpeed;
this.#nextScrollLeft += adjustedScrollSpeed;
}
}

// Once we've reached at least one full pixel, scroll by that pixel(s) and then remove it from the next scroll.
if (Math.abs(this.#nextScrollLeft) >= 1) {
this.scrollLeft += Math.trunc(this.#nextScrollLeft);
this.#nextScrollLeft = this.#nextScrollLeft % 1;
}

// If we've made a full loop by reaching the second instance of the first element,
// go back to the front of the list.
if (this.scrollLeft >= this.#firstElementInstances[1]?.offsetLeft) {
Expand Down
12 changes: 6 additions & 6 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58291ea

Please sign in to comment.