在溢出框中添加叠加到图像

时间:2015-12-27 14:55:05

标签: html css css3 overflow

每张图片都应该有一个透明的黑色覆盖层,上面有一个白色数字,如“100”,“200”等。这个数字表示解锁图片所需的点数。我怎样才能做到这一点?

JSFiddle

HTML

<div class="wrapper">
  <div class="scrolls">


 <img src="http://placehold.it/150x150" style="height:100px"/>
 <img src="http://placehold.it/150x150" style="height:100px"/>
  <img src="http://placehold.it/150x150" style="height:100px"/>
   <img src="http://placehold.it/150x150" style="height:100px"/>
    <img src="http://placehold.it/150x150" style="height:100px"/>
     <img src="http://placehold.it/150x150" style="height:100px"/>
      <img src="http://placehold.it/150x150" style="height:100px"/>
       <img src="http://placehold.it/150x150" style="height:100px"/>
</div>
  </div>

CSS

.wrapper { 
        background:#f4f4f4; 
        margin: auto; 
        text-align: center; 
        position: relative;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
        margin-bottom: 20px !important;
        width: 550px;
        padding-top: 5px;
    }
    .scrolls { 
        overflow-x: scroll;
        overflow-y: hidden;
        height: 150px;
    white-space:nowrap
    } 
    .imageDiv img { 

        margin: 2px;
        max-height: 150px;
        cursor: pointer;
    display:inline-block;
    *display:inline;
    *zoom:1;
    vertical-align:top;
    }

1 个答案:

答案 0 :(得分:0)

您需要将每个图像和叠加层包装在单个div中。然后将覆盖图绝对定位在图像上。

&#13;
&#13;
.wrapper {
  background: #f4f4f4;
  margin: auto;
  text-align: center;
  position: relative;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  margin-bottom: 20px !important;
  width: 550px;
  padding-top: 5px;
}
.scrolls {
  overflow-x: scroll;
  overflow-y: hidden;
  height: 150px;
  white-space: nowrap;
  padding-bottom: 25px;
}
.imageDiv {
  margin: 2px;
  max-height: 150px;
  cursor: pointer;
  display: inline-block;
  *display: inline;
  *zoom: 1;
  vertical-align: top;
  position: relative;
}
.imageDiv .overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.25);
  line-height: 150px;
  color: transparent;
  font-size: 2em;
  text-align: center;
  transition: all .5s ease;
}
.imageDiv:hover .overlay {
  background: rgba(0, 0, 0, 0.55);
  color: white;
  ;
}
.imgaDiv img {
  display: block;
}
&#13;
<div class="wrapper">
  <div class="scrolls">

    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>
    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>
    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>
    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>
    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>
    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>
    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>
    <div class="imageDiv">
      <img src=" http://placehold.it/150x150 " />
      <div class="overlay">100</div>
    </div>

  </div>
</div>
&#13;
&#13;
&#13;

对于细化/增强,你可以取消实际的叠加div并使用伪元素代替......如果它只是样式。技术保持不变。