在CSS3悬停'之后'伪元素中使链接可点击

时间:2015-01-30 09:44:15

标签: css css3

我根据this tutorial设置了一些图像悬停样式,使用beforeafter伪元素创建悬停样式。

工作(和外观)很棒,但我需要能够在悬停内容中插入按钮/链接,但由于这是在伪元素上设置样式,after阻止了按钮/链接点击。

如下所示,我设置了一个jsfiddle来展示我想要做的事情 - 正如您将注意到的那样,链接在悬停时无法点击,我想知道是否有人可以建议一种简单的方法,使这个链接可以点击,而不必完全改变它的结构方式。

默认状态

enter image description here

悬停状态

enter image description here

.media {
  display: inline-block;
  position: relative;
  vertical-align: top;
}

.media__image { display: block; }

.media__body {
  background: rgba(41, 128, 185, 0.7);
  bottom: 0;
  color: white;
  font-size: 1em;
  left: 0;
  opacity: 0;
  overflow: hidden;
  padding: 3.75em 3em;
  position: absolute;
  text-align: center;
  top: 0;
  right: 0;
  -webkit-transition: 0.6s;
  transition: 0.6s;
}

.media__body:hover { opacity: 1; }

.media__body:after,
.media__body:before {
  border: 1px solid rgba(255, 255, 255, 0.7);
  bottom: 1em;
  content: '';
  left: 1em;
  opacity: 0;
  position: absolute;
  right: 1em;
  top: 1em;
  -webkit-transform: scale(1.5);
  -ms-transform: scale(1.5);
  transform: scale(1.5);
  -webkit-transition: 0.6s 0.2s;
  transition: 0.6s 0.2s;
}

.media__body:before {
  border-bottom: none;
  border-top: none;
  left: 2em;
  right: 2em;
}

.media__body:after {
  border-left: none;
  border-right: none;
  bottom: 2em;
  top: 2em;
}

.media__body:hover:after,
.media__body:hover:before {
  -webkit-transform: scale(1);
  -ms-transform: scale(1);
  transform: scale(1);
  opacity: 1;
}

.media__body h2 { margin-top: 0; }

.media__body p { margin-bottom: 1.5em; }
<div class="media">
    <img class="media__image" src="http://lorempixel.com/400/300/animals" alt="" />
    <div class="media__body">
        <h2>Cool Title</h2>
        <p>Lorem ipsum dolor sit amet, <a href="/mylink">consectetur adipisicing</a> elit. Nesciunt laboriosam voluptatem necessitatibus cum, tenetur repellat, eaque eos debitis! Quaerat.</p>
    </div>
</div>

2 个答案:

答案 0 :(得分:6)

CSS3 - 仅限(实际移入CSS4规范)解决方案是pointer-events属性,例如

.media__body:hover:after,
.media__body:hover:before {
  ...
  pointer-events: none;
}

支持all modern browser,但仅限于IE11(关于HTML / XML内容)

示例:http://jsfiddle.net/8uz7mx6h/


另一个解决方案,也支持旧IE,是将position: relativez-index一起应用于该段落,例如

.media__body p { 
  margin-bottom: 1.5em; 
  position: relative;
  z-index: 1;
}

示例:http://jsfiddle.net/0nrbjwmg/2/

答案 1 :(得分:1)

.media__body p { margin-bottom: 1.5em; 
position: relative;
z-index:999;

}

试试这个!

相关问题