繁体   English   中英

100%Height Div与jQuery和CSS

[英]100% Height Div with jQuery and CSS

我一直在尝试创建一套全页DIV,但在移动平台上却没有成功。 在iOS和Android设备上,第一个DIV会显示正常,但是即使我的边距设置为“ 0”,第二个DIV也会有较高的利润率。

滚动到底部后,第一个DIV的红色仍然可见。

屏幕截图

这是我用来复制问题的JavaScript ...

$(document).ready(function() {

    $(window).resize(function() {

        var newHeight = $(window).height();

        $('div').css({
            width: '100%',
            height: newHeight + 'px'
        });

    });

});

CSS ...

        html, body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            border: 0;
        }

        div {
            width: 100%;
            height: 100%;
        }

        .red {
            background: red;
        }

        .blue {
            background: blue;
        }

和HTML ...

<div class="red">
</div>

<div class="blue">
</div>

为什么不只使用CSS?

.red, .blue{
    position:fixed;
    top:0;
    left:0;
    right:0;
    bottom:0;
}

我通过将DIV包裹在容器DIV中并将overflow-x设置为隐藏来解决此问题。

新的CSS。

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 0;
}

div#container {
    overflow-x: hidden;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 0;
}

.red {
    width: 100%;
    height: 100%;
    background: red;
}

.blue {
    width: 100%;
    height: 100%;
    background: blue;
}

Javascript ...

$(document).ready(function() {
    $(window).resize(function() {
        var winHeight = $(window).height(); winWidth = $(window).width();
        $('fp').css(function(){height : winHeight + " !important", width : winWidth + '!important'});
    });
});

现在,它在移动和桌面浏览器中都可以正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM