图和重叠段落中的可点击图像

时间:2018-09-04 06:55:08

标签: html css z-index

我无法使我的图片可点击。我有一个<figure>,包裹在图片和一些段落元素上。我也有一些CSS,这样当您将鼠标悬停在该图上时,这些段落就会一次从底部(屏幕外)过渡到顶部。我认为这种动画使我无法使用将图像包装在锚定标签中的常规方法,就像这个问题:a href link on img

不幸的是,我已经在这种特定的HTML配置上花了很多功夫,所以我不确定是否可以彻底地重新排列DOM元素。 尽可能,我想将图形,图像和段落保留在当前配置中,然后解决。这是一个演示:

figure img {
  width: 300px;
  height: 300px;
}

figure h2 {
  max-width: 235px;
}

figure {
  border: 2px solid black;
  margin-bottom: 0;
  margin-top: -2px;
  margin-right: -40px;
  position: relative;
  z-index: 1;
  display: inline-block;
  overflow: hidden;
  text-align: center;
}

figure figcaption {
  padding: 2em;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

figure figcaption::before,
figure figcaption::after {
  pointer-events: none;
}

figure figcaption {
  position: absolute;
  top: 0px;
  left: 0;
  width: 100%;
  height: 100%;
}

figure p {
  font-family: Play;
  font-size: 20px;
  max-width: 235px;
  height: auto;
  overflow: hidden;
  position: relative;
  opacity: 0;
  bottom: -110%;
}

figure:hover h2 {
  opacity: 0;
  -webkit-transition: opacity 0.95s, -webkit-transform 0.95s;
  transition: opacity 0.95s, transform 0.95s;
}

figcaption:hover p:nth-of-type(1) {
  transition: 1s;
  bottom: 70%;
  opacity: 1;
}

figcaption:hover p:nth-of-type(2) {
  bottom: 70%;
  opacity: 1;
  transition: 1s;
  transition-delay: .3s;
}

figcaption:hover p:nth-of-type(3) {
  bottom: 70%;
  opacity: 1;
  transition: 1s;
  transition-delay: .6s;
}

figure:hover .border-rect {
  opacity: 0;
  -webkit-transition: opacity 0.5s;
  transition: opacity 0.5s;
}

figure.effect img {
  -webkit-transition: opacity 0.35s;
  transition: opacity 0.35s;
}

figure.effect:hover img {
  opacity: 0.4;
}

figure.effect figcaption::before,
figure.effect figcaption::after {
  position: absolute;
  top: 30px;
  right: 30px;
  bottom: 30px;
  left: 30px;
  content: '';
  opacity: 0;
  -webkit-transition: opacity 0.35s, -webkit-transform 0.35s;
  transition: opacity 0.35s, transform 0.35s;
}

figure.effect figcaption::before {
  border-top: 1px solid #fff;
  border-bottom: 1px solid #fff;
  -webkit-transform: scale(0, 1);
  transform: scale(0, 1);
}

figure.effect figcaption::after {
  border-right: 1px solid #fff;
  border-left: 1px solid #fff;
  -webkit-transform: scale(1, 0);
  transform: scale(1, 0);
}

figure.effect h2 {
  opacity: 1;
  -webkit-transition: opacity 0.95s, -webkit-transform 0.95s;
  transition: opacity 0.95s, transform 0.95s;
  -webkit-transition: -webkit-transform 0.35s;
  transition: transform 0.35s;
  -webkit-transform: translate3d(0, -20px, 0);
  transform: translate3d(0, -20px, 0);
  padding-top: 30%;
  max-width:235px;
}

figure.effect:hover figcaption::before,
figure.effect:hover figcaption::after {
  opacity: 1;
  -webkit-transform: scale(1);
  transform: scale(1);
}

figure:hover h2 {
  opacity: 0;
  -webkit-transition: opacity 0.5s;
  transition: opacity 0.5s;
}
    <figure class="effect">
      <img src="http://en.wikipedia.org/wiki/Mountain#/media/File:Lewis_overthrust_fault_nh10f.jpg">
      <figcaption>
        <a href="www.the-image-url.com/">
        <h2>Hover Somewhere Around Here
        </h2>
        <p>paragraph paragraph paragraph paragraph paragraph paragraph </p>
        <p>So you think you can hover, huh?</p>
        <p>paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph</p>
        </a>
      </figcaption>
    </figure>

问题

我无法完全说明为什么我无法获得可点击的图像。我还尝试将整个图形包装在<a>中,但是它不起作用。我的目标是:如果用户单击图形上的任意位置,该链接将称为我不想牺牲任何其他当前可用的元素。这是可能吗?如果是这样怎么办?

我的猜测是我元素的分层或z-index不允许这样做,但我希望可以解决此问题。

1 个答案:

答案 0 :(得分:2)

您有被覆盖的Overlay覆盖的Image标记,并且您已经给出了指向问题图像的Give链接。您可以在叠加层元素中拥有链接

<figure class="effect">

      <img src="http://en.wikipedia.org/wiki/Mountain#/media/File:Lewis_overthrust_fault_nh10f.jpg">

      <figcaption>
      <a href="www.the-image-url.com/">
        <h2>Hover Somewhere Around Here
        </h2>
        <p>paragraph paragraph paragraph paragraph paragraph paragraph </p>
        <p>So you think you can hover, huh?</p>
        <p>paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph</p>
        </a>
        <a href="#">View more</a>
      </figcaption>
    </figure>

编辑:添加/更新以下CSS

figcaption a {
    top: 0px;
    left: 0;
    width: 100%;
    height: 100%;
    text-decoration: none;
    max-width: 300px;
    color: #333;
    position: absolute;
    right: 0px;
    text-align: center;
}
figure.effect h2 {
    opacity: 1;
    -webkit-transition: opacity 0.95s, -webkit-transform 0.95s;
    transition: opacity 0.95s, transform 0.95s;
    -webkit-transition: -webkit-transform 0.35s;
    transition: transform 0.35s;
    -webkit-transform: translate3d(0, -20px, 0);
    transform: translate3d(0, -20px, 0);
    padding-top: 30%;
    max-width: 235px;
    margin: auto;
}
figure p {
    font-family: Play;
    font-size: 20px;
    max-width: 235px;
    height: auto;
    overflow: hidden;
    position: relative;
    opacity: 0;
    bottom: -110%;
    margin: auto;
}