2D动画——CSS制作摩天轮

1、步骤

  1. 需要的素材分为三个部分,一个是底部位置固定的支架;一个是中间旋转的骨架;一个是单个座位;
  2. 分为三个盒子分别装这三种素材,第一个盒子放骨架,第二个盒子利用绝对定位放在第一个盒子之上,里面放单个的座位,第三个盒子就放底部支架,通过 z-index: -1属性使它放在最底层,让其他盒子遮住它;
  3. 单个座位需要使用绝对定位来对齐骨架的每个连接处;
  4. 动画部分就两个,一个逆时针自转(单个座位),一个顺时针整体旋转(包含单个座位的父盒子)

 2、代码

素材和效果图在下面

    <style>
        section {
            position: relative;
            width: 600px;
            height: 600px;
            margin: 100px auto;
        }
        
        section div:nth-child(1) img {
            /* 原图片太大,所有等比例强制缩放固定大小了 */
            width: 358px;
            height: 358px;
        }
        
        section div:nth-child(1) {
            /* 固定大盒子的位置,并且添加顺时针动画 */
            position: absolute;
            top: 0;
            left: 0;
            animation: rotate 5s linear infinite;
        }
        
        section div:nth-child(3) {
            position: relative;
            top: -400px;
            left: -5px;
            /* 把底部支架移到最底层 */
            z-index: -1;
        }
        
        section div:nth-of-type(3) img {
            /* 原图片太大,所有等比例强制缩放固定大小了 */
            width: 366px;
            height: 436px;
        }
        
        section .box div img {
            /* 原图片太大,所有等比例强制缩放固定大小了 */
            width: 80px;
            height: 80px;
        }
        
        section .box {
            width: 358px;
            height: 358px;
            position: relative;
            top: 0;
            left: 0;
            /* 控制旋转架子添加顺时针动画 */
            animation: rotate 5s linear infinite;
        }
        
        section .box div:nth-child(1) {
            /* 固定定位座位 */
            position: absolute;
            top: -20px;
            left: 66px;
            animation: rotate2 5s linear infinite;
        }
        
        section .box div:nth-child(2) {
            /* 固定定位座位 */
            position: absolute;
            top: -23px;
            left: 192px;
            animation: rotate2 5s linear infinite;
        }
        
        section .box div:nth-child(3) {
            /* 固定定位座位 */
            position: absolute;
            top: 48px;
            left: 280px;
            animation: rotate2 5s linear infinite;
        }
        
        section .box div:nth-child(4) {
            /* 固定定位座位 */
            position: absolute;
            top: 175px;
            left: 300px;
            animation: rotate2 5s linear infinite;
        }
        
        section .box div:nth-child(5) {
            /* 固定定位座位 */
            position: absolute;
            top: 285px;
            left: 212px;
            animation: rotate2 5s linear infinite;
        }
        
        section .box div:nth-child(6) {
            /* 固定定位座位 */
            position: absolute;
            top: 290px;
            left: 75px;
            animation: rotate2 5s linear infinite;
        }
        
        section .box div:nth-child(7) {
            /* 固定定位座位 */
            position: absolute;
            top: 209px;
            left: -8px;
            animation: rotate2 5s linear infinite;
        }
        
        section .box div:nth-child(8) {
            /* 固定定位座位 */
            position: absolute;
            top: 77px;
            left: -20px;
            animation: rotate2 5s linear infinite;
        }
        /* 控制摩天轮整体的顺时针旋转 */
        
        @keyframes rotate {
            0% {}
            100% {
                transform: rotate(360deg);
            }
        }
        /* 控制摩天轮上单个座位的逆时针自转 */
        
        @keyframes rotate2 {
            0% {}
            100% {
                transform: rotate(-360deg);
            }
        }
    </style>
</head>

<body>
    <section>
        <div><img src="images/骨架.png" alt=""></div>
        <div class="box">
            <div><img src="images/座位1.png" alt=""></div>
            <div><img src="images/座位2.png" alt=""></div>
            <div><img src="images/座位1.png" alt=""></div>
            <div><img src="images/座位2.png" alt=""></div>
            <div><img src="images/座位1.png" alt=""></div>
            <div><img src="images/座位2.png" alt=""></div>
            <div><img src="images/座位1.png" alt=""></div>
            <div><img src="images/座位2.png" alt=""></div>
        </div>
        <div><img src="images/支架.png" alt=""></div>
    </section>
</body>

3、效果

4、总结

很简单的一个小案例,注意盒子层级关系,找好素材,相信你也可以做出来的! 素材在下面,需要自取:

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

super码力

么么哒,夏天来块儿冰西瓜!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值