CSS相对-绝对定位导致容器没有高度
我想创建
中包含的图像幻灯片:<ul>
<li><a href="images/01/large.jpg"><img src="images/01/default.jpg"></a></li>
<li><a href="images/02/large.jpg"><img src="images/02/default.jpg"></a></li>
<li><a href="images/03/large.jpg"><img src="images/03/default.jpg"></a></li>
<li><a href="images/04/large.jpg"><img src="images/04/default.jpg"></a></li>
<li><a href="images/05/large.jpg"><img src="images/05/default.jpg"></a></li>
<li><a href="images/06/large.jpg"><img src="images/06/default.jpg"></a></li>
<li><a href="images/07/large.jpg"><img src="images/07/default.jpg"></a></li>
<li><a href="images/08/large.jpg"><img src="images/08/default.jpg"></a></li>
<li><a href="images/09/large.jpg"><img src="images/09/default.jpg"></a></li>
<li><a href="images/10/large.jpg"><img src="images/10/default.jpg"></a></li>
<li><a href="images/11/large.jpg"><img src="images/11/default.jpg"></a></li>
<li><a href="images/12/large.jpg"><img src="images/12/default.jpg"></a></li>
<li><a href="images/13/large.jpg"><img src="images/13/default.jpg"></a></li>
</ul>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
position: relative;
}
li {
display: none;
list-style-type:none;
margin:0;
padding:0;
position: absolute;
}
</style>
您可以看到 html 标记和 CSS 非常简单。我遇到的问题是,虽然所有图像的尺寸相同,但尺寸是事先未知的。因此,我无法在
上设置固定高度,但我确实必须这样做——因为相对定位元素内的绝对定位元素意味着容器的高度为零,并且图像开始出现在上方其下方的内容。我正在使用 jQuery,所以请指导我使用基于 CSS 和/或 jQuery 的解决方案。 请注意我已经尝试过:
$("ul").height(
$("ul li:eq(0)").outerheight()
)
但是,在文档准备好时,图像未加载,因此高度未知。我还尝试添加延迟/等待 $("ul li:eq(0)").outerheight() > 0
但这会在 IE 中带来问题。当 IE 部分下载图像时,它通常会返回大于零的值,但该值通常小于实际图像高度。如果您需要任何说明,请告诉我。
I want to create a slideshow of images that are contained in a <ul>
:
<ul>
<li><a href="images/01/large.jpg"><img src="images/01/default.jpg"></a></li>
<li><a href="images/02/large.jpg"><img src="images/02/default.jpg"></a></li>
<li><a href="images/03/large.jpg"><img src="images/03/default.jpg"></a></li>
<li><a href="images/04/large.jpg"><img src="images/04/default.jpg"></a></li>
<li><a href="images/05/large.jpg"><img src="images/05/default.jpg"></a></li>
<li><a href="images/06/large.jpg"><img src="images/06/default.jpg"></a></li>
<li><a href="images/07/large.jpg"><img src="images/07/default.jpg"></a></li>
<li><a href="images/08/large.jpg"><img src="images/08/default.jpg"></a></li>
<li><a href="images/09/large.jpg"><img src="images/09/default.jpg"></a></li>
<li><a href="images/10/large.jpg"><img src="images/10/default.jpg"></a></li>
<li><a href="images/11/large.jpg"><img src="images/11/default.jpg"></a></li>
<li><a href="images/12/large.jpg"><img src="images/12/default.jpg"></a></li>
<li><a href="images/13/large.jpg"><img src="images/13/default.jpg"></a></li>
</ul>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
position: relative;
}
li {
display: none;
list-style-type:none;
margin:0;
padding:0;
position: absolute;
}
</style>
You can see that the html markup and CSS is pretty straight forward. The problem I have is that although all images are of same size, the size is not known in advance. I therefore cannot set a fixed height on the <ul>
but I really have to -- because the absolute positioned elements inside relative positioned element means that the container has zero height and the images start to appear over the content below it.
I am using jQuery so please guide me towards a CSS and/or jQuery based solution. Please note that I 've already tried:
$("ul").height(
$("ul li:eq(0)").outerheight()
)
but, on document ready, the images are not loaded and hence the height is unknown. I also tried adding a delay/waiting for $("ul li:eq(0)").outerheight() > 0
but this gives problems in IE. IE often returns a value greater than zero when it has partially downloaded the image but this value if often less than actual image height. Let me know if you need any clarification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想说从相反的角度来做这件事在技术上会更好。去掉显示:无;从你的 CSS 中,然后在页面加载时获取框的高度(这样,没有 JavaScript 人们仍然可以看到图像)。然后,通过 javascript 将 li 的显示属性设置为 none。
您可以获得高度和相同的功能!
I would say it'd be technically better to do this from the opposite angle. Remove the display: none; from your CSS, then get the height of the box when the page loads (this way, without javascript people can still see the images). Then, set the display property of the li's to none via javascript.
You get the height, and the same functionality!
例如:
借用这个问题的答案 用JavaScript获取图像的真实宽度和高度? (在 Safari/Chrome 中)
For example:
Borrows from the answer to this question Get the real width and height of an image with JavaScript? (in Safari/Chrome)
并通过这样做?
And by doing this?