使叠加框停留在图片内部

时间:2018-08-08 13:22:16

标签: html css twitter-bootstrap

我正在尝试对图像进行一些悬停效果。参见demosite here。我现在陷入困境,因为我希望无论将图像大小是多少,当您将鼠标悬停在每个图像的中心时,盒子都会出现。

我尝试将鼠标悬停在其上,但这不是一个好的解决方案。不管图片的大小如何,有人知道我如何在每张图片上使渐变叠加层居中吗?

.hovereffect {
  width: 100%;
  height: 100%;
  float: left;
  overflow: hidden;
  position: relative;
  text-align: center;
  cursor: default;
}

.hovereffect .overlay {
  width: 100%;
  height: 100%;
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  -webkit-transition: all 0.4s ease-in-out;
  transition: all 0.4s ease-in-out;
}

.hovereffect:hover .overlay {
  background-color: rgba(170,170,170,0.4);
}

.hovereffect h2, .hovereffect img {
  -webkit-transition: all 0.4s ease-in-out;
  transition: all 0.4s ease-in-out;
}

.hovereffect img {
  display: block;
  position: relative;
  -webkit-transform: scale(1.1);
  -ms-transform: scale(1.1);
  transform: scale(1.1);
}

.hovereffect:hover img {
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
}

.hovereffect h2 {
  text-transform: uppercase;
  color: #fff;
  text-align: center;
  position: relative;
  font-size: 17px;
  padding: 10px;
  background: rgba(0, 0, 0, 0.6);
}

.hovereffect a.info {
  display: inline-block;
  text-decoration: none;
  padding: 7px 14px;
  text-transform: uppercase;
  color: #fff;
  border: 1px solid #fff;
  margin: 50px 0 0 0;
  background-color: transparent;
  opacity: 0;
  filter: alpha(opacity=0);
  -webkit-transform: scale(1.5);
  -ms-transform: scale(1.5);
  transform: scale(1.5);
  -webkit-transition: all 0.4s ease-in-out;
  transition: all 0.4s ease-in-out;
  font-weight: normal;
  height: 85%;
  width: 85%;
  position: absolute;
  top: -20%;
  left: 8%;
  padding: 70px;
}

.hovereffect:hover a.info {
  opacity: 1;
  filter: alpha(opacity=100);
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
  background-color: rgba(0,0,0,0.4);
}
    <div class="container-fluid">
  <div class="row">
    <div class="col-sm-12">
      <div class="hovereffect">
          <img class="img-responsive" src="https://www.capitalhyundai.ca/wp-content/uploads/sites/385/2017/08/2017-hyundai-elantra-gt-interior-view.jpg" alt="text"></img>
          <div class="overlay">
             
             <a class="info" href="#">link here</a>
          </div>
      </div>
    </div>
  </div>
  <br/><br/>
  <div class="row">
    <div class="col-sm-5">
      <div class="hovereffect">
          <img class="img-responsive" src="https://www.carzone.ie/newcar/assets/img/models/kiasportage-abb0540cd673ba0e6dd80d5e1edc9c64.jpg" alt="text"></img>
          <div class="overlay">
             
             <a class="info" href="#">link here</a>
          </div>
      </div>
    </div>
  </div>
  <br/><br/>
  <div class="row">
    <div class="col-sm-3">
      <div class="hovereffect">
          <img class="img-responsive" src="http://solcontrolcustomsandtint.com/wp-content/uploads/2014/09/car-audio-system-sound-4.jpg" alt="text"></img>
          <div class="overlay">
             
             <a class="info" href="#">link here</a>
          </div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

调整图像容器inline-block的宽度,使其宽度由其内容定义,然后您可以轻松调整叠加层:

.hovereffect {
  display:inline-block;
  overflow: hidden;
  position: relative;
  text-align: center;
  cursor: default;
}

.hovereffect .overlay {
  width: 100%;
  height: 100%;
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  transition: all 0.4s ease-in-out;
}

.hovereffect:hover .overlay {
  background-color: rgba(170,170,170,0.4);
}

.hovereffect h2, .hovereffect img {
  transition: all 0.4s ease-in-out;
}

.hovereffect img {
  display: block;
  position: relative;
  transform: scale(1.1);
}

.hovereffect:hover img {
  transform: scale(1);
}

.hovereffect h2 {
  text-transform: uppercase;
  color: #fff;
  text-align: center;
  position: relative;
  font-size: 17px;
  padding: 10px;
  background: rgba(0, 0, 0, 0.6);
}

.hovereffect a.info {
  text-decoration: none;
  text-transform: uppercase;
  color: #fff;
  border: 1px solid #fff;
  background-color: transparent;
  opacity: 0;
  transform: scale(1.5);
  transition: all 0.4s ease-in-out;
  font-weight: normal;
  height: 85%;
  width: 85%;
  top:7.5%; /* (100% - 85%)/2 */
  left:7.5%;
  position: absolute;
}

.hovereffect:hover a.info {
  opacity: 1;
  transform: scale(1);
  background-color: rgba(0,0,0,0.4);
}
<div class="container-fluid">
  <div class="row">
    <div class="col-sm-12">
      <div class="hovereffect">
          <img class="img-responsive" src="https://www.capitalhyundai.ca/wp-content/uploads/sites/385/2017/08/2017-hyundai-elantra-gt-interior-view.jpg" alt="text"></img>
          <div class="overlay">
             
             <a class="info" href="#">link here</a>
          </div>
      </div>
    </div>
  </div>
  <br/><br/>
  <div class="row">
    <div class="col-sm-5">
      <div class="hovereffect">
          <img class="img-responsive" src="https://www.carzone.ie/newcar/assets/img/models/kiasportage-abb0540cd673ba0e6dd80d5e1edc9c64.jpg" alt="text"></img>
          <div class="overlay">
             
             <a class="info" href="#">link here</a>
          </div>
      </div>
    </div>
  </div>
  <br/><br/>
  <div class="row">
    <div class="col-sm-3">
      <div class="hovereffect">
          <img class="img-responsive" src="http://solcontrolcustomsandtint.com/wp-content/uploads/2014/09/car-audio-system-sound-4.jpg" alt="text"></img>
          <div class="overlay">
             
             <a class="info" href="#">link here</a>
          </div>
      </div>
    </div>
  </div>
</div>