Home > Mobile >  CSS text flying on a sine wave animation
CSS text flying on a sine wave animation

Time:09-23

I'm doing an animation of text floating on waves with a sine wave rotation. I got the sinusoid itself. But I got stuck at the moment of smoothly rotating the letters in the text so that they smoothly follow each other, as in the example that I attached below. The text itself floats up and down, and the letters do not turn smoothly when turning. Tell me how to implement the correct rotation of letters.

        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 >
        <div >V</div>
        <div >I</div>
        <div >C</div>
        <div >T</div>
        <div >O</div>
        <div >R</div>
        <div >I</div>
        <div >N</div>
        <div >E</div>
    </div>

Expected result example

CodePudding user response:

This kind of animation depends on screen width, so run this in full screen page to get better result:

<!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 >
      <div >V</div>
      <div >I</div>
      <div >C</div>
      <div >T</div>
      <div >O</div>
      <div >R</div>
      <div >I</div>
      <div >N</div>
      <div >E</div>
    </div>

    <script></script>
  </body>
</html>

To customize animation for less width just change timing: flight-right-to-left 16s linear infinite, curve 8s ease-in-out infinite, curve-rotate 8s ease-in-out infinite

If you want change space between letters: change animation delay for each child .text-anim:nth-child()

  • Related