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

CSS呼吸<按钮>停止文本抖动

  •  6
  • Fjott  · 技术社区  · 6 年前

    我呼吸急促 click me 下面的按钮,我正在使用 @keyframes

    但正如你所知 文本在呼吸动画中抖动。

    有没有办法避免这种情况发生?

    .centered {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) 
    }
    
    button.circle {
      --startSize: 65vh;
      --endSize: 85vh;
      width: var(--startSize);
      height: var(--startSize);
      background: teal;
      border-radius: 100%;
      animation-name: breathe;
      animation-play-state: running;
      animation-duration: 4.5s;
      animation-iteration-count: infinite;
      animation-direction: alternate;
        border: none;
    
    }
    
    @keyframes breathe {
      
      0% {
        width: var(--startSize); 
        height: var(--startSize);
      }
      
      25% {
        width: var(--startSize); 
        height: var(--startSize);
      }
    
      75% {
        width: var(--endSize);
        height: var(--endSize);
      }
      
      100% {
        width: var(--endSize);
        height: var(--endSize);
      }
    }
    <a href="https://www.w3schools.com"><button class="circle centered">Click me</button></a>
    2 回复  |  直到 6 年前
        1
  •  5
  •   chazsolo    6 年前

    也许更好的动画制作方法是使用 transform: scale(...) ::after 伪元素。使用此方法,我们可以获得以下好处:

    1. 动画不再影响文档流 1. . 制作动画时, prefer transform and opacity over properties like width or height . 后者将影响其周围的元素(文档流)。变换纯粹是视觉的-它们在放置方面对其他元素没有影响,这意味着 improved performance .

    .centered {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) 
    }
    
    button.circle {
      width: 65vh;
      height: 65vh;
      border: 0;
      background-color: transparent;
    }
    
    button.circle::after {
      z-index: -1;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      content: '';
      display: block;
      background: teal;
      border-radius: 100%;
      animation: breathe 4.5s ease infinite alternate running;
    }
    
    @keyframes breathe {
      from { transform: scale(1); }
      to { transform: scale(1.4); }
    }
    <a href="https://www.w3schools.com"><button class="circle centered">Click me</button></a>

    注: Browser support for this method


    1.我意识到按钮是居中的 absolute 这意味着它一开始不会影响文档流。这就是说,这种使用变换设置动画的方法对于两种场景都更灵活。

        2
  •  0
  •   Turnip Moushumi Ahmed    6 年前

    问题在于用于使按钮居中的变换属性。我使用grid属性将一个JSFIDLE放在一起,使按钮水平和垂直居中,从而停止文本抖动。

    body,
    html {
      height: 100%;
      display: grid;
    }
    
    .circle-outer {
      margin: auto;
      text-align: center;
    }
    
    button.circle {
      --startSize: 65vh;
      --endSize: 85vh;
      width: var(--startSize);
      height: var(--startSize);
      background: teal;
      border-radius: 100%;
      animation-name: breathe;
      animation-play-state: running;
      animation-duration: 4.5s;
      animation-iteration-count: infinite;
      animation-direction: alternate;
      border: none;
    }
    
    @keyframes breathe {
      0% {
        width: var(--startSize);
        height: var(--startSize);
      }
      25% {
        width: var(--startSize);
        height: var(--startSize);
      }
      75% {
        width: var(--endSize);
        height: var(--endSize);
      }
      100% {
        width: var(--endSize);
        height: var(--endSize);
      }
    }
    <div class="circle-outer">
      <a href="https://www.w3schools.com"><button class="circle">Click me</button></a>
    </div>

    还有一个工作示例:

    https://jsfiddle.net/WebDevelopWolf/7ujm2L5v/11/