CSS3 @keyframes 实现渐变动画

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>classList</title>

    <style>

        .orange{

            width: 250px;

            height: 200px;

            background-color:lawngreen;

            border: 1px dotted grey;

            animation-name: test;

            /* 动画完成一个周期所需要的时间 */

            animation-duration:2s;

            /* 开始前等待的时间 */

            animation-delay:1s;

            /* 播放次数 */

            animation-iteration-count:1;

        }

        @keyframes test {

            0% {

                width: 250px;

                height: 40px;

                background-color:orangered;

            }

            50% {

                width: 250px;

                height: 120px;

                background-color:gold;

            }

            100%{

                width: 250px;

                height: 200px;

                background-color:greenyellow;

            }

        }

        .orangered{

            animation-name: test2;

            animation-duration: 3s;

            animation-iteration-count: 1;

            animation-delay: 0s;

        }

        @keyframes test2 {

            0% {

                height: 160px;

                width: 250px;

                background-color:greenyellow;

                border: 1px dotted black;

            }

            50% {

                height: 80px;

                width: 250px;

                background-color:gold;

                border: 1px dotted black;

            }

            100%{

                height: 0px;

                width: 250px;

                background-color:orangered;

                border: 1px dotted black;

            }

        }

    </style>

</head>

<body>

    <button οnclick="btn()">点一下会渐变哦</button>

    <br/>

    <div class="orange"></div>

    <script>

        let orange = document.querySelector(".orange");

        

        function btn(){

            if(orange.classList.contains("orange")){

                orange.classList.remove("orange")

                orange.classList.add("orangered")

            }else{

                orange.classList.remove("orangered")

                orange.classList.add("orange")

            }

        }

    </script>

</body>

</html>

发布了28 篇原创文章 · 获赞 0 · 访问量 150

猜你喜欢

转载自blog.csdn.net/weixin_41813243/article/details/104831799