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);

完整代码下载

完整代码下载

相关推荐
我不只是切图仔11 分钟前
我只是想给网站加个注册验证码,咋就那么难!
前端·后端
该用户已不存在36 分钟前
macOS是开发的终极进化版吗?
前端·后端
小豆包api1 小时前
小豆包AI API × Nano Banana:3D手办 + AI视频生成,「动起来」的神级玩法!
前端·api
布列瑟农的星空1 小时前
大话设计模式——观察者模式和发布/订阅模式的区别
前端·后端·架构
龙在天1 小时前
Vue3 实现 B站 视差 动画
前端
KenXu1 小时前
F2C Prompt to Design、AI 驱动的设计革命
前端
小鱼儿亮亮1 小时前
canvas中画线条,线条效果比预期宽1像素且模糊问题分析及解决方案
前端·react.js
@大迁世界1 小时前
用 popover=“hint“ 打造友好的 HTML 提示:一招让界面更“懂人”
开发语言·前端·javascript·css·html
伍哥的传说1 小时前
Tailwind CSS v4 终极指南:体验 Rust 驱动的闪电般性能与现代化 CSS 工作流
前端·css·rust·tailwindcss·tailwind css v4·lightning css·utility-first
小鱼儿亮亮1 小时前
使用Redux的combineReducers对数据拆分
前端·react.js