try adding your script at the end of the document. The reason is that, a the beginning your document width is zero, because you content is not loaded yet, so there's nothing to display and so, you width is zero
当获取屏幕当前大小时,提示
Uncaught TypeError: Cannot read property 'clientWidth' of null
在网上找到结论,亲测有效。是在js执行前,html还未加载,屏幕尺寸为0,需把js放到html的最后。
参考自:
try adding your script at the end of the document. The reason is that, a the beginning your document width is zero, because you content is not loaded yet, so there's nothing to display and so, you width is zero