【问题标题】:Making link clickable within a CSS3 hover 'after' pseudo element在 CSS3 悬停“之后”伪元素中使链接可点击
【发布时间】:2015-01-30 09:44:15
【问题描述】:

我已经基于this tutorial 设置了一些图像悬停样式,它使用beforeafter 伪元素创建悬停样式。

工作(并且看起来)很棒,但我需要能够在悬停内容中插入按钮/链接,但由于这是在伪元素上设置的,after 阻止了按钮/链接可点击。

如下所示,我设置了一个 jsfiddle 来演示我想要做什么 - 你会注意到,悬停时无法点击链接,我想知道是否有人可以建议一种无需完全改变其结构即可使该链接可点击的简单方法。

默认状态

悬停状态

.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>

【问题讨论】:

    标签: css


    【解决方案1】:

    CSS3-only(实际上已移至 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/

    【讨论】:

    • 天哪,我尝试了 z-index,但它没有工作,但显然我犯了一个错误,因为它完全符合我的预期。非常感谢:)
    【解决方案2】:
    .media__body p { margin-bottom: 1.5em; 
    position: relative;
    z-index:999;
    

    }

    试试这个!

    【讨论】:

    • 谢谢,工作得很好——虽然先到了,但还是接受了其他答案。
    • 恭喜!很好。
    猜你喜欢
    • 2020-11-29
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2012-12-16
    • 2015-04-20
    • 2019-07-31
    相关资源
    最近更新 更多