【问题标题】:JS: check if browser supports focus optionsJS:检查浏览器是否支持焦点选项
【发布时间】:2019-12-29 18:42:15
【问题描述】:

有什么方法可以检测(不使用user-agent 和类似的).focus() 支持 focusOptions,例如:preventScroll: true

我在 Chrome 中使用它:

element.focus({
  preventScroll: true
});

当然,这在 Chrome 和 Opera 之外的任何地方都不起作用。 但是我怎么能检测到呢?喜欢:

try { window.focus().preventScroll } catch {...}

有可能吗?

【问题讨论】:

  • 我能想到一种方法,但它涉及(可能)滚动,并且还可能由于用户操作而返回误报,所以......没有帮助。
  • @T.J.Crowder 你能解释一下吗?
  • 确保您在视口底部有输入,发出调用,并查看滚动位置是否改变。如果是这样,浏览器不支持preventScroll 用户碰巧滚动。如果没有,也许浏览器支持它。

标签: javascript html google-chrome firefox


【解决方案1】:

你可以试试这个:

    let supported = false;

    document.createElement('div').focus({
        get preventScroll() {
            supported = true;

            return false;
        },
    });

    console.log(supported);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 2020-01-04
    相关资源
    最近更新 更多