【问题标题】:Scale image and add text overlay on hover缩放图像并在悬停时添加文本叠加
【发布时间】:2016-08-04 15:04:14
【问题描述】:

我有一组图像显示在另一个下方,第一眼看起来好像是一个完整的图像,但在悬停时图像会在悬停时缩小。

不过,这些图片中的每一个都链接到我网站上的不同页面,所以我还想在悬停时在每张图片的中心添加一些文字。

我找到了很多关于如何添加文本的建议,但没有一个不会破坏我已有的图像过渡效果。

<style>
.image4 {
 -webkit-transition: all 0.7s ease;
 transition: all 0.7s ease;
} 

.image4:hover {
-webkit-transform:scale(0.7);
transform:scale(0.7);
}

</style>
<a>
<A HREF="http://philandkaren.weebly.com/the-day.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage1.jpeg">
</a>

<a>
<A HREF="http://philandkaren.weebly.com/getting-there.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage2.jpeg">
</a>

<a>
<A HREF="http://philandkaren.weebly.com/local-information.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage3.jpeg">
</a>

<a>
<A HREF="http://philandkaren.weebly.com/accommodation.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage4.jpeg">
</a>

<a>
<A HREF="http://philandkaren.weebly.com/taxis.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage5.jpeg">
</a>

<a>
<A HREF="http://philandkaren.weebly.com/honeymoon-fund.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage6.jpeg">
</a>


<a>
<A HREF="http://philandkaren.weebly.com/faqs.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage7.jpeg">
</a>

<a>
<A HREF="http://philandkaren.weebly.com/rsvp.html">
<img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage8.jpeg">
</a>

有谁知道我可以如何实现我正在寻找的文本叠加,同时保留图像缩放?每张图片为 750 x 128。

【问题讨论】:

  • 你有一些非常奇怪的标记......你有链接吗?

标签: html css hover overlay scale


【解决方案1】:

将链接用作包装器,使用position:relative 并将您的文本内容添加到绝对定位的叠加层中。

然后将悬停触发器移动到父链接:

a:hover .image4 {
  -webkit-transform: scale(0.7);
  transform: scale(0.7);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
.image4 {
  -webkit-transition: all 0.7s ease;
  transition: all 0.7s ease;
  display: block;
}
a:hover .image4 {
  -webkit-transform: scale(0.7);
  transform: scale(0.7);
}
a {
  margin: 1em;
  display: inline-block;
  position: relative;
  text-decoration: none;
  color: white;
}
a .overlay {
  text-decoration: none;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity .7s ease;
}
a:hover .overlay {
  opacity: 1;
}
<a HREF="http://philandkaren.weebly.com/faqs.html">
  <img class="image4" src="http://philandkaren.weebly.com/files/theme/Homepage7.jpeg">
  <div class="overlay">
    <h1>OVERLAY TEXT</h1>
  </div>
</a>

【讨论】:

  • 谢谢 - 这正是我正在寻找的,只是我现在的每张图片之间都有一个间隙,所以当第一次查看页面时,它们看起来不像一张图片。你知道我该如何解决吗?我是一个完全的初学者,如果我从一个陌生的地方开始,请道歉......
  • 这是由 inline-block 元素的空格引起的 - stackoverflow.com/questions/5078239/…
猜你喜欢
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
相关资源
最近更新 更多