[英]Javascript - Dynamic div width depending on page size
I need to set the width of a div depending on the page size. 我需要根据页面大小设置div的宽度。
I have three columns. 我有三列。 The first and third ones are 50px fixed.
第一个和第三个是50px固定。 I need to set my middle one so it will take all the space with a small margin.
我需要设置我的中间位置,以便占用所有空间。 I haven't found how to do it in CSS.
我还没有找到如何在CSS中做到这一点。 So I tried using Javascript with
window.innerWidth
and subtracting the two 50px. 所以我尝试使用带有
window.innerWidth
Javascript并减去两个50px。
Here is my CSS : 这是我的CSS:
<div class="col1">
...
</div>
<div class="col2">
<div class="col2-1">
...
</div>
<div class="col2-2">
...
</div>
</div>
And the corresponding Javascript 和相应的Javascript
<script type="text/javascript">
setStoreInfoWidth = function () {
$('div.col2-1').css('width', window.innerWidth-100 + 'px')
};
setStoreInfoWidth();
$(window).resize(function () {
setStoreInfoWidth();
});
</script>
Here is a JsFiddle with the code working : http://jsfiddle.net/MrYUb/ 这是一个代码工作的JsFiddle: http : //jsfiddle.net/MrYUb/
However, I can't make it work on my actual website. 但是,我无法在我的实际网站上运行它。
Do you have any idea why? 你知道为什么吗?
You can use negative margins to create a 3 column layout: 您可以使用负边距来创建3列布局:
http://jsfiddle.net/teynon/J2mx7/2/ http://jsfiddle.net/teynon/J2mx7/2/
<div class="container">
<div class="leftCol">
Left stuff
</div>
<div class="rightCol">
Right Stuff
</div>
<div class="middleCol">
Middle stuff
</div>
</div>
<div style="background-color: #0000BB; clear: both;">Test</div>
CSS: CSS:
.container {
width: 100%;
position: relative;
}
.leftCol {
float: left;
margin-right: -50px;
width: 50px;
left: 0px;
min-height: 100px;
background-color: #FF0000;
}
.rightCol {
width: 50px;
margin-left: -50px;
right: 0px;
min-height: 50px;
background-color: #00FF00;
float: right;
}
.middleCol {
margin: 0px 55px 0px 55px;
background-color: #0000FF;
min-height: 50px;
}
Why not using percent? 为什么不使用百分比? If you have 2 divs, that 'll make 50% for each.
如果你有2个div,那么每个div将达到50%。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.