效果图:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>js波浪点击按钮</title>
<style>
* {
padding: 0;
margin: 0 auto;
}
body,
html {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
body {
background: #f0f0f0;
}
main {
width: 400px;
height: 80px;
text-align: center;
line-height: 80px;
letter-spacing: 0.2em;
font-size: 30px;
cursor: pointer;
position: relative;
overflow: hidden;
background: white;
color: rgb(143, 141, 141);
}
main div {
border-radius: 50%;
width: 20px;
height: 20px;
background: rgba(0, 0, 0, 0.342);
position: absolute;
animation: bian 0.5s linear both 1;
}
@keyframes bian {
0% {
opacity: 1;
transform: scale(1)
}
100% {
opacity: 0;
transform: scale(8)
}
}
</style>
</head>
<body>
<main unselectable="on" onselectstart="return false;">
点我!点我!
</main>
</body>
<!-- 效果实现部分 -->
<script>
var main = document.querySelector('main');
main.onmousemove = function (e) {
var x = e.clientX - main.offsetLeft;
var y = e.clientY - main.offsetTop;
console.log(x, y);
main.onclick = function () {
var div = document.createElement('div');
div.style.left = x + 'px';
div.style.top = y + 'px';
var r = Math.floor(Math.random() * 256)
var g = Math.floor(Math.random() * 256)
var b = Math.floor(Math.random() * 256)
div.style.background = "rgb(" + r + ',' + g + ',' + b + ")";
main.appendChild(div);
setTimeout(function () {
main.removeChild(div);
}, 600)
}
}
</script>
</html>