我有一个绝对定位的“左菜单”和一个“右菜单”div。我希望他们在滚动时跟随浏览器窗口。我的解决方案在 Opera、Firefox 和 Chrome 中运行良好,但在 IE 中的结果是不可预测的。有时它会按预期工作,但有时 div 不会恢复“位置:绝对”样式。
这是我的代码:
if(window.pageYOffset) {
if(window.pageYOffset > 142) {
document.getElementById("menuRight").style.position = "fixed";
document.getElementById("menuRight").style.top = "0";
document.getElementById("menuRight").style.marginTop = "10px";
document.getElementById("menuLeft").style.position = "fixed";
document.getElementById("menuLeft").style.top = "0";
document.getElementById("menuLeft").style.marginTop = "10px";
} else {
document.getElementById("menuRight").style.position = "absolute";
document.getElementById("menuRight").style.top = "152px";
document.getElementById("menuRight").style.marginTop = "0px";
document.getElementById("menuLeft").style.position = "absolute";
document.getElementById("menuLeft").style.top = "152px";
document.getElementById("menuLeft").style.marginTop = "0px";
}
} else if(document.body && document.body.scrollTop) {
if(document.body.scrollTop > 142) {
document.getElementById("menuRight").style.position = "fixed";
document.getElementById("menuRight").style.top = "0";
document.getElementById("menuRight").style.marginTop = "10px";
document.getElementById("menuLeft").style.position = "fixed";
document.getElementById("menuLeft").style.top = "0";
document.getElementById("menuLeft").style.marginTop = "10px";
} else {
document.getElementById("menuRight").style.position = "absolute";
document.getElementById("menuRight").style.top = "152px";
document.getElementById("menuRight").style.marginTop = "0px";
document.getElementById("menuLeft").style.position = "absolute";
document.getElementById("menuLeft").style.top = "152px";
document.getElementById("menuLeft").style.marginTop = "0px";
}
} else if(document.documentElement && document.documentElement.scrollTop) {
if(document.documentElement.scrollTop > 142) {
document.getElementById("menuRight").style.position = "fixed";
document.getElementById("menuRight").style.top = "0";
document.getElementById("menuRight").style.marginTop = "10px";
document.getElementById("menuLeft").style.position = "fixed";
document.getElementById("menuLeft").style.top = "0";
document.getElementById("menuLeft").style.marginTop = "10px";
} else {
document.getElementById("menuRight").style.position = "absolute";
document.getElementById("menuRight").style.top = "152px";
document.getElementById("menuRight").style.marginTop = "0px";
document.getElementById("menuLeft").style.position = "absolute";
document.getElementById("menuLeft").style.top = "152px";
document.getElementById("menuLeft").style.marginTop = "0px";
}
}
我该怎么做才能使 IE 中的结果更可预测?