复制代码即可用
<!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>demo</title>
<style>
.button {
position: relative;
appearance: none;
background: #f72359;
padding: 1em 6em;
border: none;
color: white;
font-size: 1.2em;
cursor: pointer;
outline: none;
overflow: hidden;
border-radius: 100px;
}
.button::before {
--size: 0;
content: '';
position: absolute;
left: var(--x);
top: var(--y);
width: var(--size);
height: var(--size);
background: radial-gradient(circle closest-side, #4405f7, transparent);
transform: translate(-50%, -50%);
transition: width .2s ease, height .2s ease;
}
/* 这一步的目的是 上一步的颜色会吧button文本覆盖掉。 在这里重新添加一个! */
.button::after{
content: 'Niu bi class';
position: absolute;
z-index: 2;
left: 0;
right: 0;
}
.button:hover::before {
--size: 400px;
}
</style>
</head>
<body>
<button class="button">Niu bi class</button>
<script>
// 获取到鼠标的位置。
document.querySelector('.button').onmousemove = (e) => {
const x = e.pageX - e.target.offsetLeft
const y = e.pageY - e.target.offsetTop
e.target.style.setProperty('--x', `${ x }px`)
e.target.style.setProperty('--y', `${ y }px`)
}
</script>
</body>
</html>