CSS 实现丝滑动画

效果展示

CSS 知识点

  • animation 综合运用

页面整体布局

html 复制代码
<div class="box">
  <div class="circle"></div>
</div>

编写基础样式

css 复制代码
.box {
  position: relative;
  width: 400px;
  height: 400px;
  border: 80px solid transparent;
  border-left: 80px solid #fff5;
  border-bottom: 80px solid #fff5;
  border-radius: 50%;
  transform: rotate(-45deg);
}

.box::after {
  content: "";
  position: absolute;
  top: -13px;
  left: -34px;
  width: 80px;
  height: 40px;
  background: #fff;
  border-radius: 50%;
  transform: rotate(45deg);
}

.box::before {
  content: "";
  position: absolute;
  top: 214px;
  right: -33px;
  width: 80px;
  height: 40px;
  background: #fff;
  border-radius: 50%;
  transform: rotate(45deg);
}

编写小球样式

css 复制代码
.circle {
  position: absolute;
  top: 65px;
  left: -65px;
  width: 70px;
  height: 70px;
  background: #fff;
  border-radius: 50%;
  box-shadow: inset 0 5px 20px rgba(0, 0, 0, 0.5);
  z-index: 10000;
  transform-origin: 200px;
}

编写动画

css 复制代码
.box {
  animation: animate 2s linear infinite;
}

/* 第三阶段 */
@keyframes animate {
  0%,
  100% {
    transform: rotate(333deg);
  }
  50% {
    transform: rotate(290deg);
  }
}

.circle {
  animation: animateBall 2s linear infinite;
}

@keyframes animateBall {
  0%,
  100% {
    transform: rotate(45deg);
  }
  50% {
    transform: rotate(225deg);
  }
}

编写 JS 定时刷新背景

javascript 复制代码
function changeBg() {
  let r = Math.floor(Math.random() * 255);
  let g = Math.floor(Math.random() * 255);
  let b = Math.floor(Math.random() * 255);

  document.body.style.backgroundColor = `rgba(${r}, ${g}, ${b})`;
}

setInterval(() => {
  changeBg();
}, 4000);

完整代码下载

完整代码下载

相关推荐
软件技术NINI8 分钟前
前端怎么学
前端
O***p60410 分钟前
前端体验的下一次革命:从页面导航到“流式体验”的系统化重构
前端·重构
一岁天才饺子13 分钟前
XSS挑战赛实战演练
前端·网络安全·xss
Hilaku15 分钟前
Canvas 粒子特效:带你写一个黑客帝国同款的代码雨(附源码)😆
前端·javascript·前端框架
文心快码BaiduComate23 分钟前
我用文心快码Spec 模式搓了个“pre作弊器”,妈妈再也不用担心我开会忘词了(附源码)
前端·后端·程序员
JH灰色26 分钟前
【大模型】-LangChain--stream流式同步异步
服务器·前端·langchain
lxh011342 分钟前
二叉树中的最大路径和
前端·算法·js
CC码码1 小时前
前端字符串排序搜索可以更加细化了
前端·javascript·面试
喵爱吃鱼1 小时前
kuma-ui中Flex vs FlexMin的关键区别
前端
codingMan1 小时前
[Android Compose] 拒绝闪烁!打造丝滑的聊天页面列表(仿微信效果)
前端