如何获取附加元素作为图像的高度

时间:2019-01-08 13:46:23

标签: javascript html css css3

我将div javac -help | grep releases附加到特定的.soonOverlay。我似乎无法弄清楚的是如何使.smallCatalogBlock的高度仅与soonOverlay中的img一样高。现在,它的高度覆盖了整个容器。

有人看到我在做什么错或如何解决吗?

smallCatalogBlock
$('.smallCatalogBlock').each(function() {
		if ($(this).data('availability') === 'No') {
			$(this).append('<div class="soonOverlay"><div class="soonOverlayInner"><div class="total-center"><p class="dGw">Coming Soon</p></div></div></div>');
			console.log("It should be working");
		}
	});
.smallCatalogWrap {
	width: 100%;
	height: auto;
	margin: 60px 0;
}
.smallCatalogBlock {
	width: 60%;
	height: auto;
	display: inline-block;
	vertical-align: top;
	margin: 20px auto;
	text-decoration: none;
}
.smallCatalogBlock img {
	width: 80%;
	height: auto;
	box-shadow: 10px 5px 5px rgba(0,0,0,.3);
	display: block;
	margin: 0px auto 15px auto;
}
.smallCatalogTitle {
	font-family: 'Nunito', sans-serif;
	color: #4d4d4d;
	font-size: 1.3rem;
	text-align: center;
	display: block;
	font-weight: 400;
}
.comingSoonSmall {
	position: relative;
}
.comingSoonSmall .soonOverlay {
	width: 80%;
	height: 100%;
	background: #b82222;
	opacity: .8;
	position: absolute;
	top: 0;
	margin: 0 10%;
}
.soonOverlayInner {
	position: relative;
	min-height: 350px;
}
.soonOverlayInner .dGw {
	font-weight: 600;
	text-transform: uppercase;
	font-size: 2.5rem;
}

1 个答案:

答案 0 :(得分:1)

我已经更新了以下脚本。

$('.smallCatalogBlock').each(function() {
		if ($(this).data('availability') === 'No') {
			$(this).append('<div class="soonOverlay"><div class="soonOverlayInner"><div class="total-center"><p class="dGw">Coming Soon</p></div></div></div>');
			console.log("It should be working");
      var img_w = $('.smallCatalogBlock > img').width();
      var img_h = $('.smallCatalogBlock > img').height();
      $('.soonOverlay').width(img_w).height(img_h);
		}
    
	});
.smallCatalogWrap {
	width: 100%;
	height: auto;
	margin: 60px 0;
}
.smallCatalogBlock {
	width: 60%;
	height: auto;
	display: inline-block;
	vertical-align: top;
	margin: 20px auto;
	text-decoration: none;
}
.smallCatalogBlock img {
	width: 80%;
	height: auto;
	box-shadow: 10px 5px 5px rgba(0,0,0,.3);
	display: block;
	margin: 0px auto 15px auto;
}
.smallCatalogTitle {
	font-family: 'Nunito', sans-serif;
	color: #4d4d4d;
	font-size: 1.3rem;
	text-align: center;
	display: block;
	font-weight: 400;
}
.comingSoonSmall {
	position: relative;
}
.comingSoonSmall .soonOverlay {
	width: 80%;
	height: 100%;
	background: #b82222;
	opacity: .8;
	position: absolute;
	top: 0;
	margin: 0 10%;
}
.soonOverlayInner {
	position: relative;
	min-height: 350px;
}
.soonOverlayInner .dGw {
	font-weight: 600;
	text-transform: uppercase;
	font-size: 2.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
<div class="smallCatalogBlock comingSoonSmall" data-availability="No"> 
<img src="https://boygeniusreport.files.wordpress.com/2016/11/puppy-dog.jpg?quality=98&strip=all&w=782" alt="Fastening Technology Catalog"> 
<span class="smallCatalogTitle">Technology</span> 
<div class="smallCatalogButtonWrap"> 
<div class="catalogSmallCircle"></div> 
</div> 
</div>