<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3边框按钮悬停动画</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #222;
font-family: 'Arial', sans-serif;
flex-direction: column;
gap: 40px;
}
.link {
position: absolute;
bottom: 20px;
right: 20px;
color: rgba(255,255,255,0.7);
font-family: Arial, sans-serif;
text-decoration: none;
z-index: 100;
transition: color 0.3s ease;
}
.link:hover {
color: #fff;
}
.btn-container {
display: flex;
flex-direction: column;
gap: 30px;
align-items: center;
}
.btn-row {
display: flex;
gap: 30px;
}
.btn {
position: relative;
padding: 15px 40px;
color: #fff;
text-decoration: none;
text-transform: uppercase;
letter-spacing: 2px;
font-size: 1.2em;
overflow: hidden;
transition: 0.5s;
-webkit-box-reflect: below 1px linear-gradient(transparent, #0003);
}
/* 效果1 - 边框流光 */
.btn-effect-1 {
background: rgba(255,255,255,0.1);
border: 1px solid rgba(255,255,255,0.1);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.btn-effect-1:hover {
background: var(--clr);
color: #111;
box-shadow: 0 0 50px var(--clr);
transition-delay: 0.5s;
}
.btn-effect-1::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 10px;
height: 10px;
border-top: 2px solid var(--clr);
border-left: 2px solid var(--clr);
transition: 0.5s;
}
.btn-effect-1:hover::before {
width: 100%;
height: 100%;
}
.btn-effect-1::after {
content: '';
position: absolute;
bottom: 0;
right: 0;
width: 10px;
height: 10px;
border-bottom: 2px solid var(--clr);
border-right: 2px solid var(--clr);
transition: 0.5s;
}
.btn-effect-1:hover::after {
width: 100%;
height: 100%;
}
/* 效果2 - 边框扩展 */
.btn-effect-2 {
background: transparent;
border: none;
}
.btn-effect-2 span {
position: absolute;
display: block;
background: var(--clr);
transition: 0.5s ease-in-out;
}
.btn-effect-2 span:nth-child(1) {
top: 0;
left: 0;
width: 0;
height: 2px;
}
.btn-effect-2 span:nth-child(2) {
top: 0;
right: 0;
width: 2px;
height: 0;
}
.btn-effect-2 span:nth-child(3) {
bottom: 0;
right: 0;
width: 0;
height: 2px;
}
.btn-effect-2 span:nth-child(4) {
bottom: 0;
left: 0;
width: 2px;
height: 0;
}
.btn-effect-2:hover span:nth-child(1) {
width: 100%;
}
.btn-effect-2:hover span:nth-child(2) {
height: 100%;
transition-delay: 0.2s;
}
.btn-effect-2:hover span:nth-child(3) {
width: 100%;
transition-delay: 0.4s;
}
.btn-effect-2:hover span:nth-child(4) {
height: 100%;
transition-delay: 0.6s;
}
/* 效果3 - 边框旋转 */
.btn-effect-3 {
background: transparent;
border: none;
}
.btn-effect-3::before,
.btn-effect-3::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
box-sizing: border-box;
border: 2px solid var(--clr);
top: 0;
left: 0;
transition: 0.8s;
}
.btn-effect-3:hover::before {
transform: rotate(45deg);
opacity: 0;
}
.btn-effect-3:hover::after {
transform: rotate(-45deg);
opacity: 0;
}
/* 效果4 - 边框填充 */
.btn-effect-4 {
background: transparent;
border: 2px solid var(--clr);
z-index: 1;
}
.btn-effect-4::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 0;
height: 100%;
background: var(--clr);
z-index: -1;
transition: 0.8s;
}
.btn-effect-4:hover {
color: #111;
}
.btn-effect-4:hover::before {
width: 100%;
}
/* 效果5 - 边框脉冲 */
.btn-effect-5 {
background: transparent;
border: 2px solid var(--clr);
overflow: visible;
}
.btn-effect-5::before {
content: '';
position: absolute;
top: -5px;
left: -5px;
right: -5px;
bottom: -5px;
border: 2px solid var(--clr);
opacity: 0;
transition: 0.5s;
}
.btn-effect-5:hover::before {
opacity: 1;
top: -10px;
left: -10px;
right: -10px;
bottom: -10px;
}
/* 效果6 - 边框双线 */
.btn-effect-6 {
background: transparent;
border: none;
}
.btn-effect-6::before,
.btn-effect-6::after {
content: '';
position: absolute;
width: 30px;
height: 30px;
border: 2px solid var(--clr);
transition: 0.5s;
}
.btn-effect-6::before {
top: -10px;
left: -10px;
border-right: none;
border-bottom: none;
}
.btn-effect-6::after {
bottom: -10px;
right: -10px;
border-left: none;
border-top: none;
}
.btn-effect-6:hover::before,
.btn-effect-6:hover::after {
width: calc(100% + 18px);
height: calc(100% + 18px);
}
</style>
</head>
<body>
<div class="btn-container">
<div class="btn-row">
<a href="#" class="btn btn-effect-1" style="--clr: #ff0f5b">效果1</a>
<a href="#" class="btn btn-effect-2" style="--clr: #00ccff">
<span></span>
<span></span>
<span></span>
<span></span>
效果2
</a>
<a href="#" class="btn btn-effect-3" style="--clr: #0f0">效果3</a>
</div>
<div class="btn-row">
<a href="#" class="btn btn-effect-4" style="--clr: #ff0">效果4</a>
<a href="#" class="btn btn-effect-5" style="--clr: #f0f">效果5</a>
<a href="#" class="btn btn-effect-6" style="--clr: #0ff">效果6</a>
</div>
</div>
</body>
</html>
CSS3边框按钮悬停动画
于 2025-06-09 17:13:01 首次发布