有趣的 CSS - 动态圆点水波纹效果

先看效果

整体效果

这个效果使用 css 中 animation 属性,以及搭配伪元素 ::after::before 来实现两个圆交替变化。

核心代码

html部分代码

html 复制代码
<div>
  <label class="dot"></label>
</div>

label 标签画圆点主体。

css部分代码

css 复制代码
.app{
  width: 100%;
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.dot {
  width: 48px;
  height: 48px;
  display: block;
  position: relative;
  border-radius: 50%;
  background-color: blue;
  z-index: 1;
}
.dot::after {
  width: 100%;
  height: 100%;
  content: "";
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -2;
  background-color: blue;
  animation: dot-play 4s linear 400ms infinite;
}
.dot::before {
  width: 100%;
  height: 100%;
  content: "";
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  background-color: blue;
  animation: dot-play 4s linear 200ms infinite;
  animation-delay: 2s; /* 延迟 2s */
}
@keyframes dot-play{
  from{
    transform: scale(1);
    opacity: .2;
  }
  to{
    transform: scale(4);
    opacity: 0;
  }
}

伪元素基于主体圆写出两个蓝色圆形来做水波纹,并设置 animation 属性进行变化。

注意:第二个伪元素的圆形,延迟 2s 启动 animation 动画。

完整代码

html代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css?k3f22ww">
  <title>03 圆点水波纹效果</title>
</head>
<body>
  <div class="app">
    <label class="dot"></label>
  </div>
</body>
</html>

css代码

css 复制代码
*{
  margin: 0;
  padding: 0;
  list-style: none;
  transition: .5s;
}
html,body{
  background-color: #f5f5f5;
  color: #fff;
  font-size: 14px;
}
.app{
  width: 100%;
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.dot {
  width: 48px;
  height: 48px;
  display: block;
  position: relative;
  border-radius: 50%;
  background-color: blue;
  z-index: 1;
}
.dot::after {
  width: 100%;
  height: 100%;
  content: "";
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -2;
  background-color: blue;
  animation: dot-play 4s linear 400ms infinite;
}
.dot::before {
  width: 100%;
  height: 100%;
  content: "";
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  background-color: blue;
  animation: dot-play 4s linear 200ms infinite;
  animation-delay: 2s; /* 延迟 2s */
}
@keyframes dot-play{
  from{
    transform: scale(1);
    opacity: .2;
  }
  to{
    transform: scale(4);
    opacity: 0;
  }
}

页面效果


1\] [阅读原文](https://mp.weixin.qq.com/s/XcUOW9_-GqdgTlGTMCo0Vg) 我是 Just,这里是「设计师工作日常」,求点赞求关注!

相关推荐
孤水寒月2 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀2 小时前
html初学者第一天
前端·html
耶啵奶膘4 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
视频砖家5 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
lyj1689975 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽6 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头7 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
墨菲安全7 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing7 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆7 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试