起始页动画

我正在尝试在首次加载时为我的主页创建动画。我想要我必须从页面底部升起的球,但是一旦它到达中间,用户就可以单击它并扩展到整个页面。我有这里的升球代码:


*, *::after, *::before {box-sizing: inherit;}


html{

    box-sizing: border-box;

}


body{

    margin: 0; 

    padding: 0;

    overflow: hidden;

}


.ball{

    background-color: #eb8c28; 

    width: 100px;

    height: 100px;

    border-radius: 0%;

    position: absolute;

    bottom: 0%;

    left: 50%;

    animation: rise;

    animation-duration: 2s;

    animation-iteration-count: 1;

    animation-fill-mode: forwards;

}


@keyframes rise{

    0%{

        border-radius: 50%;

    }

    100%{

        border-radius: 50%;

        transform:translateY(-100%);

    }


    75%{

        border-radius: 40%;

    }


    80%{

        border-radius: 30%;

    }


    90%{

        border-radius:20%;

    }

    100%{

        transform: scale(20,20);

    }

}

<!DOCTYPE html>

<html>

    <head>

        <link rel="stylesheet" type="text/css" href="ballcopy.css">

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

    </head>

    <body>

        <main>

                <div class="ball"></div>

        </main>

    </body>

</html>

但是,我坚持要如何将球缩放到整个页面。我应该创建另一个 div 并使其可点击,还是有办法创建一个可点击一半的动画以使用 JS 完成动画。



元芳怎么了
浏览 104回答 1
1回答

翻过高山走不出你

您可以使用 Javascript 向ball元素添加一个类(例如click),然后设置一个新动画,以便在设置该类后运行。它基本上将您的原始动画一分为二。// Get the ball elementlet ball = document.getElementsByClassName("ball");// First instance of the ball object, add a click listener.ball[0].addEventListener('click', (event) => {&nbsp; // add the click class&nbsp; ball[0].classList.add('click');});*,*::after,*::before {&nbsp; box-sizing: inherit;}html {&nbsp; box-sizing: border-box;}body {&nbsp; margin: 0;&nbsp; padding: 0;&nbsp; overflow: hidden;}.ball {&nbsp; background-color: #eb8c28;&nbsp; width: 100px;&nbsp; height: 100px;&nbsp; border-radius: 0;&nbsp; position: absolute;&nbsp; bottom: 0%;&nbsp; /* Added calc here to center the ball */&nbsp; left: calc(50% - 50px);&nbsp; animation: rise;&nbsp; animation-duration: 2s;&nbsp; animation-iteration-count: 1;&nbsp; animation-fill-mode: forwards;}.ball.click {&nbsp; animation: fill;&nbsp; animation-duration: 2s;&nbsp; animation-iteration-count: 1;&nbsp; animation-fill-mode: forwards;}@keyframes rise {&nbsp; 0% {&nbsp; &nbsp; border-radius: 50%;&nbsp; }&nbsp; 100% {&nbsp; &nbsp; border-radius: 50%;&nbsp; &nbsp; transform: translateY(-100%) scale(1);&nbsp; }}@keyframes fill {&nbsp; 0% {&nbsp; &nbsp; border-radius: 50%;&nbsp; &nbsp; transform: translateY(-100%) scale(1);&nbsp; }&nbsp; 100% {&nbsp; &nbsp; border-radius: 0;&nbsp; &nbsp; transform: translateY(-100%) scale(20);&nbsp; }}<!DOCTYPE html><html><head>&nbsp; <link rel="stylesheet" type="text/css" href="ballcopy.css">&nbsp; <meta name="veiwport" content="width=device-width, initial-scale=1.0"></head><body>&nbsp; <main>&nbsp; &nbsp; <div class="ball"></div>&nbsp; </main></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript