有趣的css - 牛顿摆效果

大家好,我是 Just,这里是「设计师工作日常」,今天分享的是一个牛顿摆物理效果动效,可以用在 loading 加载动画的需求场景中,提高用户体验感。

最新文章通过公众号「设计师工作日常」发布。


整体效果

知识点:
transform 属性中 transform-origin 变形中参考点(基点)
② 伪元素 :before:after
animation 动画
flex 布局

思路:
绘制4个牛顿摆的小球以及绳子,然后让第一个小球和最后一个小球进行动画摆动。

一个牛顿摆物理效果动效,可以用在 loading 加载动画的需求场景中,提高用户体验感。


核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

核心代码

html 代码

<div class="niudunbai59">
  <div class="niudunbai-dot-59"></div>
  <div class="niudunbai-dot-59"></div>
  <div class="niudunbai-dot-59"></div>
  <div class="niudunbai-dot-59"></div>
</div>

.niudunbai59 标签作为整个牛顿摆的主体块,然后下面 4 个子元素标签 .niudunbai-dot-59 作为 4 个小球的主体。

css 部分代码

.niudunbai59{
  width: 80px;
  height: 80px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.niudunbai-dot-59{
  width: 20px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  transform-origin: center top;
}
.niudunbai-dot-59:before{
  content: '';
  width: 20px;
  height: 20px;
  position: relative;
  border-radius: 50%;
  background-color: #0093E9;
  background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%);
  box-shadow: inset 0 0 4px rgba(0,0,0,0.2);
}
.niudunbai-dot-59:after{
  content: '';
  width: 2px;
  height: 60px;
  position: absolute;
  top: 0;
  background-color: #0093E9;
  background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%);
}
.niudunbai-dot-59:first-child{
  animation: eff59 1.4s infinite;
}
.niudunbai-dot-59:last-child{
  animation: eff592 1.4s infinite;
}
@keyframes eff59{
  0% {
    transform: rotate(0deg);
    animation-timing-function: ease-out;
  }

  25% {
    transform: rotate(70deg);
    animation-timing-function: ease-in;
  }

  50% {
    transform: rotate(0deg);
    animation-timing-function: linear;
  }
}
@keyframes eff592{
  0% {
    transform: rotate(0deg);
    animation-timing-function: linear;
  }

  50% {
    transform: rotate(0deg);
    animation-timing-function: ease-out;
  }

  75% {
    transform: rotate(-70deg);
    animation-timing-function: ease-in;
  }
}

1、定义整体牛顿摆 .niudunbai59 的大小等样式,设置 flex 布局,定义子元素平行垂直居中。

2、定义悬挂的小球 .niudunbai-dot-59 整体大小样式,设置变形的参考点 transform-origin: center top;;设置 flex 布局,定义子元素平行垂直居中 。

3、基于 .niudunbai-dot-59 创建伪元素 :before:after ,分别作为小球以及绳子,分别定义其样式,这里给小球以及绳子增加了渐变背景以及内阴影效果。

4、给第一个以及最后一个悬挂的小球,分别增加一个 animation 动画属性,创建动画名称分别为 eff59eff592,并设置 infinite 无限循环播放。

5、分别给动画 eff59eff592 设置关键帧,让其变化角度。

注意: 这里的关键帧的角度变化时要叉开,一个小球摆动起来时,另一个小球要回到原位。还有就是当小球摆动到最高点时,要有一点滞空的视觉效果,所以这里还分别设置了 animation-timing-function 动画速度曲线的属性参数。

完整代码如下

html 页面

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>牛顿摆</title>
  </head>
  <body>
    <div class="app">
      <div class="niudunbai59">
        <div class="niudunbai-dot-59"></div>
        <div class="niudunbai-dot-59"></div>
        <div class="niudunbai-dot-59"></div>
        <div class="niudunbai-dot-59"></div>
      </div>
    </div>
  </body>
</html>

css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.niudunbai59{
  width: 80px;
  height: 80px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.niudunbai-dot-59{
  width: 20px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  transform-origin: center top;
}
.niudunbai-dot-59:before{
  content: '';
  width: 20px;
  height: 20px;
  position: relative;
  border-radius: 50%;
  background-color: #0093E9;
  background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%);
  box-shadow: inset 0 0 4px rgba(0,0,0,0.2);
}
.niudunbai-dot-59:after{
  content: '';
  width: 2px;
  height: 60px;
  position: absolute;
  top: 0;
  background-color: #0093E9;
  background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%);
}
.niudunbai-dot-59:first-child{
  animation: eff59 1.4s infinite;
}
.niudunbai-dot-59:last-child{
  animation: eff592 1.4s infinite;
}
@keyframes eff59{
  0% {
    transform: rotate(0deg);
    animation-timing-function: ease-out;
  }

  25% {
    transform: rotate(70deg);
    animation-timing-function: ease-in;
  }

  50% {
    transform: rotate(0deg);
    animation-timing-function: linear;
  }
}
@keyframes eff592{
  0% {
    transform: rotate(0deg);
    animation-timing-function: linear;
  }

  50% {
    transform: rotate(0deg);
    animation-timing-function: ease-out;
  }

  75% {
    transform: rotate(-70deg);
    animation-timing-function: ease-in;
  }
}

页面渲染效果

以上就是所有代码,以及简单的思路,希望对你有一些帮助或者启发。


[1] 原文阅读


CSS 是一种很酷很有趣的计算机语言,在这里跟大家分享一些 CSS 实例 Demo,为学习者获取灵感和思路提供一点帮助,希望你们喜欢。

我是 Just,这里是「设计师工作日常」,求点赞求关注!

  • 26
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

设计师工作日常

请我炫个饼🫓

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

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

打赏作者

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

抵扣说明:

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

余额充值