简体   繁体   English

设置弹性Div的高度

[英]Setting the Height of an Elastic Div

I am very new to Javascript, and am in need of some assistance! 我对Java语言非常陌生,需要一些帮助! The issue I am having is rooted in my slideshow header. 我遇到的问题根源于我的幻灯片标题。 I need my header's width set to 100% so that it occupies the entire screen. 我需要将标题的宽度设置为100%,以使其占据整个屏幕。 However, because it's a slideshow, my images are all absolutely positioned, which takes my header div out of the flow of everything else, and so things overlap and look messy. 但是,因为这是幻灯片显示,所以我的图像都是绝对放置的,这使我的页眉div脱离了其他所有内容,因此事情重叠并且看起来很混乱。

I've got a cheap and lame "fix" right now where I've set my footer's margin-top to 38% to compensate for the header. 我现在有一个便宜又la脚的“修复”,在这里我将页脚的页边距设置为38%以补偿页眉。 But at high resolutions, this does not work, and it still overlaps. 但是在高分辨率下,这是行不通的,并且仍然重叠。

What I want to do is use Javascript or JQuery to detect the height of the images in the slideshow (which changes depending on the width of the browser) and then set the header's height to that value, thus eliminating my crappy CSS bandaids that aren't really working in the way I need them to. 我想要做的是使用Javascript或JQuery来检测幻灯片中图像的高度(其随浏览器宽度的变化而变化),然后将标题的高度设置为该值,从而消除了我不喜欢的CSS绷带确实以我需要他们的方式工作。

Here's the site, so you can get a sense of what I am dealing with: http://www.legacyofdesigns.com/ 这是网站,因此您可以大致了解我正在处理的内容: http : //www.legacyofdesigns.com/

THE HTML: HTML:

<div id="header">
<img src="images/header1.png" alt="" class="active" />
<img src="images/header2.png" alt="" />
<img src="images/header3.png" alt="" />
<img src="images/header4.png" alt="" />
<img src="images/header5.png" alt="" />
</div>

<div id="footer">         
    <ul>
    <li><a href="latest-projects.html"><img src="images/project_button.png"></a></li>
    <li><a href="start-your-legacy.html"><img src="images/start_button.png"/></a></li>
    <li><a href="https://www.facebook.com/legacyofdesigns" target="_blank"><img src="images/facebookFanButton.png"/></a></li>

    </ul>

    <p>© 2011 Legacy of Designs. All rights reserved.
    <br/>Website designed by <a href="http://www.indulgemedia.ca" target="_blank">Indulge Media</a></p>
</div>

THE CSS: CSS:

#header {position: absolute; margin-top: -150px; width: 100%;} 

#header img {position: absolute; top: 0px; width: 100%;} 

#footer {position: relative; margin-left: auto; margin-right: auto; margin-top: 38%; width: 1000px;}

It should be noted that both the header and the footer are contained by a container div with relative positioning. 应该注意的是,页眉和页脚都包含在具有相对位置的容器div中。

I figured it out! 我想到了!

The CSS: I changed the position of #header to relative... CSS:我将#header的位置更改为相对...

#header {position: relative; margin-top: -150px; width: 100%;} 

#header img {position: absolute; top: 0px; width: 100%;} 

#footer {position: relative; margin-left: auto; margin-right: auto; margin-top: 38%; width: 1000px;}

...and I added this functionality to calculate the height of the header div based on the height of img.active: ...并且我添加了此功能来根据img.active的高度计算标题div的高度:

$(document).ready(function() {
$('#header').css("height",$("IMG.active").innerHeight());
});

$(window).resize(function() {
$('#header').css("height",$("IMG.active").innerHeight());
});

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

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