【问题标题】:Responsive Hover Box over Responsive Image响应式图像上的响应式悬停框
【发布时间】:2017-02-13 19:40:02
【问题描述】:

我一直在使用一个教程来制作一个悬停框来覆盖一组图像。
文章可以在here找到。

让它完美地工作,除了我希望我的图像和悬停响应窗口大小(只是通过宽度很好),我已经尝试查找如何做到这一点。似乎可能是使用 % 而不是固定值的情况,但没有足够的经验知道如何执行标记。即使我让图像重新调整大小,悬停框也不会随着它们重新调整大小。

是否可以在现有的 CSS 中添加一些东西来实现这一点。

ul.img-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
  text-align: center;
}

ul.img-list li {
  display: inline-block;
  height: 150px;
  margin: 0 1em 1em 0;
  position: relative;
  width: 150px;
}

span.text-content {
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display: table;
  height: 150px;
  left: 0;
  position: absolute;
  top: 0;
  width: 150px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}

span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

ul.img-list li:hover span.text-content {
  opacity: 1;
}
<ul class="img-list">
  <li>
    <a href="#">
      <img src="http://placehold.it/150x150">
      <span class="text-content"><span>Text</span></span>
    </a>
  </li>
</ul>

【问题讨论】:

  • @media-query :)

标签: javascript jquery html css hover


【解决方案1】:

您只能使用宽度来保持图像比例。

您可以将 display:block 用于 a 和 img,并使用 flex 将文本居中。

不太确定您要查找的响应行为,您可以在 li 上使用 % 宽度或混合使用 % width + min-width 和 max-width。

% 宽度设置为 50%(和最大/最小宽度)的示例,它可以是任何其他值和单位。

ul.img-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
  text-align: center;
}

ul.img-list li {
  display: inline-block;
  vertical-align:top;
  margin: 0;
  position: relative;
  width:50%;
  max-width:100vh;
  min-width:60vh;
}
ul.img-list li a, ul.img-list li a img {
  display:block;
  width:100%;
  }
span.text-content {
  background: rgba(0,0,0,0.5);
  color: white;
  cursor: pointer;
  display:flex;
  left: 0;
  right:0;
  position: absolute;
  top: 0;
  bottom:0;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}

span.text-content span {
  margin:auto;
}

ul.img-list li:hover span.text-content {
  opacity: 1;
}
* {
  margin:0;
  padding:0;
  }
<ul class="img-list">
  <li>
    <a href="#">
      <img src="http://placehold.it/150x150">
      <span class="text-content"><span>Text</span></span>
    </a>
  </li>
</ul>

【讨论】:

  • 这是一个很棒的萌芽,很好玩!!当你知道怎么做的时候就这么简单:)
  • 嘿,前几天这对我很有用。我在其他东西上使用相同的技术。只有这一次我希望文本位于右上角。即使我取出 display: flex 并将其更改为 text-align:right,我似乎也无法正确处理
  • @Robin.83 只需删除:span.text-content span { margin:auto; },然后在 .text-content 上使用 justify-content 和/或最终使用 text-align。也许你可以提出一个新问题 :) 你会接受其中一个回答吗?
  • 太好了,再次感谢您。工作了一个款待。我也接受了你的回答。对不起,我是stackoverflow的新手。我还在学习礼仪!
【解决方案2】:

您也可以这样做,使用伪元素在图像上显示叠加内容。这种方法是完全响应的。

.wrapper {
  width: 100%;
  overflow: auto;
  padding: 1%;
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

/* Columns floated left */
.col-4 {
  width: 33.3%;
  float: left;
  padding: 1%;
  box-sizing: border-box;
  height: auto;
  overflow: hidden;
}

/* Container to make absolute positioning easier on psuedo element */
.image_container {
  position: relative;
  overflow: auto;
  padding: 0;
  margin: 0;
}

.image_container img {
  width: 100%;
  display: block;
}

/* Structure for ::before element */
#img_1::before,
#img_2::before,
#img_3::before {
  background: rgba(0, 0, 0, 0.5);
  position: absolute;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  display: block;
  opacity: 0;
  color: white;
  padding: 10px;
  transition: 0.5s ease;
}

/* Hover state for container to show ::before on mouseover */
.image_container:hover#img_1::before,
.image_container:hover#img_2::before,
.image_container:hover#img_3::before {
  opacity: 1;
  cursor: pointer;
}

/* Text for ::before elements */
#img_1::before {
  content: 'Image Title 1';
}
#img_2::before {
  content: 'Image Title 2';
}
#img_3::before {
  content: 'Image Title 3';
}
<div class="wrapper">
  <div class="col-4">
    <div class="image_container" id="img_1">
      <img src="https://images.unsplash.com/reserve/B6PfiQ8QoSzmsZYOCkSB__DSC0530-1.jpg?dpr=1&auto=format&fit=crop&w=1500&h=1004&q=80&cs=tinysrgb&crop=">
    </div>
  </div>
  <div class="col-4">
    <div class="image_container" id="img_2">
      <img src="https://images.unsplash.com/photo-1418985991508-e47386d96a71?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=">
    </div>
  </div>
  <div class="col-4">
    <div class="image_container" id="img_3">
      <img src="https://images.unsplash.com/photo-1476362555312-ab9e108a0b7e?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=">
    </div>
  </div>
</div>

您还可以将图像设置为background-imagediv,如果需要,您可以更好地控制叠加层中的文本。

【讨论】:

  • 谢谢,我选择了下面的那个,只是因为它是对我已有的代码的微小调整。不过这看起来也不错!
  • @Robin.83 没问题!我建议您在空闲时间尝试此方法。很酷的事情是,当您调整窗口大小时,图像之间的间距(或设计中的排水沟)如何保持成比例。无论如何,祝你的项目好运!
猜你喜欢
  • 2017-03-30
  • 1970-01-01
  • 2013-12-03
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
相关资源
最近更新 更多