如何修复hover :: after和内部过渡无效?

时间:2019-10-24 07:43:36

标签: css hover pseudo-element

我正在尝试使用::aftertransition在按钮上添加炫酷效果。但, 它不符合我的预期。

我在scss中进行了以下更改:

    .btn{
       &:link,
       &:visited{
       text-transform: uppercase;
       text-decoration: none;
       padding: 1.5rem 4rem;
       display: inline-block;
       border-radius: 10rem;
       transition: all .2s;
       position: relative;
       font-size: 1.6rem;
      }
      &:hover{
       transform: translateY(-3px);
       box-shadow: 0 1rem 2rem rgba($color-black,.2);
        &::after{
          transform: scaleX(1.4) scaleY(1.6);
          opacity: 0;
       } }
     &::after{
      content: "";
      display: inline-block;
      height: 100%;
      width: 100%;
      border-radius: 10rem;
      position: absolute;
      top: 0;
      left: 0;
      z-index: -1;
      transition: all .4s;
     }
}

&hover可以正常工作,并且可以提供适当的效果。但, hover::after不起作用,我看不到很酷的效果。为什么?

1 个答案:

答案 0 :(得分:0)

我认为这是您要达到的效果。您需要为&::after

添加背景色

.btn, .btn:link, .btn:visited {
   text-transform: uppercase;
   text-decoration: none;
   padding: 1.5rem 4rem;
   display: inline-block;
   border-radius: 10rem;
   transition: all .2s;
   position: relative;
   font-size: 1.6rem;
  }
.btn:hover{
   transform: translateY(-3px);
   box-shadow: 0 1rem 2rem rgba($color-black,.2);
}
.btn:hover:after{
      transform: scaleX(1.4) scaleY(1.6);
      opacity: 0;
}
.btn:after{
  content: "";
  display: inline-block;
  background-color: red;
  height: 100%;
  width: 100%;
  border-radius: 10rem;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  transition: all .4s;
 }
<a class="btn">Button</a>