Skip to content

Permanent jitter in browser scroll #309

Open

Description

@darvi-sh

I have tested this code:

<a href="https://a.link" title="test" aria-label="something">A link</a>

Since the result is only 1 row, total page height is very short.

After pressing the "Run HTML_CodeSnipper" button, the auto scroll functionality reaches the footer and starts to constantly move the page up and down by a pixel or two, but permanently.

The way around it is to refresh the page, after pressing the "Run HTML_CodeSnipper" button, forcefully keep scrolling up and stop the auto scroll from reaching bottom of the page until it times out (probably 1-2 seconds in total).

Please disable the auto scroll, especially for people like me that want no animation. (hint: prefers-reduced-motion)

Activity

  1. speplinski commented on Mar 25, 2023

    @speplinski

    It seems that the problem might be related to the improper clearing of the interval, and this issue appears, for example, in the Chrome browser. It would be beneficial to add extra conditions to check if the scrolling has reached its target or surpassed the boundaries. Here is the improved version of the code:

    if (currScrollY === targetScrollY || (sign === 1 && currScrollY >= targetScrollY) || (sign === -1 && currScrollY <= targetScrollY)) {
        clearInterval(interval);
    }
    
  2. speplinski commented on Mar 25, 2023

    @speplinski

    Alternatively, you can also simplify the code using the smoothscroll-polyfill along with the scrollIntoView() function to achieve a smoother scrolling experience.

    Include the script in the <head> section of your HTML file:

    <script src="https://unpkg.com/smoothscroll-polyfill@0.4.4/dist/smoothscroll.min.js"></script>
    

    Then activate the polyfill and change the scrollToElement function:

    smoothscroll.polyfill();
    
    function scrollToElement(element) {
        element.scrollIntoView({
            behavior: 'smooth',
            block: 'start'
        });
    }
    

    This version of the code is easier to read and more efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions