user4563161
user4563161

Reputation:

Javascript, Test if ScrollBy behaviour "smooth" is supported

I'm trying to test whether or not a browser supports the scrollBy behaviour smooth.

I have the following which works for the most part but some IOS Safari versions are reporting true when they don't actually support it. I'm assuming that they do this because they support Behaviour just not Smooth Behaviour. Is there a way to test specifically for smooth behaviour? Or perhaps I'm doing something wrong in my try that's giving the false positive?

Here is my current code:

function testSmoothScroll() {
    let support = false;
    try {
        wrapper.scrollIntoView({
            get behavior() {
                support = true;
            }
        });
    } catch (err) {
        // Error catching
    }
    return support;
}

Upvotes: 2

Views: 919

Answers (2)

Unmitigated
Unmitigated

Reputation: 89294

You can check if the scrollBehavior property is supported by checking the style of the HTML element.

const smoothScrollSupported = 'scrollBehavior' in document.documentElement.style;

Upvotes: 3

Ben West
Ben West

Reputation: 4596

In the polyfill for this feature it checks for the scroll-behaviour CSS property. Presumably that and JS behavior: smooth come together.

Upvotes: 0

Related Questions