【问题标题】:Problem with pseudo elements after and before in csscss中前后伪元素的问题
【发布时间】:2020-05-15 07:34:46
【问题描述】:

目前,我正在尝试制作一个环绕链接的动画边框框,但是一旦我将鼠标悬停在鼠标上,动画仅适用于伪元素的一个实例,而不适用于两者。写在其他作品之上的实例。

我不知道这有什么问题,因为我也找不到问题所在。两个伪元素都获得了 Content 集,我真的不知道出了什么问题。

这里是代码

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】:

    对于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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-31
      • 2018-11-11
      • 2021-06-21
      • 2019-06-02
      • 2011-09-26
      • 2011-03-17
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多