Javascript问题:工作在FF,但不是在Chrome和Safari

Javascript problem: Works in FF but not in Chrome and Safari

本文关键字:Chrome Safari 问题 工作 FF Javascript      更新时间:2023-09-26

嗨,这是我的代码,我在一个div上使用,以便自动将其高度设置为窗口一。该脚本在Firefox中工作得很好,但在Chrome或Safari中则不行。我尝试了一些方法来修复它,包括一些从以前的海报,但没有效果,不幸的是。我认为问题隐藏在document.getElementById("ID").style.height的某个地方,但我不完全确定。如果有人能帮忙我会很高兴。提前感谢!:)

window.onload = Resize;
window.onresize = Resize;
function Resize() {
  if (document.documentElement.scrollHeight === document.documentElement.clientHeight)
  {
     document.getElementById("ID").style.height = (window.innerHeight-220) + "px";
  }
};

try this

function getHeight() {
    var myHeight = 0;
    if (typeof(window.innerHeight) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}
function getScrollY() {
    var scrOfY = 0;
    if (typeof(window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
    } else if (document.body && document.body.scrollTop) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}
function Resize() {
    document.getElementById("ID").style.height = Math.round(getHeight() + getScrollY()) + "px";
}
window.onload = Resize();
window.onresize = Resize();

这将把ID的高度设置为浏览器窗口的高度+垂直滚动条的偏移量。