如何使图像动画对叠加文本生效?

时间:2017-08-19 08:32:43

标签: html css twitter-bootstrap

我为一个图像制作了一个动画,在悬停时使用叠加文字将其从黑白变为彩色,但当悬停在叠加文本图像上时,图像会变回黑白。我怎样才能保持它的颜色?

这是我的代码:

#img {
  filter: gray; /* IE6-9 */
  -webkit-filter: grayscale(1); /* Google Chrome, Safari 6+ & Opera 15+ */
  -webkit-box-shadow: 0px 2px 6px 2px rgba(0,0,0,0.75);
  -moz-box-shadow: 0px 2px 6px 2px rgba(0,0,0,0.75);
  box-shadow: 0px 2px 6px 2px rgba(0,0,0,0.75);
  margin-bottom:20px;
}
#img:hover {
  filter: none; /* IE6-9 */
  -webkit-filter: grayscale(0); /* Google Chrome, Safari 6+ & Opera 15+ */
}
.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%)
}
.container1:hover .image {
  opacity: 0.3;
}

.container1:hover .middle {
  opacity: 1;
}

.text {
  background-color: rgb(45, 45, 65);
  color: rgb(255, 250, 235);
  text-align: center;
  font-size: 14px;
  padding: 16px 32px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-md-6 col-sm-4 col-xs-6 container1">
  <img id="img" class="img-responsive" src="http://lorempixel.com/400/200/"/>
  <div class="middle">
    <div class="text">Text</br>
      <a href="https://vimeo.com/206720941" target="_blank">Watch Video</a>
    </div>
  </div>
</div>

enter image description here

1 个答案:

答案 0 :(得分:2)

您只需将元素的:hover selector#img:hover更改为.container1:hover #img,因此每当mouseenter容器将样式属性添加到#img时。

.container1:hover #img {
  filter: none;
  /* IE6-9 */
  -webkit-filter: grayscale(0);
  /* Google Chrome, Safari 6+ & Opera 15+ */
}

&#13;
&#13;
#img {
  filter: gray;
  /* IE6-9 */
  -webkit-filter: grayscale(1);
  /* Google Chrome, Safari 6+ & Opera 15+ */
  -webkit-box-shadow: 0px 2px 6px 2px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 2px 6px 2px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 2px 6px 2px rgba(0, 0, 0, 0.75);
  margin-bottom: 20px;
}

.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%)
}

.container1:hover .image {
  opacity: 0.3;
}

.container1:hover .middle {
  opacity: 1;
}

.container1:hover #img {
  filter: none;
  /* IE6-9 */
  -webkit-filter: grayscale(0);
  /* Google Chrome, Safari 6+ & Opera 15+ */
}

.text {
  background-color: rgb(45, 45, 65);
  color: rgb(255, 250, 235);
  text-align: center;
  font-size: 14px;
  padding: 16px 32px;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-md-6 col-sm-4 col-xs-6 container1">
  <img id="img" class="img-responsive" src="http://lorempixel.com/400/200/" />
  <div class="middle">
    <div class="text">Text</br>
      <a href="https://vimeo.com/206720941" target="_blank">Watch Video</a>
    </div>
  </div>
&#13;
&#13;
&#13;