黑色不透明叠加层使图像增大

4
我有一个小问题,真的很烦人。每当我的图片被悬停时,会出现一个带有不透明度的黑色叠加层。但是,它会使图像在高度方面增长。请注意,我指的不是transform,scale属性。实际上,图像在底部增加了高度。
这是什么原因造成的?

$('.home-img-block').find('img').each(function() {
  var imgClass = (this.width / this.height > 1) ? 'wide' : 'tall';
  console.log(imgClass);
  $(this).addClass(imgClass);
});
#home-img-block-section {
  width: 100%;
  height: 900px;
}
#home-img-blocks {
  width: 100%;
  height: 450px;
}
.home-img-block {
  width: 33.33%;
  /*height: 100%;*/
  float: left;
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}
.home-img-block:hover .overlay {
  background: rgba(0, 0, 0, 0.6);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
.home-img-block:after {
  content: attr(data-content);
  color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  border: 1px solid #fff;
  padding: 20px 25px;
  text-align: center;
}
.home-img-block:hover:after {
  opacity: 1;
}
.home-img-block img {
  -webkit-transition: all 1s ease;
  /* Safari and Chrome */
  -moz-transition: all 1s ease;
  /* Firefox */
  -ms-transition: all 1s ease;
  /* IE 9 */
  -o-transition: all 1s ease;
  /* Opera */
  transition: all 1s ease;
}
.home-img-block:hover img {
  -webkit-transform: scale(1.25);
  /* Safari and Chrome */
  -moz-transform: scale(1.25);
  /* Firefox */
  -ms-transform: scale(1.25);
  /* IE 9 */
  -o-transform: scale(1.25);
  /* Opera */
  transform: scale(1.25);
  background: rgba(0, 0, 0, 0.3);
  width: 33.33%;
  max-height: 100%;
}
.home-img-block img.wide {
  max-width: 100%;
  max-height: 100%;
  height: auto;
  width: 100%;
}
.home-img-block img.tall {
  max-width: 100%;
  max-height: 100%;
  width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home-img-blocks">
  <div data-content="FIND OUT MORE" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test1.jpg">
    <div class="overlay"></div>
  </div>
  <div data-content="FIND OUT MORE" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test2new.jpg">
    <div class="overlay"></div>
  </div>
  <div data-content="FIND OUT MORE" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test3new.jpg">
    <div class="overlay"></div>
  </div>
</div>


2
完全与您的问题无关,但您可以将添加类的第一个jQuery片段缩短为:$('.home-img-block img').addClass(function () { return (this.width / this.height > 1) ? 'wide' : 'tall'; });(但这只是一个随意的FYI,并且与您发布的问题无关。) :) - David Thomas
1个回答

6

图片的 display 属性很奇怪。它们既不是 blockinline-block,也不是 inline。由于其 baseline 属性,请尝试将 display: block 应用于图片。

$('.home-img-block').find('img').each(function() {
  var imgClass = (this.width / this.height > 1) ? 'wide' : 'tall';
  console.log(imgClass);
  $(this).addClass(imgClass);
});
img {
  display: block;
}
#home-img-block-section {
  width: 100%;
  height: 900px;
}
#home-img-blocks {
  width: 100%;
  height: 450px;
}
.home-img-block {
  width: 33.33%;
  /*height: 100%;*/
  float: left;
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}
.home-img-block:hover .overlay {
  background: rgba(0, 0, 0, 0.6);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
.home-img-block:after {
  content: attr(data-content);
  color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  border: 1px solid #fff;
  padding: 20px 25px;
  text-align: center;
}
.home-img-block:hover:after {
  opacity: 1;
}
.home-img-block img {
  -webkit-transition: all 1s ease;
  /* Safari and Chrome */
  -moz-transition: all 1s ease;
  /* Firefox */
  -ms-transition: all 1s ease;
  /* IE 9 */
  -o-transition: all 1s ease;
  /* Opera */
  transition: all 1s ease;
}
.home-img-block:hover img {
  -webkit-transform: scale(1.25);
  /* Safari and Chrome */
  -moz-transform: scale(1.25);
  /* Firefox */
  -ms-transform: scale(1.25);
  /* IE 9 */
  -o-transform: scale(1.25);
  /* Opera */
  transform: scale(1.25);
  background: rgba(0, 0, 0, 0.3);
  width: 33.33%;
  max-height: 100%;
}
.home-img-block img.wide {
  max-width: 100%;
  max-height: 100%;
  height: auto;
  width: 100%;
}
.home-img-block img.tall {
  max-width: 100%;
  max-height: 100%;
  width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home-img-blocks">
  <div data-content="FIND OUT MORE" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test1.jpg">
    <div class="overlay"></div>
  </div>
  <div data-content="FIND OUT MORE" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test2new.jpg">
    <div class="overlay"></div>
  </div>
  <div data-content="FIND OUT MORE" class="home-img-block">
    <img src="http://optimumwebdesigns.com/images/test3new.jpg">
    <div class="overlay"></div>
  </div>
</div>


1
干得好!我之前没有意识到<img>元素的display类型(或问题)有多疯狂。 - David Thomas
1
太棒了!谢谢。我唯一做的不同之处是将其应用于这个div .home-img-block img,而不是所有的图片。再次感谢! - Becky
1
我认为那一定是个问题;无论你花了多少时间去发现原因,我都很同情你 - 尽管我内心的一部分希望你在这里提出那个问题(并自己回答),以帮助社区和那些从未遇到过那个问题的人们 :) - David Thomas
@Becky 我应该这么说。确保你只给那些图片,而不像我一样愚蠢地为所有图片提供演示目的。:P - Praveen Kumar Purushothaman
@DavidThomas 哇,真的吗?当您以“错误”的方式进行图像链接时,您将面临它。现在谁在使用图像进行链接呢?所以现在这已经过时了。我们都可以欢呼雀跃了。CSS背景图片万岁!:D - Praveen Kumar Purushothaman
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接