Modernizr: Detect support for scrollIntoView(scrollIntoViewOptions)

Created on 31 Mar 2015  路  6Comments  路  Source: Modernizr/Modernizr

no idea _how_, but it would be useful to just use element.scrollIntoView({behavior: "smooth"}) if available and else some polyfill for smooth scrolling via JS.

detect needed

Most helpful comment

I just noticed a Stack Overflow post that suggests that 'scrollBehavior' in document.documentElement.style is a sufficient test. The results I'm seeing for that do match up with the notes about who does/doesn't support the behavior option on https://caniuse.com/#search=scrollIntoView, but it's not clear to me if there's a direct connection (or really any connection) between a browser supporting the style property and supporting that option for the scrollIntoView call.

Edit: oh, and even though it's already three years old, I do have to give a +1 to @andyearnshaw's approach above, that is a clever one.

All 6 comments

I was just passing by and thought it might be interesting to attempt this. Something like the following would detect that _scrollIntoViewOptions_ is supported, but doesn't confirm that smooth scrolling is supported specifically:

(function test() {
    var res = false,
        a = document.createElement('a'),
        csy = window.pageYOffset,
        csx = window.pageXOffset;

    a.style.cssText = 'position: absolute; top: 0px; width: 1px; height: ' + (window.innerHeight + 1) + 'px;';

    // Test
    document.body.appendChild(a);
    a.scrollIntoView({ block: 'end' });
    res = a.getBoundingClientRect().top === -1;
    document.body.removeChild(a);

    // Revert and return
    window.scrollTo(csx, csy);
    return res;
})();

I haven't thoroughly tested it beyond running it in the console in a few pages in Firefox (true) and Chrome (false), though. It seems unlikely that a browser would implement _scrollIntoViewOptions_ without bothering with smooth scrolling at the same time, but you never know. A more robust test might follow up (if this test is true) with a check that the scroll position hasn't changed when behaviour: 'smooth' is added to the view options.

ah, very cool!

Any update on this one?

+1

PullRequests are always welcome ;-) Glad to review them and help them get in shape.

I just noticed a Stack Overflow post that suggests that 'scrollBehavior' in document.documentElement.style is a sufficient test. The results I'm seeing for that do match up with the notes about who does/doesn't support the behavior option on https://caniuse.com/#search=scrollIntoView, but it's not clear to me if there's a direct connection (or really any connection) between a browser supporting the style property and supporting that option for the scrollIntoView call.

Edit: oh, and even though it's already three years old, I do have to give a +1 to @andyearnshaw's approach above, that is a clever one.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MariuszGorzoch picture MariuszGorzoch  路  9Comments

aaarichter picture aaarichter  路  6Comments

YounesKeraressi picture YounesKeraressi  路  8Comments

evanrmurphy picture evanrmurphy  路  7Comments

yukulele picture yukulele  路  10Comments