search

Home  >  Q&A  >  body text

css - Can @keyframes accept parameters?

@keyframes around {
    from {
      margin-left: 100%;
    }
    to {
      margin-left: -5em;
    }
  }

The -5em should be passed in as a parameter? Can it be solved?

大家讲道理大家讲道理2840 days ago1097

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-06-16 09:21:25

    Set custom CSS properties combined with the var function. The custom properties start with the -- symbol
    Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
        <style>
            @keyframes around {
                from {
                    margin-left: 100%;
                }
                to {
                    margin-left: var(--margin-left);
                }
            }
    
            .main{
                position: absolute;
                left:0p;
                top:0px;
                width: 200px;
                height: 200px;
                background-color: #7C2845;
                color:#ffffff;
                --margin-left:-5em;
    
            }
    
            .main.active{
                animation-name: around;
                animation-duration: 1s;
                animation-direction: alternate;
                animation-iteration-count: infinite;
            }
        </style>
    </head>
    <body>
        <p class="main active">
            TEST
        </p>
    </body>
    </html>

    reply
    0
  • Cancelreply