【问题标题】:CSS text flying on a sine wave animationCSS 文本在正弦波动画上飞行
【发布时间】:2022-09-22 20:50:09
【问题描述】:

我正在做一个文本漂浮在正弦波旋转的波浪上的动画。我得到了正弦曲线本身。但是我在平滑旋转文本中的字母以使它们平滑地相互跟随时卡住了,如下面的示例所示。 文字本身上下浮动,转动时字母转动不顺畅。告诉我如何实现正确的字母轮换。

        html,
        body{
            margin: 0;
            padding: 0;
            background-color: black;
            font-family: Arial, Helvetica, sans-serif;
        }
        .text-anim-wrapper{
            position: relative;
            overflow: hidden;
            height: 150px;
        }

        .text-anim{
            position: absolute;
            left: calc(100% + 200px);
            width: 20px;
            font-size: 24px;
            color: white;
            font-weight: bold;
            transform: rotate(10deg);
            animation: flight-right-to-left 6s linear infinite, curve 6s ease infinite, curve-rotate 6s ease infinite;
        }
        .text-anim:nth-child(2) {
            animation-delay: .1s;
        }
        .text-anim:nth-child(3) {
            animation-delay: .2s;
        }
        .text-anim:nth-child(4) {
            animation-delay: .3s;
        }
        .text-anim:nth-child(5) {
            animation-delay: .4s;
        }
        .text-anim:nth-child(6) {
            animation-delay: .5s;
        }
        .text-anim:nth-child(7) {
            animation-delay: .6s;
        }
        .text-anim:nth-child(8) {
            animation-delay: .7s;
        }
        .text-anim:nth-child(9) {
            animation-delay: .8s;
        }

        @keyframes flight-right-to-left {
            0%{
                left: 100%;
            }
            100%{
                left: -20px;
            }
        }
        @keyframes curve {
            0%{
                top: 50px;
            }
            20%{
                top: 0;
            }
            50%{
                top: 80px;
            }
            85%{
                top: 0;
            }
            100%{
                top: 50px;
            }
        }
        @keyframes curve-rotate {
            25%{
                transform: rotate(-10deg);
            }
            50%{
                transform: rotate(0deg);
            }
            75%{
                transform: rotate(10deg);
            }
            100%{
                transform: rotate(-10deg);
            }
        }
    <div class=\"text-anim-wrapper\">
        <div class=\"text-anim\">V</div>
        <div class=\"text-anim\">I</div>
        <div class=\"text-anim\">C</div>
        <div class=\"text-anim\">T</div>
        <div class=\"text-anim\">O</div>
        <div class=\"text-anim\">R</div>
        <div class=\"text-anim\">I</div>
        <div class=\"text-anim\">N</div>
        <div class=\"text-anim\">E</div>
    </div>

预期结果example

    标签: html css animation text css-animations


    【解决方案1】:

    这种动画取决于屏幕宽度,所以在全屏页面运行以获得更好的效果:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="style.css" />
        <title>Document</title>
        <style>
           html,
            body{
                margin: 0;
                padding: 0;
                background-color: black;
                font-family: Arial, Helvetica, sans-serif;
            }
            .text-anim-wrapper{
                position: relative;
                overflow: hidden;
                height: 150px;
            }
    
            .text-anim{
                position: absolute;
                left: calc(100% + 200px);
                width: 20px;
                font-size: 24px;
                color: white;
                font-weight: bold;
                transform: rotate(10deg);
                animation: flight-right-to-left 16s linear infinite, curve 8s ease-in-out infinite, curve-rotate 8s ease-in-out infinite;
            }
            .text-anim:nth-child(1) {
                animation-delay: 0s;
            }
            .text-anim:nth-child(2) {
                animation-delay: .25s;
            }
            .text-anim:nth-child(3) {
                animation-delay: .5s;
            }
            .text-anim:nth-child(4) {
                animation-delay: .75s;
            }
            .text-anim:nth-child(5) {
                animation-delay: 1s;
            }
            .text-anim:nth-child(6) {
                animation-delay: 1.25s;
            }
            .text-anim:nth-child(7) {
                animation-delay: 1.5s;
            }
            .text-anim:nth-child(8) {
                animation-delay: 1.75s;
            }
            .text-anim:nth-child(9) {
                animation-delay: 2s;
            }
    
            @keyframes flight-right-to-left {
                0%{
                    left: 100%;
                }
                100%{
                    left: -20px;
                }
            }
            @keyframes curve {
                0%{
                    top: 50px;
                }
                25%{
                    top: 0;
                }
                50%{
                    top: 50px;
                }
                75%{
                    top: 0;
                }
                100%{
                    top: 50px;
                }
            }
            @keyframes curve-rotate {
                0% {
                  transform: rotate(0deg);
                }
                12.5% {
                  transform: rotate(25deg);
                }
                25%{
                    transform: rotate(0deg);
                }
                37.5% {
                  transform: rotate(-25deg);
                }
                50%{
                    transform: rotate(0deg);
                }
                62.5% {
                  transform: rotate(25deg);
                }
                75%{
                    transform: rotate(0deg);
                }
                87.5% {
                  transform: rotate(-25deg);
                }
                100%{
                    transform: rotate(0deg);
                }
            }
        </style>
      </head>
      <body>
        <div class="text-anim-wrapper">
          <div class="text-anim">V</div>
          <div class="text-anim">I</div>
          <div class="text-anim">C</div>
          <div class="text-anim">T</div>
          <div class="text-anim">O</div>
          <div class="text-anim">R</div>
          <div class="text-anim">I</div>
          <div class="text-anim">N</div>
          <div class="text-anim">E</div>
        </div>
    
        <script></script>
      </body>
    </html>

    要自定义宽度更小的动画,只需更改时间: 从右到左飞行16s线性无限,曲线8s缓入 无限,曲线旋转8sease-in-out 无限

    如果你想改变字母之间的空间:改变动画延迟 每个孩子.text-anim:nth-child()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多