CSS 专题:动效优化与微交互
掌握现代 CSS 动画技术,打造流畅自然的用户交互体验
📚 专题目标
通过本专题学习,你将掌握:
- 动画性能优化策略
- 微交互设计模式
- 滚动驱动动画
- 物理动效实现
- 动画调试与测试
⚡ 动画性能优化
渲染层优化
/* 只使用 transform 和 opacity 进行动画 */
.optimized-animation {
transform: translateX(0);
opacity: 1;
transition: transform 0.3s ease, opacity 0.3s ease;
}
.optimized-animation:hover {
transform: translateX(10px);
opacity: 0.8;
}
/* 避免触发布局和绘制的属性 */
.bad-animation {
/* ❌ 会触发布局 */
left: 100px;
width: 200px;
height: 200px;
/* ❌ 会触发绘制 */
background-color: red;
border: 1px solid blue;
}
.good-animation {
/* ✅ 只触发合成层 */
transform: translateX(100px) scale(1.2);
opacity: 0.8;
}
/* 使用 will-change 提示浏览器 */
.will-change {
will-change: transform, opacity;
}
.will-change:hover {
transform: scale(1.1);
}
/* 动画结束后移除 will-change */
.animation-complete {
will-change: auto;
}
动画分层策略
/* 基础动画层 */
.animation-layer-1 {
/* 背景层 - 静态 */
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
.animation-layer-2 {
/* 内容层 - 轻微动画 */
transform: translateZ(0);
transition: transform 0.3s ease;
}
.animation-layer-3 {
/* 交互层 - 频繁动画 */
transform: translateZ(0);
will-change: transform;
transition: transform 0.1s ease;
}
/* 复杂动画分层 */
.complex-animation {
position: relative;
}
.complex-animation::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.1);
transform: translateX(-100%);
transition: transform 0.6s ease;
}
.complex-animation:hover::before {
transform: translateX(0);
}
动画性能监控
/* 性能友好的动画 */
.performance-optimized {
/* 使用 transform 替代位置属性 */
transform: translate3d(0, 0, 0);
/* 使用 opacity 替代 visibility */
opacity: 1;
/* 避免在动画中改变布局属性 */
/* 避免在动画中改变绘制属性 */
}
/* 动画性能测试 */
.performance-test {
animation: performanceTest 1s ease-in-out infinite;
}
@keyframes performanceTest {
0% {
transform: translateX(0) scale(1);
opacity: 1;
}
50% {
transform: translateX(100px) scale(1.1);
opacity: 0.8;
}
100% {
transform: translateX(0) scale(1);
opacity: 1;
}
}
/* 使用 contain 属性优化 */
.contain-optimized {
contain: layout style paint;
}
🎭 微交互设计
按钮微交互
/* 基础按钮动画 */
.btn {
position: relative;
padding: 12px 24px;
border: none;
border-radius: 6px;
background: #007bff;
color: white;
cursor: pointer;
overflow: hidden;
transition: all 0.3s ease;
}
.btn::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 0;
height: 0;
background: rgba(255, 255, 255, 0.2);
border-radius: 50%;
transform: translate(-50%, -50%);
transition: width 0.6s, height 0.6s;
}
.btn:hover::before {
width: 300px;
height: 300px;
}
.btn:active {
transform: scale(0.95);
}
/* 按钮加载状态 */
.btn-loading {
position: relative;
color: transparent;
}
.btn-loading::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 20px;
margin: -10px 0 0 -10px;
border: 2px solid transparent;
border-top: 2px solid #ffffff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* 按钮成功状态 */
.btn-success {
background: #28a745;
animation: successPulse 0.6s ease;
}
@keyframes successPulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
卡片微交互
/* 卡片悬停效果 */
.card {
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
overflow: hidden;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}
.card:active {
transform: translateY(-2px);
}
/* 卡片内容动画 */
.card-content {
padding: 24px;
transition: padding 0.3s ease;
}
.card:hover .card-content {
padding: 28px;
}
/* 卡片图片动画 */
.card-image {
width: 100%;
height: 200px;
object-fit: cover;
transition: transform 0.3s ease;
}
.card:hover .card-image {
transform: scale(1.05);
}
/* 卡片标签动画 */
.card-tag {
display: inline-block;
padding: 4px 12px;
background: #e9ecef;
border-radius: 20px;
font-size: 12px;
transition: all 0.3s ease;
}
.card:hover .card-tag {
background: #007bff;
color: white;
transform: scale(1.1);
}
表单微交互
/* 输入框焦点动画 */
.form-group {
position: relative;
margin-bottom: 24px;
}
.form-input {
width: 100%;
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 8px;
background: white;
transition: all 0.3s ease;
outline: none;
}
.form-input:focus {
border-color: #007bff;
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}
.form-label {
position: absolute;
top: 12px;
left: 16px;
color: #6c757d;
transition: all 0.3s ease;
pointer-events: none;
}
.form-input:focus + .form-label,
.form-input:not(:placeholder-shown) + .form-label {
top: -8px;
left: 12px;
font-size: 12px;
color: #007bff;
background: white;
padding: 0 4px;
}
/* 输入框验证状态 */
.form-input.valid {
border-color: #28a745;
}
.form-input.invalid {
border-color: #dc3545;
animation: shake 0.5s ease-in-out;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}
/* 表单提交动画 */
.form-submit {
position: relative;
overflow: hidden;
}
.form-submit::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(
90deg,
transparent,
rgba(255, 255, 255, 0.2),
transparent
);
transition: left 0.5s;
}
.form-submit:hover::before {
left: 100%;
}
🌊 滚动驱动动画
基础滚动动画
/* 滚动进入动画 */
.scroll-in {
opacity: 0;
transform: translateY(30px);
transition: all 0.6s ease;
}
.scroll-in.visible {
opacity: 1;
transform: translateY(0);
}
/* 滚动视差效果 */
.parallax {
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
/* 滚动进度条 */
.scroll-progress {
position: fixed;
top: 0;
left: 0;
width: 0%;
height: 4px;
background: linear-gradient(90deg, #ff6b6b, #4ecdc4);
z-index: 1000;
transition: width 0.1s ease;
}
/* 滚动指示器 */
.scroll-indicator {
position: fixed;
right: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 1000;
}
.scroll-dot {
width: 12px;
height: 12px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.3);
margin: 8px 0;
cursor: pointer;
transition: all 0.3s ease;
}
.scroll-dot.active {
background: white;
transform: scale(1.2);
}
高级滚动动画
/* 滚动触发动画 */
.scroll-trigger {
opacity: 0;
transform: translateY(50px);
transition: all 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.scroll-trigger.animate {
opacity: 1;
transform: translateY(0);
}
/* 滚动视差容器 */
.parallax-container {
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
perspective: 1px;
}
.parallax-element {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
transform: translateZ(-1px) scale(2);
}
/* 滚动粘性效果 */
.sticky-element {
position: sticky;
top: 0;
z-index: 100;
background: white;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
/* 滚动进度动画 */
.progress-bar {
position: fixed;
top: 0;
left: 0;
width: 0%;
height: 3px;
background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #45b7d1);
z-index: 1000;
transition: width 0.1s ease;
}
滚动动画组件
<template>
<div class="scroll-animation-container">
<!-- 滚动进度条 -->
<div class="scroll-progress" :style="{ width: scrollProgress + '%' }"></div>
<!-- 滚动指示器 -->
<div class="scroll-indicator">
<div
v-for="(section, index) in sections"
:key="index"
:class="['scroll-dot', { active: currentSection === index }]"
@click="scrollToSection(index)"
></div>
</div>
<!-- 内容区域 -->
<div class="content">
<section
v-for="(section, index) in sections"
:key="index"
:ref="el => sectionRefs[index] = el"
class="scroll-section"
:class="{ 'scroll-section--visible': currentSection === index }"
>
<h2>{{ section.title }}</h2>
<p>{{ section.content }}</p>
</section>
</div>
</div>
</template>
<script setup lang="ts">
const sections = ref([
{ title: 'Section 1', content: 'Content for section 1' },
{ title: 'Section 2', content: 'Content for section 2' },
{ title: 'Section 3', content: 'Content for section 3' },
{ title: 'Section 4', content: 'Content for section 4' }
])
const sectionRefs = ref<HTMLElement[]>([])
const currentSection = ref(0)
const scrollProgress = ref(0)
const scrollToSection = (index: number) => {
const element = sectionRefs.value[index]
if (element) {
element.scrollIntoView({ behavior: 'smooth' })
}
}
const updateScrollProgress = () => {
const scrollTop = window.pageYOffset
const docHeight = document.documentElement.scrollHeight - window.innerHeight
const progress = (scrollTop / docHeight) * 100
scrollProgress.value = Math.min(100, Math.max(0, progress))
}
const updateCurrentSection = () => {
const scrollTop = window.pageYOffset
const windowHeight = window.innerHeight
sectionRefs.value.forEach((element, index) => {
if (element) {
const rect = element.getBoundingClientRect()
if (rect.top <= windowHeight / 2 && rect.bottom >= windowHeight / 2) {
currentSection.value = index
}
}
})
}
onMounted(() => {
window.addEventListener('scroll', () => {
updateScrollProgress()
updateCurrentSection()
})
})
onUnmounted(() => {
window.removeEventListener('scroll', () => {
updateScrollProgress()
updateCurrentSection()
})
})
</script>
<style scoped>
.scroll-animation-container {
position: relative;
}
.scroll-progress {
position: fixed;
top: 0;
left: 0;
height: 4px;
background: linear-gradient(90deg, #ff6b6b, #4ecdc4);
z-index: 1000;
transition: width 0.1s ease;
}
.scroll-indicator {
position: fixed;
right: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 1000;
}
.scroll-dot {
width: 12px;
height: 12px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.3);
margin: 8px 0;
cursor: pointer;
transition: all 0.3s ease;
}
.scroll-dot.active {
background: white;
transform: scale(1.2);
}
.scroll-section {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 40px;
opacity: 0;
transform: translateY(50px);
transition: all 0.8s ease;
}
.scroll-section--visible {
opacity: 1;
transform: translateY(0);
}
</style>
🎨 物理动效
弹性动画
/* 弹性按钮 */
.elastic-button {
padding: 12px 24px;
border: none;
border-radius: 6px;
background: #007bff;
color: white;
cursor: pointer;
transition: transform 0.1s ease;
}
.elastic-button:active {
transform: scale(0.95);
}
.elastic-button:hover {
animation: elasticHover 0.6s ease;
}
@keyframes elasticHover {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
/* 弹性卡片 */
.elastic-card {
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.elastic-card:hover {
transform: scale(1.05);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}
/* 弹性输入框 */
.elastic-input {
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 8px;
transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.elastic-input:focus {
border-color: #007bff;
transform: scale(1.02);
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}
重力效果
/* 重力下拉动画 */
.gravity-drop {
animation: gravityDrop 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
@keyframes gravityDrop {
0% {
transform: translateY(-100px);
opacity: 0;
}
60% {
transform: translateY(10px);
opacity: 1;
}
80% {
transform: translateY(-5px);
}
100% {
transform: translateY(0);
}
}
/* 重力弹跳 */
.gravity-bounce {
animation: gravityBounce 1s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
@keyframes gravityBounce {
0% {
transform: translateY(-50px);
opacity: 0;
}
50% {
transform: translateY(20px);
opacity: 1;
}
70% {
transform: translateY(-10px);
}
90% {
transform: translateY(5px);
}
100% {
transform: translateY(0);
}
}
/* 重力摇摆 */
.gravity-swing {
animation: gravitySwing 0.6s ease-in-out;
}
@keyframes gravitySwing {
0% { transform: rotate(0deg); }
25% { transform: rotate(15deg); }
75% { transform: rotate(-15deg); }
100% { transform: rotate(0deg); }
}
流体动画
/* 流体背景 */
.fluid-background {
background: linear-gradient(
45deg,
#ff6b6b,
#4ecdc4,
#45b7d1,
#96ceb4,
#feca57
);
background-size: 400% 400%;
animation: fluidMove 8s ease infinite;
}
@keyframes fluidMove {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* 流体形状 */
.fluid-shape {
width: 200px;
height: 200px;
background: #ff6b6b;
border-radius: 50%;
animation: fluidShape 4s ease-in-out infinite;
}
@keyframes fluidShape {
0%, 100% {
border-radius: 50%;
transform: rotate(0deg);
}
25% {
border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
transform: rotate(90deg);
}
50% {
border-radius: 70% 30% 30% 70% / 70% 70% 30% 30%;
transform: rotate(180deg);
}
75% {
border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
transform: rotate(270deg);
}
}
/* 流体文字 */
.fluid-text {
background: linear-gradient(
45deg,
#ff6b6b,
#4ecdc4,
#45b7d1,
#96ceb4
);
background-size: 400% 400%;
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
animation: fluidText 3s ease infinite;
}
@keyframes fluidText {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
🎯 微交互组件
加载动画组件
<template>
<div class="loading-container" :class="{ 'loading-container--visible': visible }">
<div class="loading-spinner" :class="`loading-spinner--${type}`">
<div v-if="type === 'dots'" class="loading-dots">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
<div v-else-if="type === 'pulse'" class="loading-pulse"></div>
<div v-else-if="type === 'wave'" class="loading-wave">
<div class="wave"></div>
<div class="wave"></div>
<div class="wave"></div>
<div class="wave"></div>
<div class="wave"></div>
</div>
<div v-else-if="type === 'spinner'" class="loading-spinner-ring"></div>
</div>
<p v-if="text" class="loading-text">{{ text }}</p>
</div>
</template>
<script setup lang="ts">
interface Props {
visible: boolean
type?: 'dots' | 'pulse' | 'wave' | 'spinner'
text?: string
size?: 'small' | 'medium' | 'large'
}
const props = withDefaults(defineProps<Props>(), {
type: 'dots',
size: 'medium'
})
</script>
<style scoped>
.loading-container {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
}
.loading-container--visible {
opacity: 1;
visibility: visible;
}
.loading-spinner {
margin-bottom: 16px;
}
/* 点状加载动画 */
.loading-dots {
display: flex;
gap: 8px;
}
.dot {
width: 12px;
height: 12px;
background: white;
border-radius: 50%;
animation: dotBounce 1.4s ease-in-out infinite both;
}
.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes dotBounce {
0%, 80%, 100% {
transform: scale(0);
}
40% {
transform: scale(1);
}
}
/* 脉冲加载动画 */
.loading-pulse {
width: 40px;
height: 40px;
background: white;
border-radius: 50%;
animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
0% {
transform: scale(0);
opacity: 1;
}
100% {
transform: scale(1);
opacity: 0;
}
}
/* 波浪加载动画 */
.loading-wave {
display: flex;
gap: 4px;
}
.wave {
width: 8px;
height: 40px;
background: white;
border-radius: 4px;
animation: wave 1.2s ease-in-out infinite;
}
.wave:nth-child(1) { animation-delay: -1.1s; }
.wave:nth-child(2) { animation-delay: -1.0s; }
.wave:nth-child(3) { animation-delay: -0.9s; }
.wave:nth-child(4) { animation-delay: -0.8s; }
.wave:nth-child(5) { animation-delay: -0.7s; }
@keyframes wave {
0%, 40%, 100% {
transform: scaleY(0.4);
}
20% {
transform: scaleY(1);
}
}
/* 环形加载动画 */
.loading-spinner-ring {
width: 40px;
height: 40px;
border: 4px solid rgba(255, 255, 255, 0.3);
border-top: 4px solid white;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.loading-text {
color: white;
font-size: 14px;
margin: 0;
}
</style>
通知动画组件
<template>
<TransitionGroup name="notification" tag="div" class="notification-container">
<div
v-for="notification in notifications"
:key="notification.id"
:class="[
'notification',
`notification--${notification.type}`,
{ 'notification--closable': notification.closable }
]"
>
<div class="notification__icon">
<Icon :name="getIconName(notification.type)" />
</div>
<div class="notification__content">
<h4 v-if="notification.title" class="notification__title">
{{ notification.title }}
</h4>
<p class="notification__message">
{{ notification.message }}
</p>
</div>
<button
v-if="notification.closable"
@click="removeNotification(notification.id)"
class="notification__close"
>
<Icon name="close" />
</button>
</div>
</TransitionGroup>
</template>
<script setup lang="ts">
interface Notification {
id: string
type: 'success' | 'error' | 'warning' | 'info'
title?: string
message: string
duration?: number
closable?: boolean
}
const notifications = ref<Notification[]>([])
const addNotification = (notification: Omit<Notification, 'id'>) => {
const id = Date.now().toString()
const newNotification = {
...notification,
id,
duration: notification.duration || 5000,
closable: notification.closable !== false
}
notifications.value.push(newNotification)
if (newNotification.duration > 0) {
setTimeout(() => {
removeNotification(id)
}, newNotification.duration)
}
}
const removeNotification = (id: string) => {
const index = notifications.value.findIndex(n => n.id === id)
if (index > -1) {
notifications.value.splice(index, 1)
}
}
const getIconName = (type: string) => {
const iconMap = {
success: 'check-circle',
error: 'x-circle',
warning: 'exclamation-triangle',
info: 'information-circle'
}
return iconMap[type] || 'information-circle'
}
// 提供全局方法
provide('addNotification', addNotification)
</script>
<style scoped>
.notification-container {
position: fixed;
top: 20px;
right: 20px;
z-index: 1000;
display: flex;
flex-direction: column;
gap: 12px;
}
.notification {
display: flex;
align-items: flex-start;
gap: 12px;
padding: 16px;
background: white;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
min-width: 300px;
max-width: 400px;
position: relative;
overflow: hidden;
}
.notification::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 4px;
height: 100%;
}
.notification--success::before {
background: #28a745;
}
.notification--error::before {
background: #dc3545;
}
.notification--warning::before {
background: #ffc107;
}
.notification--info::before {
background: #17a2b8;
}
.notification__icon {
flex-shrink: 0;
width: 20px;
height: 20px;
margin-top: 2px;
}
.notification--success .notification__icon {
color: #28a745;
}
.notification--error .notification__icon {
color: #dc3545;
}
.notification--warning .notification__icon {
color: #ffc107;
}
.notification--info .notification__icon {
color: #17a2b8;
}
.notification__content {
flex: 1;
}
.notification__title {
margin: 0 0 4px 0;
font-size: 14px;
font-weight: 600;
color: #333;
}
.notification__message {
margin: 0;
font-size: 13px;
color: #666;
line-height: 1.4;
}
.notification__close {
flex-shrink: 0;
width: 20px;
height: 20px;
border: none;
background: none;
cursor: pointer;
color: #999;
transition: color 0.2s ease;
}
.notification__close:hover {
color: #333;
}
/* 动画 */
.notification-enter-active {
transition: all 0.3s ease;
}
.notification-leave-active {
transition: all 0.3s ease;
}
.notification-enter-from {
transform: translateX(100%);
opacity: 0;
}
.notification-leave-to {
transform: translateX(100%);
opacity: 0;
}
.notification-move {
transition: transform 0.3s ease;
}
</style>
🎯 专题总结
通过本专题学习,你掌握了:
- 动画性能优化:渲染层优化、动画分层、性能监控
- 微交互设计:按钮、卡片、表单的交互细节
- 滚动驱动动画:基础滚动动画、高级效果、组件实现
- 物理动效:弹性动画、重力效果、流体动画
- 微交互组件:加载动画、通知系统等实用组件
📝 练习题
- 实现一个完整的页面滚动动画系统
- 创建一个微交互丰富的表单组件
- 设计一个物理动效的卡片组件
- 实现一个可配置的加载动画库
- 创建一个通知系统的完整实现