鼠标悬浮特效:常见6种边框类悬浮特效
前言
在之前的文章中,我们介绍了 CSS元素动画篇:基于当前位置的变换动画 ,在代码实现中,我们也使用了很多鼠标悬浮事件来触发特效,这些特效都可以用于鼠标悬浮特效。除此之外,常见的鼠标悬浮特效还有元素边框类特效(改变元素边框的展示方式)和元素背景类特效(改变元素背景颜色、阴影等)。本文将着重介绍日常开发中,常见的6种边框类特效。
边框展示
边框展示:鼠标悬浮到元素,展示元素边框,其效果图如下所示:
效果预览
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>边框展示</title>
<style>
.hover-border {
width: 200px;
height: 100px;
background-color: #e0e0e0;
border: 5px solid transparent;
/* 文字相关属性 */
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #333;
}
.hover-border:hover {
animation: reveal-border 0.3s ease forwards;
}
@keyframes reveal-border {
from {
border-color: transparent;
}
to {
border-color: #230FEE;
}
}
</style>
</head>
<body>
<div class="hover-border">边框展示</div>
</body>
</html>
边框外浮
边框外浮:鼠标悬浮到元素,元素从内到外展示一个边框,且边框和元素本身存在一定间距,其效果图如下所示:
效果预览
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>边框外浮</title>
<style>
.hover-border {
margin: 50px;
width: 200px;
height: 100px;
background-color: #e0e0e0;
position: relative;
/* 文字相关属性 */
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #333;
}
.hover-border::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border: 5px solid #e0e0e0;
opacity: 0;
transition: all 0.3s ease;
}
.hover-border:hover::before {
opacity: 1;
top: -10px;
left: -10px;
right: -10px;
bottom: -10px;
}
</style>
</head>
<body>
<div class="hover-border">边框外浮</div>
</body>
</html>
边框闪现
边框闪现:边框闪效的效果与边框外浮的效果类似,只不过边框外浮会向外产生一个边框固定,而边框闪现产生边框后消失,其效果图如下所示:
效果预览
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>边框闪现</title>
<style>
.hover-border {
margin: 50px;
width: 200px;
height: 100px;
background-color: #e0e0e0;
position: relative;
/* 文字相关属性 */
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #333;
}
.hover-border::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border: 5px solid #e0e0e0;
opacity: 0;
transition: all 0.3s ease;
}
.hover-border:hover::before {
animation: flink 1s ease-in-out 1;
}
@keyframes flink {
0% {
opacity: 0;
}
50% {
opacity: 0.5;
top: -8px;
left: -8px;
right: -8px;
bottom: -8px;
}
100% {
opacity: 0;
top: -15px;
left: -15px;
right: -15px;
bottom: -15px;
}
}
</style>
</head>
<body>
<div class="hover-border">边框闪现</div>
</body>
</html>
元素空心
元素空心:鼠标移动到元素上,元素背景色消失,只留下一个边框,其效果图如下所示:
效果预览
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>元素空心</title>
<style>
.hover-border {
margin: 50px;
width: 200px;
height: 100px;
border: 5px solid #e0e0e0;
background-color: transparent;
position: relative;
/* 文字相关属性 */
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #333;
}
.hover-border::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #e0e0e0;
z-index: -1;
transform: scale(1);
transform-origin: center;
transition: transform 0.3s ease;
}
.hover-border:hover::before {
transform: scale(0);
}
</style>
</head>
<body>
<div class="hover-border">元素空心</div>
</body>
</html>
元素裁剪
元素裁剪:元素裁剪和边框外浮的效果正好相反,边框外浮是外向展示一个边框,元素裁剪是内形成一个内边框,看起来元素像被裁剪了,其效果图如下所示:
效果预览
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>元素裁剪</title>
<style>
.hover-border {
margin: 50px;
width: 200px;
height: 100px;
border: 5px solid #e0e0e0;
background-color: transparent;
position: relative;
/* 文字相关属性 */
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #333;
}
.hover-border::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #e0e0e0;
z-index: -1;
transform-origin: center;
transition: all 0.3s ease;
}
.hover-border:hover::before {
top: 5px;
left: 5px;
right: 5px;
bottom: 5px;
}
</style>
</head>
<body>
<div class="hover-border">元素裁剪</div>
</body>
</html>
圆角边框
圆角边框:鼠标移动到元素上,元素边框变成圆角边框,,其效果图如下所示:
效果预览
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>边框圆角</title>
<style>
.hover-border {
margin: 50px;
width: 200px;
height: 100px;
background-color: #e0e0e0;
transition: border-radius 0.5s ease;
/* 文字相关属性 */
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #333;
}
.hover-border:hover {
border-radius: 20px;
}
</style>
</head>
<body>
<div class="hover-border">边框圆角</div>
</body>
</html>
结语
本文主要介绍了6种常见的鼠标悬浮边框类特效,你还知道哪些鼠标悬浮边框类特效?欢迎在评论区留言分享!