The*_*ewy 7 javascript feature-detection intersection-observer
当某些浏览器无法使用此方法时,是否可以将内置的javascript方法存储在变量中以设置不同的行为?
我的特定情况是在Safari或旧版MS浏览器中不可用的intersectionObserver。我有一些由此触发的动画,如果不可用intersectionObserver,请关闭它们。
我想要做的基本上是这样的:
var iO = intersectionObserver;
if ( !iO ) {
// set other defaults
}
Run Code Online (Sandbox Code Playgroud)
我真的不想只为一个功能加载polyfill或库吗?
非常感谢
艾米莉
将在运营商广泛用于检测浏览器支持的功能。JavaScript功能可作为window对象的属性全局使用。因此,我们可以检查windowelement是否具有属性。
if("IntersectionObserver" in window){
/* work with IntersectionObserver */
}
if("FileReader" in window){
/* work with FileReader */
}
Run Code Online (Sandbox Code Playgroud)
的
in操作者返回true如果指定的属性是在指定的对象或它的原型链。
语法:物件中的prop
[来源:developer.mozilla.org ]
因此,您也可以将结果保存在boolean变量中,并在以后的代码中使用它。
var iO = "IntersectionObserver" in window; /* true if supported */
if ( !iO ) {
// set other defaults
}
Run Code Online (Sandbox Code Playgroud)