Let's take a look at the effect achieved first
html code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>第七天</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="bt">泛起波纹的按钮</div>
<script>
const bt = document.querySelector('div.bt');
bt.addEventListener('click',(e) =>{
let offsetLeft=e.target.offsetLeft;
let offsetTop=e.target.offsetTop;
if(e.target.nodeName=='SPAN'){
offsetLeft=e.target.offsetParent.offsetLeft;
offsetTop=e.target.offsetParent.offsetTop;
}
let x=e.clientX-offsetLeft;
let y=e.clientY-offsetTop;
let ripple=document.createElement('span');
ripple.style.left=x+"px";
ripple.style.top=y+"px";
bt.appendChild(ripple);
setTimeout(()=>{
ripple.remove();
},1000);
});
</script>
</body>
</html>
css code
:root{
--background-color:#2c3e50;
--show_color1:#03a9f4;
--show_color2:#f441a5;
}
*{
margin: 0;
padding: 0;
}
body{
width: 100vw;
height: 100vh;
background-color: var(--background-color);
display: flex;
justify-content: center;
align-items: center;
}
.bt{
font-size: 15px;
color: #fff;
background: linear-gradient(90deg,var(--show_color1),var(--show_color2));
padding: 24px 48px;
border-radius: 60px;
position: relative;
overflow: hidden;
}
.bt span{
position: absolute;
background-color: #fff;
border-radius: 50%;
transform: translate(-50%,-50%);
animation: show 1s;
}
@keyframes show{
0%{
opacity: 0.5;
width: 0;
height: 0;
}
100%{
opacity: 0;
width: 300px;
height: 300px;
}
}
Summary of knowledge learned today
Label or function |
effect |
opacity |
Set opacity level |
span |
Used to combine elements in the line, very flexible |
document.querySelector |
Get an element in the document |
addEventListener |
Add click event |
e.target.offsetLeft |
Returns the upper left corner of the current element (event) |
e.target.nodeName |
Get node name |
e.target.offsetParent.offsetLeft |
Return the upper left corner of the parent element |
e.clientX |
The clientX event property returns the horizontal coordinate of the mouse pointer to the browser page (or client area) when the event is triggered. |
document.createElement |
The createElement() method creates an element by specifying the name |
.style.left |
The left property sets or returns the left position of the positioned element. |
appendChild |
The appendChild() method adds the last child node to the node. |
setTimeout |
The setTimeout() method is used to call a function or calculation expression after the specified number of milliseconds. |