七、动画与过渡
7.1 过渡
.element {
/* 过渡属性 */
transition-property: all;
transition-property: width, height, background-color;
transition-property: none;
/* 过渡时长 */
transition-duration: 0.3s;
transition-duration: 0.3s, 0.5s;
/* 过渡时机函数 */
transition-timing-function: ease; /* 缓动 */
transition-timing-function: linear; /* 线性 */
transition-timing-function: ease-in; /* 加速 */
transition-timing-function: ease-out; /* 减速 */
transition-timing-function: ease-in-out; /* 先加速后减速 */
transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
/* 过渡延迟 */
transition-delay: 0s;
transition-delay: 0.2s;
/* 简写 */
transition: all 0.3s ease;
transition: width 0.3s, height 0.5s;
}
/* 过渡示例 */
.button {
background: #007bff;
padding: 10px 20px;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.button:hover {
background: #0056b3;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0,123,255,0.3);
}
.card {
transition: transform 0.3s, box-shadow 0.3s;
}
.card:hover {
transform: translateY(-8px);
box-shadow: 0 12px 24px rgba(0,0,0,0.1);
}
7.2 关键帧动画
/* 定义动画 */
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes slideIn {
0% {
transform: translateX(-100%);
opacity: 0;
}
100% {
transform: translateX(0);
opacity: 1;
}
}
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.05);
}
100% {
transform: scale(1);
}
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
/* 使用动画 */
.element {
/* 动画名称 */
animation-name: fadeIn;
/* 动画时长 */
animation-duration: 0.5s;
/* 动画时机函数 */
animation-timing-function: ease;
/* 动画延迟 */
animation-delay: 0s;
/* 动画次数 */
animation-iteration-count: 1;
animation-iteration-count: infinite;
/* 动画方向 */
animation-direction: normal; /* 正向 */
animation-direction: reverse; /* 反向 */
animation-direction: alternate; /* 交替 */
animation-direction: alternate-reverse; /* 反向交替 */
/* 动画填充模式 */
animation-fill-mode: none; /* 默认 */
animation-fill-mode: forwards; /* 保持最后状态 */
animation-fill-mode: backwards; /* 应用第一帧 */
animation-fill-mode: both; /* 同时应用 */
/* 动画播放状态 */
animation-play-state: running;
animation-play-state: paused;
/* 简写 */
animation: fadeIn 0.5s ease forwards;
animation: slideIn 0.6s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}
/* 动画示例 */
.loader {
width: 40px;
height: 40px;
border: 3px solid #f3f3f3;
border-top: 3px solid #007bff;
border-radius: 50%;
animation: rotate 1s linear infinite;
}
.notification {
animation: slideIn 0.3s ease forwards;
}
.notification.hide {
animation: fadeOut 0.3s ease forwards;
}
@keyframes fadeOut {
to {
opacity: 0;
transform: translateY(-20px);
}
}
/* 滚动动画 */
.reveal {
opacity: 0;
transform: translateY(30px);
transition: all 0.6s ease;
}
.reveal.active {
opacity: 1;
transform: translateY(0);
}
八、CSS 变量
/* 定义变量 */
:root {
/* 颜色变量 */
--primary-color: #007bff;
--secondary-color: #6c757d;
--success-color: #28a745;
--danger-color: #dc3545;
--warning-color: #ffc107;
--info-color: #17a2b8;
--light-color: #f8f9fa;
--dark-color: #343a40;
/* 间距变量 */
--spacing-xs: 4px;
--spacing-sm: 8px;
--spacing-md: 16px;
--spacing-lg: 24px;
--spacing-xl: 32px;
/* 字体变量 */
--font-family-base: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
--font-size-sm: 12px;
--font-size-base: 16px;
--font-size-lg: 20px;
--font-size-xl: 24px;
/* 圆角变量 */
--border-radius-sm: 4px;
--border-radius-md: 8px;
--border-radius-lg: 16px;
--border-radius-round: 50%;
/* 阴影变量 */
--shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
--shadow-md: 0 4px 6px rgba(0,0,0,0.1);
--shadow-lg: 0 10px 15px rgba(0,0,0,0.1);
}
/* 使用变量 */
.element {
color: var(--primary-color);
padding: var(--spacing-md);
font-family: var(--font-family-base);
border-radius: var(--border-radius-md);
box-shadow: var(--shadow-md);
}
/* 后备值 */
.element {
color: var(--primary-color, #007bff);
}
/* 动态修改变量 */
@media (prefers-color-scheme: dark) {
:root {
--light-color: #1a1a1a;
--dark-color: #f8f9fa;
}
}
/* JavaScript 修改 */
// document.documentElement.style.setProperty('--primary-color', '#ff0000');
/* 变量计算 */
.element {
--padding: 20px;
padding: var(--padding);
margin: calc(var(--padding) * 2);
}
/* 主题切换示例 */
[data-theme="dark"] {
--bg-color: #1a1a1a;
--text-color: #f8f9fa;
--border-color: #333;
}
[data-theme="light"] {
--bg-color: #ffffff;
--text-color: #333;
--border-color: #ddd;
}
body {
background: var(--bg-color);
color: var(--text-color);
}
九、现代 CSS 特性
9.1 容器查询
js/* 定义容器 */
.card-container {
container-type: inline-size;
container-name: card;
}
/* 或简写 */
.card {
container: card / inline-size;
}
/* 容器查询 */
@container card (min-width: 400px) {
.card-content {
display: flex;
gap: 20px;
}
.card-image {
width: 200px;
}
}
@container card (max-width: 399px) {
.card-content {
display: block;
}
.card-image {
width: 100%;
}
}
9.2 CSS 嵌套
/* 原生 CSS 嵌套(CSS Nesting) */
.card {
background: white;
border-radius: 8px;
& .card-header {
padding: 16px;
border-bottom: 1px solid #eee;
& h2 {
font-size: 1.25rem;
margin: 0;
}
}
& .card-body {
padding: 16px;
}
&:hover {
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
}
/* 等价于 */
.card {
background: white;
border-radius: 8px;
}
.card .card-header {
padding: 16px;
border-bottom: 1px solid #eee;
}
.card .card-header h2 {
font-size: 1.25rem;
margin: 0;
}
.card .card-body {
padding: 16px;
}
.card:hover {
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
9.3 视口单位
/* 视口单位 */
.element {
width: 100vw; /* 视口宽度的100% */
height: 100vh; /* 视口高度的100% */
/* 动态视口单位 */
width: 100dvw; /* 动态视口宽度 */
height: 100dvh; /* 动态视口高度 */
/* 小视口单位 */
width: 100svw; /* 小视口宽度 */
height: 100svh; /* 小视口高度 */
/* 大视口单位 */
width: 100lvw; /* 大视口宽度 */
height: 100lvh; /* 大视口高度 */
/* 视口最小/最大单位 */
width: 100vmin; /* 视口宽度和高度的最小值 */
height: 100vmax; /* 视口宽度和高度的最大值 */
}
/* 全屏元素 */
.hero {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
9.4 颜色函数
/* 现代颜色函数 */
.element {
/* 相对颜色 */
--primary: #007bff;
background: color-mix(in srgb, var(--primary), white 50%);
/* 颜色混合 */
background: color-mix(in srgb, red 30%, blue 70%);
/* 相对颜色语法 */
background: rgb(from var(--primary) r g b / 0.5);
/* 对比度颜色 */
color: contrast-color(var(--primary) vs black, white);
/* OKLCH 颜色空间 */
background: oklch(0.6 0.2 45deg);
background: oklch(from var(--primary) l c h);
/* Lab 颜色空间 */
background: lab(54% 80 70);
}