動態(tài)Button【手把手教程】【代碼】



button.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Button Effects Javascript</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<a href="#">
<i></i>
<i></i>
<span>Button</span>
</a>
<script>
let btn = document.querySelector('a');
btn.addEventListener('mousemove',e =>{
let rect = e.target.getBoundingClientRect();
let x = e.clientX*3-rect.left;
btn.style.setProperty('--x',x +'deg');
})
</script>
</body>
</html>
style.css
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800;900&display=swap');
*
{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins',sans-serif;
}
:root
{
--x: 45deg;
}
body
{
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #0e1538;
}
a
{
position: relative;
width: 150px;
height: 55px;
display: inline-block;
border-radius: 5px;
}
a i?
{
position: absolute;
inset: -2px;
display: block;
border-radius: 5px;
}
a i,
a i:nth-child(2)
{
background: linear-gradient(var(--x),#00ccff,#0e1538,#0e1538,#d400d4);
}
a i:nth-child(2)
{
filter: blur(10px);
}
a span
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
text-transform: uppercase;
color: #fff;
letter-spacing: 2px;
border: 1px solid #040a29;
border-radius: 3px;
background: rgba(14, 21, 56, 0.65);
overflow: hidden;
}
a span::before
{
content: '';
position: absolute;
top: 0;
left: -50%;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.075);
transform: skew(25deg);
}