CSS3动画实践——简易牛顿摆

最近在练习CSS3的关键帧动画(keyframes),于是做了一个简单的牛顿摆(听名字可能陌生,但你一定见过它):



先上代码(老版本IE可能存在兼容性问题):
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        html,
        body {
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        .main-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;
            background-color: #d7c8d0;
        }

        .swing-container {
            width: 500px;
            height: 400px;
            display: flex;
            flex-wrap: wrap;
        }

        .roof {
            width: 100%;
            height: 20px;
            background-color: #18619b;
        }

        .ball-wrapper {
            flex-basis: 100px;
            height: 380px;
            display: flex;
            align-items: center;
            flex-direction: column;
        }

        .ball {
            height: 100px;
            width: 100px;
            text-align: center;
            line-height: 90px;
            border-radius: 50px;
            background-color: #edcb66;
        }

        .rope {
            background: black;
            width: 2px;
            height: 280px;
        }

        .left-ball.ball-wrapper {
            transform-origin: center top;
            animation: left-ball-swing 4s 0s infinite normal;
        }

        .right-ball.ball-wrapper {
            transform-origin: center top;
            animation: right-ball-swing 4s 2s infinite normal;
        }

        @keyframes left-ball-swing {
            0% {
                transform: rotate(0deg);
                animation-timing-function: ease-out;
            }
            25% {
                transform: rotate(90deg);
                animation-timing-function: ease-in;
            }
            50% {
                transform: rotate(0deg)
            }
            100% {}
        }

        @keyframes right-ball-swing {
            0% {
                transform: rotate(0deg);
                animation-timing-function: ease-out;
            }
            25% {
                transform: rotate(-90deg);
                animation-timing-function: ease-in;
            }
            50% {
                transform: rotate(0deg)
            }
            100% {}
        }
    </style>
</head>

<body>
    <div class="main-container">
        <div class="swing-container">
            <div class="roof">
            </div>
            <div class="left-ball ball-wrapper">
                <div class="rope"></div>
                <div class="ball"></div>
            </div>
            <div class="ball-wrapper">
                <div class="rope"></div>
                <div class="ball"></div>
            </div>
            <div class="ball-wrapper">
                <div class="rope"></div>
                <div class="ball"></div>
            </div>
            <div class="ball-wrapper">
                <div class="rope"></div>
                <div class="ball"></div>
            </div>
            <div class="right-ball ball-wrapper">
                <div class="rope"></div>
                <div class="ball"></div>
            </div>
        </div>
    </div>
</body>
</html>
实现起来并不复杂,只需对最左和最右的小球进行关键帧动画处理,同时注意应该将绳子与小球作为一个整体,以左边小球为例:
.left-ball.ball-wrapper {
            transform-origin: center top;
            animation: left-ball-swing 4s 0s infinite normal;
        }
        
 @keyframes left-ball-swing {
            0% {
                transform: rotate(0deg);
                animation-timing-function: ease-out;
            }
            25% {
                transform: rotate(90deg);
                animation-timing-function: ease-in;
            }
            50% {
                transform: rotate(0deg)
            }
            100% {}
        }   
以绳子的起点为圆心,摆动共4秒:
  0%--25%(第一秒): 顺时针旋转90度,速度为ease-out(逐渐变缓,因为动能转化为势能到了最高点速度为0)
  25%--50%(第二秒): 返回到0度,即逆时针旋转了90度,速度为ease-in,势能变为动能
  50%--100%(后两秒):不做变化(这两秒是右边小球在摆动)

That'it,最后来看效果图:

 

感谢阅读,o(* ̄▽ ̄*)o

 


 

原创文章,转载请注明出处。

转载于:https://www.cnblogs.com/freddysia/p/11070055.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值