css中前后有伪元素的问题

问题描述 投票:0回答:1

[当前,我试图制作一个动画链接框,但当我将鼠标悬停在鼠标上时,动画仅适用于一个伪元素实例,而不能同时适用于这两个元素。在其他作品之上编写的实例。

我也不知道这是怎么回事,因为我也找不到问题。这两个伪元素都设置了内容集,我真的不知道出什么问题了。

这里是代码

nav a {
    text-decoration: none;
    color: white;
    width: 100px;
    height: 50px;
    padding: 25px;
    position: relative;
}

nav a::before {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    padding: 0;
    background: transparent;
    border: 2px transparent solid;
}

nav a:hover::before {
    animation: animate 0.5s linear forwards;
}

@keyframes animate 
{
    0% {
        width: 0;
        height: 0;
        border-top-color: white;
        border-right-color: transparent;
        border-bottom-color: transparent;
        border-left-color: transparent;
    }

    50% {
        width: 100%;
        height: 0;
        border-top-color: white;
        border-right-color: white;
        border-bottom-color: transparent;
        border-left-color: transparent;
    }

    100% {
        width: 100%;
        height: 100%;
        border-top-color: white;
        border-right-color: white;
        border-bottom-color: transparent;
        border-left-color: transparent;

} 

nav a::after {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    padding: 0;
    background: transparent;
    border: 2px transparent solid;
} 

nav a:hover::after {
    animation: animate2 0.5s linear forwards;
}

@keyframes animate2 
{
    0% {
        width: 0;
        height: 0;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: transparent;
        border-left-color: white;
    }

    50% {
        width: 0%;
        height: 100%;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: white;
        border-left-color: white;
    }

    100% {
        width: 100%;
        height: 100%;
        border-top-color: transparent;
        border-right-color: transparent;
        border-bottom-color: white;
        border-left-color: white;
}
html css css-selectors pseudo-element
1个回答
1
投票

对于animateanimate2,您都缺少100%代码块后的右括号。

放置它们使CSS对我有用。

例如:

html {
  background: #333;
}

nav a {
  text-decoration: none;
  color: white;
  width: 100px;
  height: 50px;
  padding: 25px;
  position: relative;
}

nav a::before {
  content: "";
  position: absolute;
  top: -2px;
  left: -2px;
  width: 0;
  height: 0;
  padding: 0;
  background: transparent;
  border: 2px transparent solid;
}

nav a:hover::before {
  animation: animate 0.5s linear forwards;
}

@keyframes animate {
  0% {
    width: 0;
    height: 0;
    border-top-color: white;
    border-right-color: transparent;
    border-bottom-color: transparent;
    border-left-color: transparent;
  }
  50% {
    width: 100%;
    height: 0;
    border-top-color: white;
    border-right-color: white;
    border-bottom-color: transparent;
    border-left-color: transparent;
  }
  100% {
    width: 100%;
    height: 100%;
    border-top-color: white;
    border-right-color: white;
    border-bottom-color: transparent;
    border-left-color: transparent;
  }
}
  nav a::after {
    content: "";
    position: absolute;
    top: -2px;
    left: -2px;
    width: 0;
    height: 0;
    padding: 0;
    background: transparent;
    border: 2px transparent solid;
  }
  nav a:hover::after {
    animation: animate2 0.5s linear forwards;
  }
  @keyframes animate2 {
    0% {
      width: 0;
      height: 0;
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: transparent;
      border-left-color: white;
    }
    50% {
      width: 0%;
      height: 100%;
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: white;
      border-left-color: white;
    }
    100% {
      width: 100%;
      height: 100%;
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: white;
      border-left-color: white;
    }
    }
<nav>
  <a>TEST</a>
</nav>
© www.soinside.com 2019 - 2024. All rights reserved.