CSS图像无法在悬停时缩放

我已经制作了一个响应式图像网格,并正在尝试向其添加悬停效果,以使图像具有深色的覆盖层,并且一些文本会淡出。但是,我一直很难实施它。

这是我的HTML结构。

<div class="tile">
        <img src="some_image" class="animate">
        <div class="overlay">
        <p>Mahatma Gandhi</p>
</div>

这是我的CSS

.gallery .row .tile:hover ~ .tile img {
  transform: scale(1.2);
}

但是,将鼠标悬停在图像上时,它没有预期的行为。

怎么了?

编辑 我有了悬浮效果,现在可以淡入文本了。

这是我的代码:

<div class="tile">
                        <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Tagore_Gandhi.jpg/220px-Tagore_Gandhi.jpg" class="animate">
                        <div class="overlay">
                                <div class="text">Mahatma Gandhi</div>
                            </div>
                      </div>

CSS

.tile {
  position: relative;
  width: 100%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: rgba(0,0.8);
}

.tile:hover .overlay {
  opacity: 1;
}

这似乎有效,但我认为它没有一定的“感觉”。因此,我需要在图像上添加缩放效果。我该怎么办

keith_ye_mao 回答:CSS图像无法在悬停时缩放

这是我认为可以帮助您解决问题的jsFiddle:https://jsfiddle.net/mcs3yn1x/

HTML

<div class="tile">
 <img src="https://picsum.photos/200/300" class="animate">
 <div class="overlay">
 <p>Mahatma Gandhi</p>
</div>

CSS

.tile {
  border: 2px solid black;
}

.tile:hover img {
  transform: scale(1.2);
}

修改

在听到有关您的问题的详细信息后,我创建了以下jsFiddle:https://jsfiddle.net/f1gzonjr/4/

HTML

<div class="tile">
  <div class="container">
   <img src="https://picsum.photos/200/300" class="animate">
   <div class="overlay">
    <p>Mahatma Gandhi</p>
  </div>
</div>

CSS

.tile {
  position: relative;

  top: 0px;
  left: 0px;

  height: 300px;
  width: 200px;

  overflow: hidden;

  border: 2px solid black;
} 

.container:hover img{
  transform: scale(1.2);
}

.overlay{
 position: absolute;
 display: none;

 top: 0px;
 left: 0px;

 height: 100%;
 width: 100%;

 background-color: rgba(0,0.5);
}

.overlay p {
 position: relative;
 text-align: center;

 color: #fff;
}

.tile:hover .overlay{
 display: block;
}
,

这是替代解决方案。不知道它是否是您想要的。

.tile:hover img,.tile.hover img {transform: scale(1.2);}

这是我改编的原始答案:Change background color of child div on hover of parent div?

-----编辑-----

要停止缩放和破坏响应能力,您需要在图像周围添加一个容器,然后将溢出设置为无。

HTML:

<div class="tile">
    <div class="img-container"><img src="https://ichef.bbci.co.uk/news/660/cpsprodpb/16C0E/production/_109089139_928b0174-4b3f-48ff-8366-d118afa1ed56.jpg" class="animate"></div>
    <div class="overlay">
    <p>Mahatma Gandhi</p>

CSS:

.img-container{
    width: 500px;
    height: 500px;
    overflow: hidden;
}
.tile:hover img,.tile.hover img {
    transform: scale(1.2);
}

有关示例,请参见下面的codepen https://codepen.io/jamesCyrius/pen/pooqwwv

,

这是一个代码

.zoom {
  padding: 50px;
  background-color: green;
  transition: transform .2s; /* Animation */
  width: 15px;
  height: 15px;
  margin: 0 auto;
}

.zoom:hover {
  transform: scale(1.5); /* (150% zoom - Note: if the zoom is too large,it will go outside of the viewport) */
}
<div class="zoom"></div>

本文链接:https://www.f2er.com/3083592.html

大家都在问