【问题标题】:Using CSS transition on ::before pseudo element在 ::before 伪元素上使用 CSS 过渡
【发布时间】:2017-01-22 04:15:07
【问题描述】:

.posts .img-hover:before {
  content: '';
  display: block;
  opacity: 0;
  -webkit-transition: opacity 1.2s ease;
  -moz-transition: opacity 1.2s ease;
  -ms-transition: opacity 1.2s ease;
  -o-transition: opacity 1.2s ease;
  transition: opacity 1.2s ease-out;
}
.posts .img-hover:hover:before {
  content: '';
  display: block;
  background: url("img/Texture1.png");
  width: 320px;
  /* image width */
  height: 220px;
  /* image height */
  position: absolute;
  top: 13px;
  right: 2px;
  opacity: 1;
}
<div class="posts">
  <a href="#">
    <h2 class="postname"> 
         Travel Flashback #1 </h2>
  </a>
  <a class="img-hover" href="#">
    <img width="960" height="720" src="http://.." class="img-hover" alt="" />
  </a>
</div>

我对这段代码有一个问题。如您所见,我想要过渡到伪元素 ::before,它具有 bkg img。

当我悬停时,过渡工作顺利,但是当我离开鼠标时,bkg img 立即消失而没有过渡。

你能推荐点什么吗?

【问题讨论】:

    标签: css css-transitions pseudo-element


    【解决方案1】:

    对于浏览这个论坛的其他人,我来到这个帖子时遇到了完全相同的问题,我试图从

    切换过渡焦点
    opacity 0.35s ease-in-out
    

    到:

    all 0.35s ease-in-out
    

    问题已解决。 我的浏览器是 Chromium 版本 80.0.3987.162,Debian Linux 10.4

    【讨论】:

      【解决方案2】:

      在悬停时,您可能只想要与过渡相关的 css,而不是伪元素的实际样式。试试这个

      .posts .img-hover:before {
          content: '';
          display: block;
          background: url("img/Texture1.png");
          width: 320px; /* image width */
          height: 220px; /* image height */
          position: absolute;
          top: 13px;
          right: 2px;
          opacity: 0;
          -webkit-transition: opacity 1.2s ease;
          -moz-transition: opacity 1.2s ease;
          -ms-transition: opacity 1.2s ease;
          -o-transition: opacity 1.2s ease;
          transition:  opacity 1.2s ease-out;
      }
      .posts .img-hover:hover:before{
          opacity: 1;
      }
      

      【讨论】:

      • 我遇到了完全相反的问题(即opacity: 1 突然出现在after 伪元素上,但根据transition 淡出),尽管应用了计时功能和延迟。知道为什么吗?
      猜你喜欢
      • 1970-01-01
      • 2012-04-05
      • 2012-05-21
      • 1970-01-01
      • 2018-09-07
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      相关资源
      最近更新 更多