• 纯 CSS 创作一个小球绕着圆环盘旋的动画


    效果预览

    在线演示

    按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

    https://codepen.io/comehope/pen/gKxyWo

    可交互视频

    此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

    请用 chrome, safari, edge 打开观看。

    https://scrimba.com/p/pEgDAM/cg48mty

    源代码下载

    本地下载

    每日前端实战系列的全部源代码请从 github 下载:

    https://github.com/comehope/front-end-daily-challenges

    代码解读

    定义 dom,容器中包含一个圆环和3个小球:

    <div class="container">
        <div class="ring"></div>
        <div class="spheres">
            <span class="sphere"></span>
            <span class="sphere"></span>
            <span class="sphere"></span>
        </div>
    </div>
    

    居中显示:

    body {
        margin: 0;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: darkslategray;
    }
    

    改变盒模型:

    * {
        box-sizing: border-box;
    }
    

    画出圆环:

    .container {
        position: relative;
        font-size: 20px;
    }
    
    .ring {
        position: relative;
         10em;
        height: 10em;
        border: 1.5em solid paleturquoise;
        border-radius: 50%;
    }
    

    在圆环的左上方画出一个小球:

    .sphere {
        position: absolute;
        top: -20%;
        left: -20%;
    }
    
    .sphere::after {
        content: '';
        position: absolute;
         3em;
        height: 3em;
        background-color: lightseagreen;
        border-radius: 50%;
    }
    

    让小球在圆环的左上方盘旋:

    .sphere {
         80%;
        height: 80%;
        animation: rotate 1.5s linear infinite;
    }
    
    @keyframes rotate {
      to {
        transform: rotate(360deg);
      }
    }
    

    让小球的圆环的上下穿梭:

    .ring {
        z-index: 2;
    }
    
    .sphere {
        animation: 
            rotate 1.5s linear infinite,
            overlapping 1.5s linear infinite;
    }
    
    @keyframes overlapping {
      to {
          z-index: 2;
      }
    }
    

    通过设置动画延时,制造 3 个小球同时盘旋的效果:

    .sphere:nth-child(2) {
        animation-delay: -0.5s;
    }
    
    .sphere:nth-child(3) {
        animation-delay: -1s;
    }
    

    最后,让容器转动起来,制造小球围绕圆环盘旋的效果:

    .container {
        animation: rotate 5s linear infinite;
    }
    

    大功告成!

    原文地址:https://segmentfault.com/a/1190000015295466

  • 相关阅读:
    闭包概念集合
    对象的基本方法
    webpack始出来
    elasticsearch性能调优
    elasticsearch 倒排索引学习
    elasticearch 归并策略
    更加详细的Log4net的配置
    第一篇博客关于Log4net的配置记录
    js数组小结
    javascript在不同的浏览器处理事件
  • 原文地址:https://www.cnblogs.com/lalalagq/p/10011850.html
Copyright © 2020-2023  润新知