如果我们使用jest和typescript,其中使用了一个交集观察者,那么模拟交集观察者将变得很困难。到目前为止,我在: beforeEach(() => {
// IntersectionObserver isn't available in test environment
const mockIntersectionObserver = class {
observe() {
console.log(this);
}
unobserve() {
console.log(this);
}
disco
是否有方法将内置的javascript方法存储在变量中,以便在某些浏览器中无法使用此方法时设置不同的行为?
我的具体案例是intersectionObserver,它在Safari或旧的MS浏览器中是不可用的。我有一些动画触发这一点,并希望关闭他们如果intersectionObserver是不可用的。
我想要做的基本上是:
var iO = intersectionObserver;
if ( !iO ) {
// set other defaults
}
我真的不想只为一个特性加载一个多填充或库?
非常感谢
艾米丽
我正在使用现代图来检查浏览器是否支持intersectionobserver。如果不是,它应该加载一个多填充,我们有,这应该是可行的。然而,在Chrome < v50中,它似乎不像预期的那样:
Uncaught TypeError: document.body.append is not a function
守则是:
if (Modernizr.intersectionobserver) {
// supported already
} else {
const script = document.createElement('script');
这是我第一次使用IntersectionObserver,我遵循了这个文档https://www.netguru.com/codestories/infinite-scroll-with-vue.js-and-intersection-observer。但是因为这个错误我被屏蔽了 [Vue warn]: Error in mounted hook: "TypeError: Failed to construct 'IntersectionObserver': The provided value is not of type '(Element or Docu