如何使叠加元素响应?

时间:2017-10-21 08:13:07

标签: css twitter-bootstrap thumbnails

我在bootstrap 3中有一个缩略图视频库。每个缩略图都有一个鼠标悬停时出现的文本覆盖。这一切都很好,但我现在正在尝试向叠加层添加播放图标。问题是,虽然原始文本缩略图是响应的,但播放图标不是。我有什么问题?

这是fiddle

<div class="container">
  <div class="row">
    <div class='col-sm-3 col-xs-6 col-md-3 col-lg-3 padding-0'>

      <div class="thumbnail">
        <a class="fancybox-media" data-fancybox-type="iframe" href="https://www.youtube.com/embed/PVob_tATVRI">

          <div class="caption">
            <h4 class="">Richard Feynman</h4>
            <p class=""> Watch Him</p>
          </div>
          <!-- /.caption-->
          <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR-JZQIhP_M6qtpPy4Hih-LsyGSBe5m7OlaRi5INdHVGy-bJRYIUg" alt="" class="img-responsive"></a>
      </div>
      <!-- /.thumb-->
    </div>
    <!-- /.col -->
  </div>
  <!--end row-->
</div>
<!-- /.container -->

CSS

.thumbnail {
  position: relative;
  overflow: hidden;
  background: transparent;
  border: none;
}

.caption {
  position: absolute;
  top: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.75);
  width: 100%;
  height: 100%;
  padding: 2%;
  display: none;
  text-align: left;
  color: #fff !important;
  z-index: 2;
  /*This is the play icon I ant to be responsive */
  background: transparent url(http://www.oceania-attitude.com/applications/site/views/oceania/images/icons/play-video.jpg) no-repeat center;
}

的jQuery

$("[rel='tooltip']").tooltip();
$(document).ready(function() {
  $('.thumbnail').hover(
    function() {
      $(this).find('.caption').slideDown(250); //.fadeIn(250)
    },
    function() {
      $(this).find('.caption').slideUp(250); //.fadeOut(205)
    }
  );

});

1 个答案:

答案 0 :(得分:1)

您正在设置背景图片,购买时未指定尺寸,因此默认为完整尺寸。

您可以使用百分比设置相对于容器大小的background-size,例如以下将使图像的容量减半:

background-size: 50%;

在您的情况下,如果您希望图像填充容器,您可以将背景设置为100%,例如

background: transparent url([your url]/play-video.jpg) no-repeat center;
background-size: 100%;

工作代码段

/* Latest compiled and minified JavaScript included as External Resource */
$("[rel='tooltip']").tooltip();
$(document).ready(function() {
  $('.thumbnail').hover(
    function() {
      $(this).find('.caption').slideDown(250); //.fadeIn(250)
    },
    function() {
      $(this).find('.caption').slideUp(250); //.fadeOut(205)
    }
  );

});
.thumbnail {
  position: relative;
  overflow: hidden;
  background: transparent;
  border: none;
}

.caption {
  position: absolute;
  top: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.75);
  width: 100%;
  height: 100%;
  padding: 2%;
  display: none;
  text-align: left;
  color: #fff !important;
  z-index: 2;
  background: transparent url(http://www.oceania-attitude.com/applications/site/views/oceania/images/icons/play-video.jpg) no-repeat center;
  background-size: 100%;
}

/* Added for tesing because your img-responsive class isn't working */
img {  width: 100%;  height: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class='col-sm-3 col-xs-6 col-md-3 col-lg-3 padding-0'>

      <div class="thumbnail">
        <a class="fancybox-media" data-fancybox-type="iframe" href="https://www.youtube.com/embed/PVob_tATVRI">

          <div class="caption">
            <h4 class="">Richard Feynman</h4>
            <p class="">

              Watch Him</p>
          </div>
          <!-- /.caption-->
          <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR-JZQIhP_M6qtpPy4Hih-LsyGSBe5m7OlaRi5INdHVGy-bJRYIUg" alt="" class="img-responsive">
        </a>
      </div>
      <!-- /.thumb-->
    </div>
    <!-- /.col -->

  </div>
  <!--end row-->

</div>
<!-- /.container -->


注意:

这使得背景图像成为容器的100%,即使容器大于原始图像大小。在您的情况下,您已将视频放入Bootstrap cols中,因此它不应成为问题,但如果它确实成为问题,您只需使用媒体查询来更改百分比值以适应。

示例:使背景图像响应,直到屏幕大小达到768px,然后将其限制为固定大小:

.caption {
    background: transparent url([your url]/play-video.jpg) no-repeat center;
    background-size: 100%;
}

@media (min-width: 768px){
    .caption {
        background-size: 400px 300px;
    }
}