简体   繁体   English

如何在两个div之间设置侧边栏,它应基于调整大小而适合任何浏览器窗口

[英]How to set side bar between two div and it should fit in the any browser window based on resize

I am having two grid one div is header another one is footer between the two grid, I am using sidebar in left side. 我有两个网格,一个div是页眉,另一个是两个网格之间的页脚,我在左侧使用侧边栏。

Here is my JavaScript code 这是我的JavaScript代码

function resize()
{
    var heights = window.innerHeight;
    document.getElementById("left").style.height = heights + "px";
}
resize();
window.onresize = function() {
    resize();
};

I want to show this full content with in the page without show any browser scroll bar. 我想在页面中显示此完整内容,而不显示任何浏览器滚动条。

Here is my demo 这是我的演示

Click here to see my demo 点击这里查看我的演示

Using Jquery: 使用jQuery:

You take the browser screen height 您以浏览器的屏幕高度为准

$(window).height()

& then reduce the height of header & footer 然后降低页眉和页脚的高度

$(window).height() - $('header').outerHeight() - $('footer').outerHeight()

Using JS 使用JS

window.innerWidth - document.getElementById('header').offsetHeight - document.getElementById('footer').offsetHeight;

Instead of offsetHeight you can also use clientHeight. 除了offsetHeight,您还可以使用clientHeight。 Difference is: 区别是:

clientHeight includes padding. clientHeight包括填充。

offsetHeight includes padding, scrollBar and borders. offsetHeight包括填充,scrollBar和边框。

Updated 更新

Update your function resize() as: 将您的function resize()更新为:

document.getElementById('inner').style.maxHeight = window.innerWidth - document.getElementById('header').offsetHeight - document.getElementById('footer').offsetHeight;

You can use pure CSS: there are a few ways to determine the behavior of CSS to adjust to screen size. 您可以使用纯CSS:有几种方法可以确定CSS的行为以适应屏幕尺寸。 I think that a good method for this is to use the viewport units: vw and vh , this will let you achieve responsive design easily... 我认为为此的一个好方法是使用视口单位: vwvh ,这将使您轻松实现响应式设计...

vw stands for "viewport width" and vh is for "viewport height". vw代表“视口宽度”,而vh代表“视口高度”。 and, you can use it with CSS like in this example: 并且,您可以将其与CSS一起使用,例如以下示例:

div {
width: 100vw;
height: 100vh;
}

This guide may be helpful to understand this method better... 本指南可能有助于更好地了解此方法...

You can also set px for pixels and other CSS sizing units instead of % and also use vmin and vmax for minimum and maximum size adjustment, or just use min-height/width like in this example: 您还可以为像素和其他CSS大小调整单位设置px而不是 ,还可以使用vminvmax进行最小和最大大小调整,或者仅使用min-height / width,例如本示例:

.sidebar {
min-width: 30%;
min-height: 30%;
}

Another more modern way is to do it with flexbox ... 另一种更现代的方法是使用flexbox ...

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

相关问题 根据浏览器窗口的大小动态调整div的大小 - Dynamically resize div based on size of browser window (总 html 新手)如何防止网页出现滚动条,而是调整网站大小以适应浏览器窗口? - (total html novice) How to keep webpage from having a scroll bar and instead resize the site to fit within the browser window? 调整子div元素的大小以适合窗口调整大小的父div - Resize child div element to fit in parent div on window resize css/JavaScript 调整 div 内容的大小以适合 window - css/JavaScript resize div content to fit window 如何使用javascript或jquery在浏览器窗口中调整div的大小 - how to resize a div with the browser window using javascript or jquery 如何相对于浏览器窗口大小调整div的大小? - How do I resize a div relative to browser window size? 如何根据浏览器窗口宽度和文档长度自动调整iframe的大小? - How to automatically resize an iframe based on browser window width and document length? 使用JavaScript根据浏览器窗口大小设置div的高度 - Using javascript to set the height of a div based on browser window size 如何使div始终位于浏览器窗口的最左侧? - How to make a div always on the left-most side of the browser window? div根据浏览器窗口的高度调整大小 - div resize according to browser window in height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM