JavaScript窗口宽度 - 调整大小

3

我在尝试弄清楚为什么这不起作用。 它以前处理高度而不是宽度,但我现在无法让它工作。 也许有人能指点我一下问题的方向吗?

            function getWindowWidth() {
        var windowWidth = 0;
        if (typeof(window.innerWidth) == 'number') {
            innerWidth = window.innerWidth;
        }
        else {
            if (document.documentElement && document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            }
            else {
                if (document.body && document.body.clientWidth) {
                    windowWidth = document.body.clientWidth;
                }
            }
        }
        return windowWidth;
    }
    function Removewhensmall(id) {
        if (document.getElementById) {
            var windowWidth = getWindowWidth();
            if (windowWidth > 0) {


              var contentElement = document.getElementById(id);
                var contentWidth = contentElement.offsetWidth;

                if (windowWidth < 600) {
                    contentElement.style.display = 'none';


                }


            }
        }
    }
    window.onload = function() {

Removewhensmall('rightwrap');
    Removewhensmall('leftwrap2');

    }
    window.onresize = function() {

            Removewhensmall('rightwrap');
    Removewhensmall('leftwrap2');

    } 
1个回答

3
    if (typeof(window.innerWidth) == 'number') {
        innerWidth = window.innerWidth;
    }

这不应该是

    if (typeof(window.innerWidth) == 'number') {
        windowWidth = window.innerWidth;
    }

?

And further in the code

var contentWidth = contentElement.offsetWidth;

已经定义了contentWidth,但后面没有再次使用...

此外,您应该使用elseif()来避免多个嵌套的if语句。


如果(typeof(window.innerWidth) == 'number') { windowWidth = window.innerWidth; } 是主要问题。 - Troels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接