svg+css实现三种图标点击状态变化动画效果代码
代码语言:html
所属分类:动画
代码描述:svg+css实现三种图标点击状态变化动画效果代码,包含点赞、手册、金币支付三种图标。
代码标签: svg css 三种 图标 点击 状态 变化 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
:root {
/* 自定义颜色变量 方便统一切换图标颜色 */
--color: #fb7299;
/* --color: #00aeec; */
}
* {
/* 初始化基础元素 */
margin: 0;
padding: 0;
box-sizing: border-box;
/* 解决手机浏览器点击有选框的问题 */
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
body {
/* 上下左右居中 */
display: flex;
justify-content: center;
align-items: center;
/* 元素的距离 */
gap: 100px;
/* 临时改成竖着居中 */
/* flex-direction: column; */
/* 超出部分隐藏 */
overflow: hidden;
min-height: 100vh;
}
.box {
/* 内部元素上下左右居中 */
display: flex;
justify-content: center;
align-items: center;
width: 200px;
height: 200px;
/* 50% 的圆角就是 ○ */
border-radius: 50%;
/* 鼠标放上去是小手 */
cursor: pointer;
position: relative;
}
.box .icon {
width: 150px;
height: 150px;
/* svg 的填充颜色 */
fill: #ddd;
/* svg 的描边线宽 */
stroke-width: 1;
/* svg 的描边颜色 */
stroke: var(--color);
/* svg 的描边线框样式 */
stroke-dasharray: 100;
/* svg 的描边偏移量,也就是描边开始到结束的距离 */
stroke-dashoffset: 100;
/* svg 的描边两头的形状,是圆的 */
stroke-linecap: round;
z-index: 10;
}
.box span,
.box span::before {
width: 20px;
height: 20px;
background-color: transparent;
border-radius: 50%;
/* 利用盒子阴影来实现小圆点绕着中心的效果 */
box-shadow: 0 -120px 0 var(--color), 0 120px 0 var(--color),
-120px 0 0 var(--color), 120px 0 0 var(--color);
/* 元素居中 */
position: absolute;
top: 50%;
left: 50%;
/* scale(0) 是先把小圆点隐藏起来,下面动画再显示 */
transform: translate(-50%, -50%) scale(0);
}
.box span::before {
content: "";
/* 利用伪元素复制一份小圆点,rotate(45deg) 旋转 45° */
transform: translate(-50%, -50%) rotate(45deg) scale(0);
}
.box input[type="checkbox"] {
/* 隐藏默认复选框样式 */
display: none;
position: absolute;
}
.box input[type="checkbox"]:checked ~ .icon path {
/* 给 svg 图标添加动画 */
animation: 1s animate-fill linear forwards;
}
@keyframes animate-fill {
/* 动画主要修改 svg 的描边偏移量和填充颜色,要掌握好动画的时间 */
0% {
stroke-dashoffset: 100;
}
80% {
stroke-dashoffset: 0;
fill: #ddd;
}
100% {
stroke-dashoffset: 0;
fill: var(--color);
}
}
.box input[type="checkbox"]:checked ~ span {
/* 给图标周围的小圆点添加动画 */
animation: 0.5s animate-flash ease-in-out forwards 0.85s;
}
@keyframes animate-flash {
0% {
/* 通过 scale(0.5) 放大缩小控制圆点距离 */
transform: translate(-50%, -50%) scale(0.5);
/* 控制圆点的显示和隐藏 */
opacity: 0.8;
}
50% {
transform: translate(-50%, -50%) scale(1);
opacity: 0.8;
}
100% {
transform: translate(-50%, -50%) scale(1.5);
opacity: 0;
}
}
.box input[type="checkbox"]:checked ~ span::before.........完整代码请登录后点击上方下载按钮下载查看
网友评论0