私のサイトには、Internet Explorerを除くすべてのブラウザで動作するスクリプトがあります。誰かがなぜこれがうまくいかないか説明できますか?スクロールハンドラJavaScriptがInternet Explorerで動作しない
var header = document.getElementById('header');
var picturebg = document.getElementsByClassName('picturebg')[0];
var arrow = document.getElementsByClassName('toTop-transparent')[0];
var supportPageOffset = window.pageXOffset !== undefined;
window.onscroll = function() {
"use strict";
var y = window.scrollY;
if (y > 7) {
header.className = 'header-colored';
arrow.className = 'toTop';
picturebg.className = 'picturebgns';
} else {
header.className = 'header-transparent';
arrow.className = 'toTop-transparent';
picturebg.className = 'picturebg';
}
};
コンソールはすべてのエラーを与えるものではありません、それだけでは動作しません。私はうまく動作する別のjQueryスクリプトを持っています。
私は同じことについて他の質問を見ましたが、それは決して役に立たなかった。
あなたが使用している機能の互換性をチェックするためにwww.caniuse.comを使用することがありますか? – Carcigenicate
そのスニペットはHTMLを持たないので動作しません。したがって、document.getElementById()はnullを返します。 –
jQueryが既にロードされている場合、これをjQueryに移植するだけではどうですか? jQueryの利点の1つは、そのメソッドがすべての最新のブラウザで動作することがテストされていることです。 – junkfoodjunkie