gpt4 book ai didi

javascript - 使 parent 的高度等于其 child 之一

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:56 25 4
gpt4 key购买 nike

我正在努力使粉色“#images-wrap”与主图像具有相同的高度。每当右侧的小翻转图像过多时,它会将粉色 divheight 推到超过主图像。如果我能让它与它的高度相匹配,那么我可以使用 overflow hidden 来不显示下面多余的翻转图像,并使用 overflow 作为 scroll-y 以便可以向下滚动以查看多余的小图像.

显示为表格不起作用 - 你会认为一个简单的子 div 和隐藏的溢出就可以解决问题,但你不能,因为你不能设置高度,否则图像纵横比不会调整大小。图片必须保持其 3:2 宽高比

此解决方案中的 javascript 不起作用,因为它可能无法获取图像的高度。我还尝试获取子图像的高度,但也失败了。

有谁知道可以实现这一目标的魔术吗?

非常感谢这里的任何帮助,谢谢!

#images-wrap {
width: 50%;
height: auto;
margin-top: 25px;
float: left;
display: flex;
background: red;
max-height: 150px;
}

#details-wrap {
width: 100%;
height: 325px;
float: left;
text-align: right;
position: relative;
}

#main-image {
width: 80.5%;
float: left;
background-size: cover !important;
background-position: center center !important;
height: auto;
}

#main-image>img {
display: block;
width: 100%;
height: auto;
margin: 0;
}

#image-thumbs {
width: 17.5%;
height: auto;
float: left;
margin-left: 2%;
overflow-y: scroll !important;
/* make it only scroll when exceeds height of main image */
/* max-height: 400px; make this the height of #main-image */
}

.image-thumb {
margin-bottom: 6px;
background-position: center;
background-size: cover;
height: auto;
}

.image-thumb:last-of-type {
margin-bottom: 0;
}

.image-thumb>img {
height: auto;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="images-wrap">
<div id="main-image" style="background-image: url('http://elephant-family.org/wp-content/uploads/2015/06/shutterstock_77217466.jpg')">
<img src="https://cml.sad.ukrd.com/tp/3x2/" id="main-image-sizer">
</div>
<div id="image-thumbs">

<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>


<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<script>
// hides overflow scroll if less than 5 thumbs
var thumbs = document.getElementsByClassName('image-thumb');
var thumbsWrap = document.getElementById('image-thumbs');
if (thumbs.length < 5) {
thumbsWrap.style.overflow = 'hidden';
}
</script>
<script>
// makes '#image-thumbs' not exceed the height of '#main-image'
var mainImgHeight = document.getElementById('main-image-sizer').style.height;
var imageThumbsInitialHeight = document.getElementById('image-thumbs').style.height;
if (imageThumbsInitialHeight > mainImgHeight) {
document.getElementById('image-thumbs').style.height = mainImgHeight;
}
</script>
</div>
</div>

最佳答案

您可以在主容器上使用display: flex:

#images-wrap {
width: 100%;
height: auto;
margin-top: 25px;
float: left;
display: flex;
}

#details-wrap {
width: 100%;
height: 325px;
float: left;
text-align: right;
position: relative;
}

#main-image {
width: 80.5%;
float: left;
background-size: cover !important;
background-position: center center !important;
height: auto;
}

#main-image>img {
display: block;
width: 100%;
height: auto;
margin: 0;
}

#image-thumbs {
width: 17.5%;
height: auto;
float: left;
margin-left: 2%;
overflow-y: scroll !important;
/* make it only scroll when exceeds height of main image */
/* max-height: 400px; make this the height of #main-image */
}

.image-thumb {
margin-bottom: 6px;
background-position: center;
background-size: cover;
height: auto;
}

.image-thumb:last-of-type {
margin-bottom: 0;
}

.image-thumb>img {
height: auto;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="images-wrap">
<div id="main-image" style="background-image: url('http://elephant-family.org/wp-content/uploads/2015/06/shutterstock_77217466.jpg')">
<img src="https://cml.sad.ukrd.com/tp/3x2/" id="main-image-sizer">
</div>
<div id="image-thumbs">
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<div class="image-thumb" onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));" style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')">
<img src="https://cml.sad.ukrd.com/tp/3x2/">
</div>
<script>
// hides overflow scroll if less than 5 thumbs
var thumbs = document.getElementsByClassName('image-thumb');
var thumbsWrap = document.getElementById('image-thumbs');
if (thumbs.length < 5) {
thumbsWrap.style.overflow = 'hidden';
}
</script>
<script>
// makes '#image-thumbs' not exceed the height of '#main-image'
var mainImgHeight = document.getElementById('main-image-sizer').style.height;
var imageThumbsInitialHeight = document.getElementById('image-thumbs').style.height;
if (imageThumbsInitialHeight > mainImgHeight) {
document.getElementById('image-thumbs').style.height = mainImgHeight;
}
</script>
</div>
</div>

关于javascript - 使 parent 的高度等于其 child 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48703335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com