CSS animation通过@keyframes实现多阶段动画,如脉冲和波纹效果,相比transition更灵活,支持复杂序列、循环及精细控制,提升交互体验。

CSS
animation
transition
要实现一个基础的按钮悬停动画,我们首先需要一个按钮元素,然后通过CSS的
@keyframes
:hover
<button class="animated-button">点击我</button>
.animated-button {
padding: 12px 24px;
font-size: 18px;
color: white;
background-color: #007bff;
border: none;
border-radius: 8px;
cursor: pointer;
outline: none;
position: relative; /* 为后续可能的伪元素动画做准备 */
overflow: hidden; /* 确保内容不溢出 */
transition: background-color 0.3s ease; /* 基础的背景色过渡 */
}
/* 定义一个简单的“脉冲”动画 */
@keyframes pulse {
0% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(0, 123, 255, 0.7);
}
70% {
transform: scale(1.05);
box-shadow: 0 0 0 10px rgba(0, 123, 255, 0);
}
100% {
transform: scale(1);
box-shadow: 0 0 0 0 rgba(0, 123, 255, 0);
}
}
.animated-button:hover {
background-color: #0056b3; /* 悬停时背景色变化 */
animation: pulse 0.8s forwards; /* 应用脉冲动画 */
}在这个例子中,我们定义了一个名为
pulse
@keyframes
animation: pulse 0.8s forwards;
pulse
forwards
transition
animation
说实话,很多人在实现简单的悬停效果时,第一反应可能就是用
transition
transition
transition
立即学习“前端免费学习笔记(深入)”;
animation
animation
@keyframes
transition
animation
animation-iteration-count
infinite
animation-delay
animation-direction
animation-fill-mode
animation-play-state
transition
animation
@keyframes
@keyframes
我有时候会想,
transition
animation
“波纹扩散”效果,也就是我们常说的“水波纹”或“涟漪”效果,是很多现代UI设计中非常受欢迎的交互方式。它给用户的反馈感非常强,感觉按钮真的被“按下”了。实现这种效果,我们通常会借助伪元素(
::before
::after
animation
核心思路是:在按钮悬停时,让一个隐藏的圆形伪元素从按钮中心开始,快速放大并逐渐透明。
<button class="ripple-button">点击触发</button>
.ripple-button {
padding: 15px 30px;
font-size: 18px;
color: white;
background-color: #6c757d;
border: none;
border-radius: 50px; /* 圆角按钮更适合波纹效果 */
cursor: pointer;
outline: none;
position: relative;
overflow: hidden; /* 关键:隐藏溢出伪元素 */
z-index: 1; /* 确保按钮内容在波纹之上 */
transition: background-color 0.3s ease;
}
.ripple-button:hover {
background-color: #5a6268;
}
/* 伪元素作为波纹 */
.ripple-button::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 0;
height: 0;
background: rgba(255, 255, 255, 0.4); /* 波纹颜色 */
border-radius: 50%;
transform: translate(-50%, -50%); /* 确保中心对齐 */
opacity: 0;
z-index: 0; /* 确保波纹在按钮内容之下 */
}
/* 定义波纹扩散动画 */
@keyframes ripple-spread {
0% {
width: 0;
height: 0;
opacity: 0.8;
}
100% {
width: 200%; /* 足够大,覆盖整个按钮 */
height: 200%;
opacity: 0;
}
}
.ripple-button:hover::before {
animation: ripple-spread 0.6s ease-out forwards;
}这里,我们给按钮设置了
position: relative
overflow: hidden
::before
width: 0; height: 0; opacity: 0;
ripple-spread
z-index
在追求酷炫动画的同时,我们也不能忽视性能和兼容性,毕竟用户体验才是王道。
transform
opacity
transform
scale
translate
rotate
opacity
width
height
margin
padding
top
left
will-change
will-change
will-change: transform, opacity;
will-change
animation-timing-function
ease-out
cubic-bezier
animation
@keyframes
-webkit-animation
-moz-keyframes
prefers-reduced-motion
@media (prefers-reduced-motion: reduce)
/* 示例:使用prefers-reduced-motion */
@media (prefers-reduced-motion: reduce) {
.animated-button:hover {
/* 在用户偏好减少动画时,只改变背景色,移除复杂动画 */
animation: none;
transition: background-color 0.3s ease;
}
.ripple-button:hover::before {
animation: none;
}
}记住,动画的目的是为了增强用户体验,而不是炫技。在设计和实现动画时,始终把用户的感受放在第一位。
以上就是如何用css animation实现按钮悬停动画的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号