html - 使叠加框留在图片内部

标签 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>

最佳答案

首先制作图像容器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>

关于html - 使叠加框留在图片内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747840/

相关文章:

php - 替换重复的 html php 仅图像和文本更改

html - 在我的网站左侧制作粘性 whatsapp 图标

html - 删除选项卡面板中的空间

javascript - 每次打开 Bootstrap 模式时执行alert(),不起作用

javascript - bootstrap carousel - 当轮播滑动时,自动更新事件导航项

javascript - Twitter Bootstrap 3 保持 Tab 刷新

html - CSS 似乎在我的 HTML 文件中不起作用。怎么会?

html - 如何使复选框兼容 IE7 和 IE8? (使用 jQuery)

javascript - 在适用于所有浏览器和 iOS 的网站上录制音频

jquery - 按钮与输入和标签在同一行