前端每日实战:50# 视频演示如何用纯 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;
    width: 12em;
    font-size: 10px;
    justify-content: space-between;
}

.loader span {
    position: relative;
    width: 0.2em;
    height: 10em;
    background-color: black;
    transform-origin: top;
}

画出小球:

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

画出牛顿摆的挂架:

.loader {
    border-style: solid;
    border-color: black;
    border-width: 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;
}

大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 HTML 和 CSS 实现 24 小时超市的代码: HTML: ```html <div class="market"> <div class="banner"> <h1>24小时超市</h1> </div> <div class="shelf"> <div class="product"> <img src="product1.jpg" alt="Product 1"> <h2>产品1</h2> <p>$10</p> </div> <div class="product"> <img src="product2.jpg" alt="Product 2"> <h2>产品2</h2> <p>$15</p> </div> <div class="product"> <img src="product3.jpg" alt="Product 3"> <h2>产品3</h2> <p>$20</p> </div> </div> </div> ``` CSS: ```css /* Reset styles */ * { box-sizing: border-box; margin: 0; padding: 0; } /* Global styles */ body { font-family: Arial, sans-serif; background-color: #f2f2f2; } /* Market styles */ .market { max-width: 960px; margin: 0 auto; padding: 20px; } .banner { background-color: #007bff; color: #fff; text-align: center; padding: 10px; margin-bottom: 20px; } .shelf { display: flex; flex-wrap: wrap; } .product { width: 30%; margin-right: 3.33333%; margin-bottom: 20px; background-color: #fff; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.4); padding: 10px; text-align: center; } .product img { max-width: 100%; height: auto; } .product h2 { font-size: 18px; margin-top: 10px; margin-bottom: 5px; } .product p { font-size: 16px; font-weight: bold; color: #007bff; margin-bottom: 10px; } ``` 这个代码会产生一个简单的超市页面,包括一个顶部的横幅和三个产品展示在一个排列良好的货架上。请注意,这只是一个基本的示例,你可以根据你的具体需求进行更改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值