将旧的js项目覆盖到webpack npm得到错误,例如无法读取null的属性'className'



我正在将旧的codekit项目转换为webpack。我几乎在那里,但在使用滚动揭示时出现奇怪的错误,我以前从未得到过。

有错误似乎纯粹与javascript相关...

Uncaught TypeError: Cannot read property 'className' of null

这是当前在旧站点上使用的用法。

// scroll reveal
const ScrollReveal = require('scrollreveal');

// jQuery
(function ($) {
// scroll reveal profile listings
if (!/(?:^|s)ie-[6-9](?:$|s)/.test(document.body.className)) {
window.sr = new ScrollReveal({reset: false});
sr.reveal('[data-reveal="true"]', {duration: 1000});
}
})(jQuery);

我正在做的是用一些正则表达式测试主体类,以确保 scrollReveal 不会在 IE 6-9 中触发。

一切都编译正常,只是我在控制台日志中出现错误并且没有滚动显示。

任何想法都会很棒,谢谢。

在代码周围添加一个包装器:

document.addEventListener("DOMContentLoaded", function(event) { 
// scroll reveal
const ScrollReveal = require('scrollreveal');
// scroll reveal profile listings
if (!/(?:^|s)ie-[6-9](?:$|s)/.test(document.body.className)) {
window.sr = new ScrollReveal({reset: false});
sr.reveal('[data-reveal="true"]', {duration: 1000});
} 
});

相关内容

最新更新