用几行代码带你制作网页水波特效
在这里分享一个我平时常用的水波特效步骤,加在按钮上特好使。
首先,是直接创建一个div盒子,不需要在里面添加其他内容,我们直接对盒子本身添加css可以形成水波效果。
html部分,我们div添加白色的波纹,所以在这里设置html背景为蓝色。
<body style="background-color: cadetblue ;">
<div class="video"></div>
</body>
css部分,先设置好div的基本属性
.video {
/* 基本属性 */
width: 100px;
height: 100px;
border-radius: 50px;
/* 给背景颜色添加不透明度 */
/* 不透明度还可以通过添加opacity属性修改 */
background-color: rgb(255, 255, 255, 0.6);
}
然后就是在video中添加这个特效中重中之重的内容,在css中设置动画。
动画是由三部分组成
-
关键帧(keyframes)–以帧的形式定义动画在不同阶段的状态。
-
如果是不同时间下形状发生的变化主要可以用动画的0%,50%,100%表示不同帧对象的变化
-
如果是不同时间下位置发生的变化主要可以用,从来来表示不同帧对象的变化
-
动画属性(属性)–决定动画的播放时长,播放次数,以及使用其他函数式去播放动画等。
-
语法:名称持续时间计时功能延迟迭代计数方向填充模式播放状态;
-
css属性–就是css元素来表示不同的关键帧下的状态。
.video {
/* 添加ripple动画效果 */
/* -webkit-animation适配-webkit内核的浏览器*/
-webkit-animation: ripple 1s linear infinite;
animation: ripple 1s linear infinite;
}
/* 定义ripple动画效果 */
@-webkit-keyframes ripple {
/* 关键帧播放到0%时的状态 */
0% {
/* 在box四周添加三层白色阴影 */
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
/* 关键帧播放到100%时的状态 */
100% {
/* 分别改变三层阴影的距离
形成两帧的动画,然后在transition的过渡下形成动画 */
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
/* 多种浏览器兼容性设置 */
@keyframes ripple {
0% {
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
100% {
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
再给div添加一个转换属性,让div在鼠标移动的时候能自然过渡,其原理跟动画类似。
.video {
/* 添加动画的过渡效果 */
transition: all 0.3s ease-in-out;
}
然后就能得到我们的结果,整体的代码如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.video {
width: 100px;
height: 100px;
border-radius: 50px;
background-color: rgb(255, 255, 255, 0.6);
transition: all 0.3s ease-in-out;
-webkit-animation适配-webkit内核的浏览器*/
-webkit-animation: ripple 1s linear infinite;
animation: ripple 1s linear infinite;
}
.video:hover {
background-color: #FFFFFF;
transform: scale(1.2);
}
@-webkit-keyframes ripple {
0% {
/* 在box四周添加三层白色阴影 */
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
100% {
/* 分别改变三层阴影的距离
形成两帧的动画,然后在transition的过渡下形成动画 */
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
@keyframes ripple {
0% {
box-shadow: 0 0 0 0 rgb(255 255 255 / 25%),
0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%);
}
100% {
box-shadow: 0 0 0 10px rgb(255 255 255 / 25%),
0 0 0 20px rgb(255 255 255 / 25%),
0 0 0 40px rgba(50, 100, 245, 0);
}
}
</style>
</head>
<body style="background-color: cadetblue ;">
<div class="video"></div>
</body>
</html>
效果图