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

完整代码下载

完整代码下载

相关推荐
HuaHua的世界8 分钟前
说说 Vue 中 CSS scoped 的原理?
css·vue.js
littleplayer8 分钟前
iOS Swift Redux 架构详解
前端·设计模式·架构
工呈士13 分钟前
HTML 模板技术与服务端渲染
前端·html
皮实的芒果14 分钟前
前端实时通信方案对比:WebSocket vs SSE vs setInterval 轮询
前端·javascript·性能优化
鹿九巫14 分钟前
【CSS】层叠,优先级与继承(三):超详细继承知识点
前端·css
奕云15 分钟前
react-redux源码分析
前端
咸鱼一号机16 分钟前
:global 是什么
前端
专业掘金17 分钟前
0425 手打基础丸
前端
五号厂房17 分钟前
Umi Max 如何灵活 配置多环境变量
前端
AXUI18 分钟前
至美!看AXUI如何美化原始HTML标签(reset/normalize)
css·html