zoukankan      html  css  js  c++  java
  • 如何用纯 CSS 创作一个永动的牛顿摆

    效果预览

    在线演示

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

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

    可交互视频教程

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

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

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

    源代码下载

    本地下载

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

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

    代码解读

    定义dom,容器中包含 5 个子元素:

    <div class="loader">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
    

    居中显示:

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

    画出小球的挂线:

    .loader {
        position: absolute;
        display: flex;
         12em;
        font-size: 10px;
        justify-content: space-between;
    }
    
    .loader span {
        position: relative;
         0.2em;
        height: 10em;
        background-color: black;
        transform-origin: top;
    }
    

    画出小球:

    .loader span::before {
        content: '';
        position: absolute;
        bottom: 0;
         3em;
        height: 3em;
        border-radius: 50%;
        background-color: black;
        left: -1.4em;
    }
    

    画出牛顿摆的挂架:

    .loader {
        border-style: solid;
        border-color: black;
        border- 0.4em 0.1em 4em 0.1em;
        padding: 0 4em 2em 4em;
    }
    

    让最左侧的摆线晃动:

    .loader span:first-child {
        animation: moving-up 0.75s cubic-bezier(0.215, 0.61, 0.355, 1) infinite alternate;
        --direction: 1;
    }
    
    @keyframes moving-up {
        0%{
            transform: rotate(0deg);
        }
    
        50%{
            transform: rotate(0deg);
        }
    
        100%{
            transform: rotate(calc(45deg * var(--direction)));
        }
    }
    

    最后,让最右侧的摆线晃动:

    .loader span:last-child {
        animation: moving-up 0.75s cubic-bezier(0.215, 0.61, 0.355, 1) infinite alternate-reverse;
        --direction: -1;
    }
    

    大功告成!

    原文地址:https://segmentfault.com/a/1190000015270808
  • 相关阅读:
    Codeforces 1316B String Modification
    Codeforces 1305C Kuroni and Impossible Calculation
    Codeforces 1305B Kuroni and Simple Strings
    Codeforces 1321D Navigation System
    Codeforces 1321C Remove Adjacent
    Codeforces 1321B Journey Planning
    Operating systems Chapter 6
    Operating systems Chapter 5
    Abandoned country HDU
    Computer HDU
  • 原文地址:https://www.cnblogs.com/lalalagq/p/10007872.html
Copyright © 2011-2022 走看看