一:灯光效果按钮
1.效果展示
2.HTML完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>灯光效果按钮</title>
<style>
body, html {
height: 100%;
/* 设置body和html元素的高度为视口的100% */
margin: 0;
/* 移除body和html元素的外边距 */
display: flex;
/* 将body和html元素的显示方式设置为弹性盒模型,便于布局 */
justify-content: center;
/* 在水平方向上居中对齐子元素 */
align-items: center;
/* 在垂直方向上居中对齐子元素 */
font-family: 'fangsong';
background-color: rgb(0, 0, 0);
}
.item {
position: relative;
/* 设置.item元素的定位方式为相对定位 */
margin: 100px;
/* 设置.item元素的外边距为50像素 */
width: 300px;
/* 设置.item元素的宽度为300像素 */
height: 80px;
/* 设置.item元素的高度为80像素 */
text-align: center;
/* 设置.item元素内的文本居中对齐 */
line-height: 80px;
/* 设置.item元素内文本的行高为80像素,使文本垂直居中 */
text-transform: uppercase;
/* 将.item元素内的文本转换为大写 */
text-decoration: none;
/* 移除.item元素内文本的装饰(如下划线) */
font-size: 35px;
/* 设置.item元素内文本的字体大小为35像素 */
letter-spacing: 5px;
/* 设置.item元素内文本的字符间距为5像素 */
color: aqua;
/* 设置.item元素内文本的颜色为青色 */
overflow: hidden;
/* 隐藏.item元素内超出其盒模型的内容 */
}
.item:hover {
/* 当鼠标悬停在.item元素上时,应用以下样式 */
background-color: aqua;
/* 设置背景颜色为青色 */
box-shadow: 0 0 5px aqua, 0 0 75px aqua, 0 0 155px aqua;
/* 设置盒阴影效果,创建发光效果 */
color: black;
/* 设置文本颜色为黑色 */
}
.item span:nth-of-type(1) {
/* 选择.item元素内的第一个span元素,并应用以下样式 */
position: absolute;
/* 设置定位方式为绝对定位 */
left: -100%;
/* 初始位置在.item元素的左侧外部 */
width: 100%;
/* 设置宽度为.item元素的100% */
height: 3px;
/* 设置高度为3像素 */
background-image: linear-gradient(to left, aqua, transparent);
/* 设置背景为从左到右的线性渐变,从青色渐变到透明 */
animation: btnanmaiton 1s linear infinite;
/* 应用名为btnanmaiton的动画,持续1秒,线性速度,无限循环 */
}
@keyframes btnanmaiton {
/* 定义名为btnanmaiton的关键帧动画 */
0% {
left: -100%; }
/* 动画开始时,元素在左侧外部 */
50%, 100% {
left: 100%; }
/* 动画进行到50%和100%时,元素移动到右侧外部 */
}
.item span:nth-of-type(2) {
position: absolute;
top: -100%;
right: 0;
width: 3px;
height: 100%;
background-image: linear-gradient(to top, aqua, transparent);
animation: two 1s linear infinite;
animation-delay: 0.25s;
}
@keyframes two {
0% {
top: -100%;
}
50%,
100% {
top: 100%;
}
}
.item span:nth-of-type(3) {
position: absolute;
right: -100%;
bottom: 0;
width: 100%;
height: 3px;
background-image: linear-gradient(to right, aqua, transparent);
animation: three 1s linear infinite;
animation-delay: 0.5s;
}
@keyframes three {
0% {
right: -100%;
}
50%,
100% {
right: 100%;
}
}
.item span:nth-of-type(4) {
position: absolute;
bottom: -100%;
left: 0;
width: 3px;
height: 100%;
background-image: linear-gradient(to bottom, aqua, transparent);
animation: zuo 1s linear infinite;
animation-delay: 0.75s;
}
@keyframes zuo {
0% {
bottom: -100%;
}
50%,
100% {
bottom: 100%;
}
}
</style>
</head>
<body>
<a href="#" class="item item2">
按钮
<span></span>
<span></span>
<span></span>
<span></span>
</a>
</body>
</html>
二:拉链式展开按钮
1.效果展示
2.HTML完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>拉链式展开按钮</title>
<style>
body, html {
height: 100%; /* 设置高度为100% */
margin: 0; /* 去除默认的外边距 */
display: flex; /* 使用弹性盒布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
/* 对应用容器.app进行样式设置 */
.app{
width: 100%; /* 宽度为100% */
height: 100vh; /* 高度为视口高度的100% */
background-color: #ffffff; /* 背景颜色为白色 */
position: relative; /* 设置定位方式为相对定位 */
display: flex; /* 使用弹性盒布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
/* 对按钮.btn72进行样式设置 */
.btn72{
width: 120px; /* 宽度为120px */
height: 42px; /* 高度为42px */
font-weight: 600; /* 字体加粗 */
font-size: 18px; /* 字体大小为18px */
line-height: 1; /* 行高为1 */
letter-spacing: 4px; /* 字符间距为4px */
background: transparent; /* 背景透明 */
border: none; /* 无边框 */
display: flex; /* 使用弹性盒布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
position: relative; /* 设置定位方式为相对定位 */
z-index: 0; /* 设置层叠顺序为0 */
transition: color 0.3s linear; /* 设置颜色变化的过渡效果,持续时间为0.3秒,变化速度为线性 */
cursor: pointer; /* 鼠标悬停时显示为指针 */
}
/* 对按钮.btn72的:before伪元素进行样式设置 */
.btn72:before{
content: ''; /* 内容为空 */
width: 0; /* 宽度为0 */
height: 2px; /* 高度为2px */
background-color: #1630f1; /* 背景颜色为深蓝色 */
position: absolute; /* 设置定位方式为绝对定位 */
left: 0; /* 左对齐 */
z-index: -2; /* 设置层叠顺序为-2 */
transition: width 0.3s linear; /* 设置宽度变化的过渡效果,持续时间为0.3秒,变化速度为线性 */
}
/* 对按钮.btn72的:after伪元素进行样式设置 */
.btn72:after{
content: ''; /* 内容为空 */
width: 100%; /* 宽度为100% */
height: 0; /* 高度为0 */
background-color: #16f1ea; /* 背景颜色为浅蓝色 */
position: absolute; /* 设置定位方式为绝对定位 */
z-index: -1; /* 设置层叠顺序为-1 */
transition: height 0.3s linear; /* 设置高度变化的过渡效果,持续时间为0.3秒,变化速度为线性 */
}
/* 当鼠标悬停在按钮.btn72上时,对:before伪元素进行样式变化 */
.btn72:hover:before{
width: 100%; /* 宽度变为100% */
}
/* 当鼠标悬停在按钮.btn72上时,对:after伪元素进行样式变化 */
.btn72:hover:after{
transition-delay: 0.3s; /* 设置过渡效果的延迟时间为0.3秒 */
height: 100%; /* 高度变为100% */
}
/* 当鼠标悬停在按钮.btn72上时,对按钮本身进行样式变化 */
.btn72:hover{
color: #fff; /* 文字颜色变为白色 */
transition-delay: 0.3s; /* 设置过渡效果的延迟时间为0.3秒 */
}
</style>
</head>
<body>
<div class="app">
<button class="btn72">按钮</button>
</div>
</body>
</html>
三:毛玻璃式按钮
1.效果展示
2.HTML完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>毛玻璃按钮</title>
<style>
body, html {
height: 100%; /* 设置高度为100% */
margin: 0; /* 去除默认的外边距 */
display: flex; /* 使用弹性盒布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
/* 对body元素进行样式设置 */
body {
position: relative; /* 设置定位方式为相对定位 */
background-color: #f0f0f0; /* 设置背景颜色为浅灰色 */
}
/* 对容器.container进行样式设置 */
.container {
width: 1000px; /* 设置宽度为1000px */
display: flex; /* 使用弹性盒布局 */
flex-wrap: wrap; /* 允许子元素换行 */
justify-content: space-around; /* 子元素之间均匀分布 */
}
/* 对按钮.btn进行样式设置 */
.btn {
position: relative; /* 设置定位方式为相对定位 */
top: 0; /* 顶部对齐 */
left: 0; /* 左侧对齐 */
width: 250px; /* 设置宽度为250px */
height: 50px; /* 设置高度为50px */
margin: 0; /* 去除外边距 */
display: flex; /* 使用弹性盒布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
/* 对按钮内的链接.btn a进行样式设置 */
.btn a {
position: absolute; /* 设置定位方式为绝对定位 */
top: 0; /* 顶部对齐 */
left: 0; /* 左侧对齐 */
width: 100%; /* 宽度为100% */
height: 100%; /* 高度为100% */
display: flex; /* 使用弹性盒布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
background: rgba(255, 255, 255, 0.05); /* 设置背景颜色为半透明白色 */
box-shadow: 0 15px 15px rgba(0, 0, 0, 0.3); /* 设置阴影效果 */
border-bottom: 1px solid rgba(255, 255, 255, 0.1); /* 设置底部边框为半透明白色 */
border-top: 1px solid rgba(255, 255, 255, 0.1); /* 设置顶部边框为半透明白色 */
border-radius: 30px; /* 设置边框圆角为30px */
padding: 10px; /* 设置内边距为10px */
box-sizing: border-box; /* 设置盒模型为边框盒模型 */
letter-spacing: 1px; /* 设置字符间距为1px */
text-decoration: none; /* 去除文本下划线 */
overflow: hidden; /* 隐藏溢出内容 */
color: #f1c978; /* 设置文本颜色为金黄色 */
z-index: 1; /* 设置层叠顺序为1 */
transition: 0.5s; /* 设置过渡效果持续时间为0.5秒 */
backdrop-filter: blur(15px); /* 设置背景滤镜为模糊效果,模糊半径为15px */
}
/* 当鼠标悬停在按钮上时,对链接.btn a的字符间距进行变化 */
.btn:hover a {
letter-spacing: 3px; /* 设置字符间距为3px */
}
/* 对链接.btn a的:before伪元素进行样式设置 */
.btn a::before {
content: ""; /* 内容为空 */
position: absolute; /* 设置定位方式为绝对定位 */
top: 0; /* 顶部对齐 */
left: 0; /* 左侧对齐 */
width: 50%; /* 宽度为50% */
height: 100%; /* 高度为100% */
/* 设置背景为从左到右的线性渐变,从半透明白色到透明 */
background: linear-gradient(to left, rgba(255, 255, 255, 0.15), transparent);
/* 设置变换效果,先沿X轴倾斜45度,再水平移动0px */
transform: skewX(45deg) translate(0);
transition: 0.5s; /* 设置过渡效果持续时间为0.5秒 */
filter: blur(0px); /* 设置滤镜为无模糊效果 */
}
/* 当鼠标悬停在按钮上时,对链接.btn a的:before伪元素的变换效果进行变化 */
.btn:hover a::before {
/* 设置变换效果,先沿X轴倾斜45度,再水平移动200px */
transform: skewX(45deg) translate(200px);
}
/* 对具有.btn-common类的按钮的:before和:after伪元素进行公共样式设置 */
.btn-common::before, .btn-common::after {
content: ""; /* 内容为空 */
position: absolute; /* 设置定位方式为绝对定位 */
left: 50%; /* 水平居中 */
transform: translateX(-50%); /* 水平移动-50%,实现真正居中 */
width: 30px; /* 宽度为30px */
height: 10px; /* 高度为10px */
border-radius: 10px; /* 设置边框圆角为10px */
transition: 0.5s; /* 设置过渡效果持续时间为0.5秒 */
}
/* 当鼠标悬停在具有.btn-common类的按钮上时,对:before和:after伪元素的宽度、高度和边框圆角进行变化 */
.btn-common:hover::before, .btn-common:hover::after {
width: 80%; /* 宽度变为80% */
height: 50%; /* 高度变为50% */
border-radius: 30px; /* 设置边框圆角为30px */
}
/* 对具有.btn-common类的按钮的:before伪元素进行定位设置 */
.btn-common::before {
bottom: -5px; /* 底部对齐并偏移-5px */
}
/* 当鼠标悬停在具有.btn-common类的按钮上时,对:before伪元素的底部位置进行变化 */
.btn-common:hover::before {
bottom: 0; /* 底部对齐 */
}
/* 对具有.btn-common类的按钮的:after伪元素进行定位设置 */
.btn-common::after {
top: -5px; /* 顶部对齐并偏移-5px */
}
/* 当鼠标悬停在具有.btn-common类的按钮上时,对:after伪元素的顶部位置进行变化 */
.btn-common:hover::after {
top: 0; /* 顶部对齐 */
}
/* 对具有.btn-blue类的按钮内的链接进行样式设置 */
.btn-blue a {
background: #2db2ff; /* 设置背景颜色为蓝色 */
}
/* 对具有.btn-blue类的按钮的:before和:after伪元素进行样式设置,添加阴影效果 */
.btn-blue::before, .btn-blue::after {
/* 设置多层阴影效果,颜色为蓝色 */
box-shadow: 0 0 5px #2db2ff, 0 0 15px #2db2ff, 0 0 30px #2db2ff, 0 0 60px #2db2ff;
}
</style>
</head>
<body>
<div class="container">
<!-- 按钮,同时应用了.btn、.btn-common和.btn-blue三个类 -->
<div class="btn btn-common btn-blue"><a href="#">按钮</a></div>
</div>
</body>
</html>
四:双开门按钮
1.效果展示
2.HTML完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>双开门按钮</title>
<style>
body, html {
height: 100%; /* 设置body和html元素的高度为100% */
margin: 0; /* 设置外边距为0 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
.app{
width: 100%; /* 设置.app元素的宽度为100% */
height: 100vh; /* 设置.app元素的高度为视口高度的100% */
background-color: #ffffff; /* 设置背景颜色为白色 */
position: relative; /* 设置定位方式为相对定位 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
.btn69{
width: 120px; /* 设置按钮的宽度为120px */
height: 50px; /* 设置按钮的高度为50px */
background-color: transparent; /* 设置按钮背景颜色为透明 */
color: #333333; /* 设置文字颜色为深灰色 */
font-size: 16px; /* 设置字体大小为16px */
font-weight: bold; /* 设置字体加粗 */
letter-spacing: 2px; /* 设置字符间距为2px */
border: none; /* 去掉按钮的边框 */
cursor: pointer; /* 设置鼠标悬停时显示为指针 */
position: relative; /* 设置定位方式为相对定位 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
z-index: 1; /* 设置层叠顺序为1 */
/* 设置文字颜色的过渡效果,持续时间为0.3秒,效果为缓入缓出 */
transition: color 0.3s ease-in-out;
}
.btn69:before,.btn69:after{
content: ''; /* 伪元素必须设置content属性 */
width: 0; /* 初始宽度为0 */
height: 50px; /* 高度与按钮一致 */
background-color: #38d0c4; /* 设置背景颜色为青绿色 */
position: absolute; /* 设置定位方式为绝对定位 */
top: 0; /* 顶部对齐 */
right: 60px; /* 右侧偏移60px,为左侧伪元素设置位置 */
z-index: -1; /* 设置层叠顺序为-1,使其位于按钮文字下方 */
/* 设置宽度的过渡效果,持续时间为0.3秒,效果为缓入缓出 */
transition: width 0.3s ease-in-out;
}
.btn69:after{
left: 60px; /* 为右侧伪元素设置左侧偏移60px */
}
.btn69:hover:before,.btn69:hover:after{
width: 60px; /* 鼠标悬停时,伪元素的宽度变为60px */
}
.btn69:hover{
color: #ffffff; /* 鼠标悬停时,按钮文字颜色变为白色 */
}
</style>
</head>
<body>
<div class="app">
<!-- 创建一个类名为app的div容器 -->
<button class="btn69">按钮</button>
<!-- 创建一个类名为btn69的按钮,并设置按钮文字为"按钮" -->
</div>
</body>
</html>
五:上下线条左右滑动按钮
1.效果展示
2.HTML完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>上下线条左右滑动按钮</title>
<style>
body, html {
height: 100%; /* 设置body和html元素的高度为100% */
margin: 0; /* 设置外边距为0 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
.app{
width: 100%; /* 设置.app元素的宽度为100% */
height: 100vh; /* 设置.app元素的高度为视口高度的100% */
background-color: #ffffff; /* 设置背景颜色为白色 */
position: relative; /* 设置定位方式为相对定位 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
.btn75{
padding: 10px 16px; /* 设置按钮的内边距,上下为10px,左右为16px */
font-size: 16px; /* 设置字体大小为16px */
font-weight: 700; /* 设置字体加粗,权重为700 */
color: #333; /* 设置文字颜色为深灰色 */
letter-spacing: 4px; /* 设置字符间距为4px */
border: none; /* 去掉按钮的边框 */
outline-style: none; /* 去掉按钮的轮廓线 */
background-color: transparent; /* 设置按钮背景颜色为透明 */
cursor: pointer; /* 设置鼠标悬停时显示为指针 */
position: relative; /* 设置定位方式为相对定位 */
/* 设置文字颜色的过渡效果,持续时间为0.4秒,效果为线性 */
transition: color 0.4s linear;
}
.btn75:before,.btn75:after{
content: ''; /* 伪元素必须设置content属性 */
width: 0; /* 初始宽度为0 */
height: 2px; /* 设置高度为2px,形成线条效果 */
background-color: #ff3a85; /* 设置背景颜色为粉红色 */
position: absolute; /* 设置定位方式为绝对定位 */
top: 0; /* 顶部对齐,为上面的线条设置位置 */
left: 0; /* 左侧对齐,为上面的线条设置初始位置 */
/* 设置宽度的过渡效果,持续时间为0.4秒,效果为线性 */
transition: width 0.4s linear;
}
.btn75:after{
top: auto; /* 取消顶部对齐,为下面的线条设置位置 */
left: auto; /* 取消左侧对齐,为下面的线条设置位置 */
bottom: 0; /* 底部对齐,为下面的线条设置位置 */
right: 0; /* 右侧对齐,为下面的线条设置初始位置 */
}
.btn75:hover:before,.btn75:hover:after{
width: 100%; /* 鼠标悬停时,伪元素的宽度变为100%,形成线条滑动效果 */
}
.btn75:hover{
color: #ff3985; /* 鼠标悬停时,按钮文字颜色变为稍深的粉红色 */
/* 添加文字阴影效果,增强视觉层次感 */
text-shadow: 0 4px 10px rgba(238,99,150,0.4);
}
</style>
</head>
<body>
<div class="app">
<!-- 创建一个类名为app的div容器 -->
<button class="btn75">按钮</button>
<!-- 创建一个类名为btn75的按钮,并设置按钮文字为"按钮" -->
</div>
</body>
</html>
六:水波纹按钮
1.效果展示
2.HTML完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>水波纹按钮</title>
<style>
body, html {
height: 100%; /* 设置body和html元素的高度为100% */
margin: 0; /* 设置外边距为0 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
.app{
width: 100%; /* 设置.app元素的宽度为100% */
height: 100vh; /* 设置.app元素的高度为视口高度的100% */
background-color: #ffffff; /* 设置背景颜色为白色 */
position: relative; /* 设置定位方式为相对定位 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
.btn68{
width: 120px; /* 设置按钮的宽度为120px */
height: 50px; /* 设置按钮的高度为50px */
background-color: #c944d2; /* 设置按钮的背景颜色为紫色 */
color: #ffffff; /* 设置按钮文字的颜色为白色 */
font-size: 16px; /* 设置字体大小为16px */
font-weight: bold; /* 设置字体加粗 */
letter-spacing: 2px; /* 设置字符间距为2px */
border: none; /* 去掉按钮的边框 */
border-radius: 25px; /* 设置按钮的圆角半径为25px,使按钮呈现圆角效果 */
cursor: pointer; /* 设置鼠标悬停时显示为指针 */
position: relative; /* 设置定位方式为相对定位 */
display: flex; /* 使用Flexbox布局 */
justify-content: center; /* 水平居中按钮内的内容 */
align-items: center; /* 垂直居中按钮内的内容 */
transition: all .1s linear; /* 设置所有属性的过渡效果,持续时间为0.1秒,效果为线性 */
}
.btn68:before,.btn68:after{
content: ''; /* 伪元素必须设置content属性 */
width: 120px; /* 设置伪元素的宽度与按钮相同 */
height: 50px; /* 设置伪元素的高度与按钮相同 */
border-radius: 25px; /* 设置伪元素的圆角半径与按钮相同 */
position: absolute; /* 设置定位方式为绝对定位 */
/* 应用名为eff68的动画,持续时间为1.4秒,线性效果,无限循环 */
animation: eff68 1.4s linear infinite;
}
.btn68:after{
/* 设置:after伪元素的动画延迟0.7秒开始,形成错开的水波纹效果 */
animation-delay: 0.7s;
}
@keyframes eff68{
0%{
box-shadow: 0 0 0 0px #67d569; /* 初始状态,无阴影 */
opacity: 0.2; /* 初始透明度为0.2 */
}
100%{
box-shadow: 0 0 0 30px #d29419; /* 结束状态,阴影扩展到30px,颜色为橙色 */
opacity: 0; /* 结束透明度为0 */
}
}
/* 定义了一个名为eff68的动画,从初始状态到结束状态,阴影逐渐扩展并改变颜色,透明度逐渐降低 */
.btn68:active{
/* 设置按钮在被点击(active状态)时的缩放效果,缩放为原大小的96% */
transform: scale(0.96);
}
/* 当按钮被点击时,会稍微缩小,模拟按下的效果 */
</style>
</head>
<body>
<div class="app">
<!-- 创建一个类名为app的div容器 -->
<button class="btn68">按钮</button>
</div>
</body>
</html>
七:单一式内波纹按钮
1.效果展示
2.HTML完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width,
initial-scale=1.0" />
<!-- 设置视口,使页面在移动设备上表现良好,宽度为设备宽度,初始缩放比例为1.0 -->
<title>单一式内波纹按钮</title>
<style>
body {
display: grid;
/* 使用网格布局 */
place-items: center;
/* 将子元素居中 */
min-height: 100vh;
/* 设置最小高度为视口高度的100% */
overflow: hidden;
/* 隐藏溢出内容 */
background-color: #bfd7e8;
/* 设置背景颜色 */
}
.ripple-btn {
position: relative;
/* 相对定位 */
overflow: hidden;
/* 隐藏溢出内容 */
border: none;
/* 无边框 */
background-color: #3acbc6;
/* 按钮背景颜色 */
color: #fff;
/* 文字颜色 */
padding: 30px 40px;
/* 内边距 */
font-size: 16px;
/* 字体大小 */
cursor: pointer;
/* 鼠标悬停时显示为手指形状 */
outline: none;
/* 去除点击时的轮廓线 */
border-radius: 4px;
/* 边框圆角 */
box-shadow: none;
/* 无阴影 */
}
.ripple-effect {
position: absolute;
/* 绝对定位 */
border-radius: 50%;
/* 圆形 */
background-color: rgba(73, 167, 39, 0.42);
/* 波纹颜色,带透明度 */
pointer-events: none;
/* 禁止鼠标事件 */
animation: ripple-animation 0.6s linear forwards;
/* 应用动画,持续0.6秒,线性,动画结束后保持最终状态 */
transform-origin: center center;
/* 变形原点为中心 */
box-shadow: none;
/* 无阴影 */
}
@keyframes ripple-animation {
/* 定义动画关键帧 */
from {
transform: scale(1);
/* 起始状态,大小为原始大小 */
opacity: 1;
/* 不透明 */
}
to {
transform: scale(10);
/* 结束状态,放大10倍 */
opacity: 0;
/* 完全透明 */
}
}
</style>
</head>
<body>
<button class="ripple-btn" id="botton">
按钮
</button>
<script>
const bottons = document.querySelector('#botton')
/* 获取ID为botton的元素,并赋值给bottons变量 */
const effect = document.querySelector('.ripple-effect')
/* 尝试获取类名为ripple-effect的元素,但此时页面上还没有这个元素,所以effect为null */
const {
width, height } =
bottons.getBoundingClientRect()
/* 获取bottons元素的尺寸,并解构赋值给width和height变量(这行代码实际上在后续代码中未使用) */
bottons.onclick = function (evt) {
/* 为bottons元素添加点击事件监听器 */
const {
offsetX, offsetY } = evt
/* 获取点击事件相对于bottons元素的位置,并解构赋值给offsetX和offsetY变量 */
const ripple = document.createElement("span");
/* 创建一个span元素,并赋值给ripple变量 */
ripple.classList.add("ripple-effect");
/* 为ripple元素添加ripple-effect类 */
ripple.style.height = '30px'
/* 设置ripple元素的高度 */
ripple.style.width = '30px'
/* 设置ripple元素的宽度 */
ripple.style.left = `${
offsetX - 15}px`
/* 设置ripple元素的left样式,使波纹中心对准点击位置 */
ripple.style.top = `${
offsetY - 15}px`
/* 设置ripple元素的top样式,使波纹中心对准点击位置 */
bottons.appendChild(ripple);
/* 将ripple元素添加到bottons元素中 */
setTimeout(() => {
bottons.removeChild(ripple);
/* 0.6秒后(与动画时间相同),从bottons元素中移除ripple元素 */
}, 600);
}
</script>
</body>
</html>