代码之家  ›  专栏  ›  技术社区  ›  John

防止在悬停CSS变换时闪烁

  •  1
  • John  · 技术社区  · 7 年前

    当鼠标悬停在垂直旋转的定位点上时,如果鼠标不靠近图像中心,则动画变换将使用以下代码闪烁:

    @keyframes spin {0% {transform: rotateX(0deg);} 100% {transform: rotateX(360deg);}}
    a:focus, a:hover {animation: spin 0.9s 1 linear;}
    

    https://jsfiddle.net/jabcreations/ahcx0wfv/

    2 回复  |  直到 7 年前
        1
  •  3
  •   Kosh    7 年前

    避免 :hover a 闪烁着你可能会移动 :悬停 致其父母:

    @keyframes spin {
      100% {
        transform: rotateX(360deg);
      }
    }
    
    ul {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
    }
    
    li:hover {
      cursor: pointer;
    }
    
    a:focus,
    li:hover a {
      animation: spin 0.9s 1 linear;
    }
    
    ul a {
      color: #000;
      display: inline-block;
      height: 50px;
    }
    
    ul a:focus,
    ul li:hover a {
      background-image: radial-gradient(ellipse at center, #777 0%, #222 100%);
      color: #fff;
    }
    <ul>
      <li><a href="admin/">Admin</a></li>
      <li><a href="appointments/">Appointments</a></li>
      <li><a href="blog/">Blog</a></li>
      <li><a href="calendar/">Calendar</a></li>
      <li><a href="contact/">Contact</a></li>
      <li><a href="events/">Events</a></li>
      <li><a href="forms/">Forms</a></li>
      <li><a href="forums/">Forums</a></li>
      <li><a href="guestbook/">Guestbook</a></li>
      <li><a href="mail/">Mail</a></li>
      <li><a href="members/">Members</a></li>
      <li><a href="newsletter/">Newsletter</a></li>
      <li><a href="notifications/">Notifications</a></li>
      <li><a href="search/">Search</a></li>
    </ul>
        2
  •  1
  •   Temani Afif    7 年前

    在不更改html的情况下,可以将动画移动到伪元素,这样可以避免丢失悬停,因为主元素不会旋转:

    p a {
      color: #000;
      display: inline-block;
      height: 50px;
      z-index:0;
      position:relative;
      color:transparent;
    }
    p a:before {
      content:attr(data-text);
      position:absolute;
      z-index:-1;
      top:0;
      left:0;
      right:0;
      bottom:0;
      color:#000;
      transition:transform 0.9s linear;
    }
    p a:focus::before,
    p a:hover::before {
      transform: rotateX(360deg);
      background: radial-gradient(ellipse at center, #777 0%, #222 100%);
      color: #fff;
    }
    <p>some content here <a href="admin/" data-text="Admin">Admin</a> and more here</p>