jquery - 如何调整与浏览器大小成比例的 div 大小?

标签 jquery html css

JQuery

    $(window).ready(function(){

    var w = $(window).width();//to detect the width of the browser
    var h = $(window).height();//to detect the height of the browser

    $(window).resize(function(){

        var w = $(window).width();//new width of the browser
        var h = $(window).height();//new height of the browser

        $("#left").css('height'+ h 'px.');//this is where I'm not sure how to assign height      value according to the browser size.

    });  

    });

我的 CSS

   #main
{
    width: 100%;
    height: 100%;
    position: relative;
    background: #000000;
}
#left
{
    width: 20%;
    height: 650px;
    position: relative;
    float: left;
    background: #666666;

}
#right
{
    width:80%;
    height: 650px;
    position: relative;
    float:right;
    background: #ff9900;
}    

我的 Html

   <div id="main">
        <div id="left">
        </div>

        <div id="right">
        </div>

    </div>

现在的问题是左右DIV的高度。 如果我设置高度:100%;它遵循我不想要的内容高度。 我希望高度向下拉伸(stretch)以适合浏览器高度。 所以如果我使用,height:650px;它可以工作但是如果页面调整到更小或更大的尺寸怎么办? 如何检测浏览器正在查看的页面的大小,并按比例改变高度,这样就不会出现滚动条了?

最佳答案

这是div的默认机制..不需要为此写任何东西 只需将 <body> 的边距和填充都设为零即可和 <div> .

或者如果你对 jquery 或 javascript 感兴趣.. 然后你可以通过使用

来做得更好

$(window).innerWidth()$(window).innerHeight() //以上代码返回浏览器的视口(viewport)区域

尽管使用的方法

$(window).width()$(window).height() . //此代码返回整个浏览器的宽度和高度。

关于jquery - 如何调整与浏览器大小成比例的 div 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20137101/

相关文章:

css - 使用 Webpack、Sass 自定义字体不生效

java - 如何在不使用文本的情况下从页面中选择内容?

jquery - 如何从该网站实现标题生成

jquery - 选择上的 addClass()

jquery 获取 url 参数(如果存在)

javascript - 将键盘焦点设置为 <div>

html - 在 bootstrap 中对齐 list-group-item 中的 3 个元素

javascript - 如何覆盖脚本创建的 html 属性 (mediaelement.js)

javascript - 发送数据到下一页触发JS中的事件

html - 为什么我在 css 类中收到错误 Unknown property name?