滚动相关属性

56 阅读8分钟

第13章:滚动相关属性

13.1 滚动行为控制

13.1.1 scroll-behavior 属性

scroll-behavior 属性控制滚动时的动画效果,提供平滑的滚动体验。

/* 平滑滚动 */
html {
  scroll-behavior: smooth;
}

/* 特定元素平滑滚动 */
.container {
  scroll-behavior: smooth;
  overflow-y: auto;
}

/* 链接跳转平滑滚动 */
a[href^="#"] {
  scroll-behavior: smooth;
}

/* 响应式平滑滚动 */
@media (prefers-reduced-motion: no-preference) {
  .smooth-scroll {
    scroll-behavior: smooth;
  }
}

/* 禁用平滑滚动 */
.no-smooth {
  scroll-behavior: auto;
}

13.1.2 滚动捕捉

滚动捕捉允许创建精确的滚动停止点,常用于轮播图、图库等场景。

/* 水平滚动捕捉 */
.horizontal-scroll-snap {
  scroll-snap-type: x mandatory;
  overflow-x: auto;
  display: flex;
  gap: 1rem;
}

.horizontal-scroll-snap > * {
  scroll-snap-align: start;
  flex: 0 0 auto;
}

/* 垂直滚动捕捉 */
.vertical-scroll-snap {
  scroll-snap-type: y proximity;
  overflow-y: auto;
  height: 400px;
}

.vertical-scroll-snap > * {
  scroll-snap-align: center;
  margin: 1rem 0;
}

/* 双向滚动捕捉 */
.two-dimensional-snap {
  scroll-snap-type: both mandatory;
  overflow: auto;
  height: 400px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.two-dimensional-snap > * {
  scroll-snap-align: start;
}

13.2 滚动条样式

13.2.1 自定义滚动条

使用 ::-webkit-scrollbar 伪元素系列来自定义滚动条外观。

/* 基本滚动条样式 */
.custom-scrollbar::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}

.custom-scrollbar::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 6px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 6px;
  border: 2px solid #f1f1f1;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

.custom-scrollbar::-webkit-scrollbar-thumb:active {
  background: #787878;
}

.custom-scrollbar::-webkit-scrollbar-corner {
  background: #f1f1f1;
}

13.2.2 现代滚动条样式

使用新的标准属性来自定义滚动条(Firefox 支持)。

/* 标准滚动条样式 */
.modern-scrollbar {
  scrollbar-width: thin;
  scrollbar-color: #c1c1c1 #f1f1f1;
}

/* 响应式滚动条 */
@media (max-width: 768px) {
  .responsive-scrollbar {
    scrollbar-width: none; /* 移动端隐藏滚动条 */
  }
  
  .responsive-scrollbar::-webkit-scrollbar {
    display: none;
  }
}

/* 主题化滚动条 */
.themed-scrollbar {
  scrollbar-color: var(--primary-color) var(--bg-color);
}

.themed-scrollbar::-webkit-scrollbar-track {
  background: var(--bg-color);
}

.themed-scrollbar::-webkit-scrollbar-thumb {
  background: var(--primary-color);
}

13.3 滚动位置控制

13.3.1 scroll-padding 和 scroll-margin

控制滚动时的偏移量,确保内容不被固定元素遮挡。

/* 固定头部时的滚动偏移 */
html {
  scroll-padding-top: 80px; /* 头部高度 */
}

/* 锚点链接偏移 */
.anchor-target {
  scroll-margin-top: 100px;
}

/* 响应式偏移 */
@media (max-width: 768px) {
  html {
    scroll-padding-top: 60px;
  }
  
  .anchor-target {
    scroll-margin-top: 80px;
  }
}

/* 多方向偏移 */
.complex-offset {
  scroll-padding: 20px 40px 60px 80px; /* 上右下左 */
  scroll-margin: 10px 20px;
}

13.3.2 scroll-snap 高级用法

/* 强制对齐 */
.mandatory-snap {
  scroll-snap-type: y mandatory;
  scroll-padding: 20px;
}

.mandatory-snap > * {
  scroll-snap-align: start;
  scroll-margin: 10px;
}

/* 接近对齐 */
.proximity-snap {
  scroll-snap-type: x proximity;
  scroll-padding: 0 50px; /* 左右内边距 */
}

.proximity-snap > * {
  scroll-snap-align: center;
  scroll-margin: 20px;
}

/* 嵌套滚动捕捉 */
.nested-snap {
  scroll-snap-type: y mandatory;
  height: 100vh;
  overflow-y: auto;
}

.nested-snap section {
  height: 100vh;
  scroll-snap-align: start;
  scroll-snap-stop: always; /* 强制停止 */
}

.nested-snap section .inner-snap {
  scroll-snap-type: x mandatory;
  overflow-x: auto;
  display: flex;
  gap: 1rem;
}

.nested-snap section .inner-snap > * {
  scroll-snap-align: start;
  flex: 0 0 80%;
}

13.4 滚动驱动动画

13.4.1 滚动触发动画

使用 @scroll-timelineanimation-timeline 创建基于滚动的动画。

/* 滚动时间线 */
@scroll-timeline progress-timeline {
  source: selector(#scroller);
  orientation: vertical;
  scroll-offsets: 0%, 100%;
}

/* 滚动进度动画 */
.progress-indicator {
  animation: grow-progress auto linear;
  animation-timeline: progress-timeline;
}

@keyframes grow-progress {
  from {
    transform: scaleX(0);
  }
  to {
    transform: scaleX(1);
  }
}

/* 视差滚动效果 */
.parallax-section {
  transform: translateY(calc(var(--scroll) * -0.5));
}

/* 基于滚动的显示/隐藏 */
.scroll-reveal {
  opacity: 0;
  transform: translateY(50px);
  animation: reveal linear both;
  animation-timeline: view();
  animation-range: entry 0% cover 50%;
}

@keyframes reveal {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

13.4.2 视口相关动画

/* 进入视口动画 */
.fade-in-up {
  animation: fadeInUp ease both;
  animation-timeline: view();
  animation-range: entry 0% cover 30%;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(100px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 离开视口动画 */
.fade-out-down {
  animation: fadeOutDown ease both;
  animation-timeline: view();
  animation-range: exit 0% cover 100%;
}

@keyframes fadeOutDown {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(100px);
  }
}

/* 视口内动画 */
.while-in-view {
  animation: scaleInOut ease-in-out both;
  animation-timeline: view();
  animation-range: entry 0% exit 100%;
}

@keyframes scaleInOut {
  0%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  50% {
    transform: scale(1);
    opacity: 1;
  }
}

13.5 滚动性能优化

13.5.1 硬件加速滚动

/* 启用硬件加速 */
.hardware-accelerated {
  transform: translateZ(0);
  will-change: transform;
}

/* 优化滚动容器 */
.optimized-scroll {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* iOS 平滑滚动 */
  scroll-behavior: smooth;
}

/* 减少重绘 */
.minimal-repaint {
  position: relative;
  transform: translate3d(0, 0, 0);
}

/* 固定元素优化 */
.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  transform: translateZ(0);
  will-change: transform;
}

13.5.2 滚动性能最佳实践

/* 避免布局抖动 */
.stable-layout {
  contain: layout style paint; /* 包含策略 */
}

/* 优化图片加载 */
.lazy-images {
  loading: lazy;
  aspect-ratio: 16 / 9;
}

/* 虚拟滚动容器 */
.virtual-scroll {
  height: 100vh;
  overflow-y: auto;
}

.virtual-scroll .item {
  height: 100px;
  contain: strict;
}

/* 防抖滚动事件 */
.debounced-scroll {
  scroll-behavior: smooth;
}

/* 减少滚动监听 */
.optimized-listeners {
  /* 使用 Intersection Observer 替代 scroll 事件 */
}

13.6 移动端滚动优化

13.6.1 触摸滚动优化

/* iOS 弹性滚动 */
.ios-bounce {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* 防止页面滚动 */
.no-body-scroll {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
}

/* 触摸选择优化 */
.touch-optimized {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

.touch-optimized:active {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
}

/* 移动端滚动条隐藏 */
.mobile-scrollbar {
  scrollbar-width: none;
}

.mobile-scrollbar::-webkit-scrollbar {
  display: none;
}

13.6.2 移动端特定样式

/* 安全区域适配 */
.safe-area {
  padding: env(safe-area-inset-top) 
           env(safe-area-inset-right) 
           env(safe-area-inset-bottom) 
           env(safe-area-inset-left);
}

/* 响应式滚动行为 */
@media (hover: none) and (pointer: coarse) {
  .touch-scroll {
    scroll-behavior: auto; /* 触摸设备禁用平滑滚动 */
    -webkit-overflow-scrolling: touch;
  }
}

/* 移动端视口单位 */
.mobile-viewport {
  height: 100vh;
  height: 100dvh; /* 动态视口高度 */
  max-height: -webkit-fill-available;
}

13.7 高级滚动模式

13.7.1 无限滚动

/* 无限滚动容器 */
.infinite-scroll {
  height: 100vh;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.infinite-scroll .item {
  height: 100px;
  scroll-snap-align: start;
}

/* 加载指示器 */
.loading-indicator {
  opacity: 0;
  animation: pulse 2s infinite;
  scroll-margin: 100px;
}

.loading-indicator.visible {
  opacity: 1;
}

@keyframes pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

13.7.2 分页滚动

/* 分页滚动布局 */
.paged-scroll {
  scroll-snap-type: y mandatory;
  height: 100vh;
  overflow-y: auto;
}

.paged-scroll .page {
  height: 100vh;
  scroll-snap-align: start;
  scroll-snap-stop: always;
}

/* 页码指示器 */
.page-indicator {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
}

.page-indicator .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ccc;
  margin: 10px 0;
  transition: all 0.3s ease;
}

.page-indicator .dot.active {
  background: #007bff;
  transform: scale(1.5);
}

13.8 滚动事件与交互

13.8.1 CSS 与 JavaScript 结合

/* 滚动状态类 */
.scrolled {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

.scroll-progress {
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.1s linear;
}

.scroll-progress.animated {
  transform: scaleX(var(--scroll-progress));
}

/* 视差层 */
.parallax-layer {
  transform: translateY(calc(var(--parallax-depth) * var(--scroll-y)));
  will-change: transform;
}

.parallax-layer-1 { --parallax-depth: 0.5; }
.parallax-layer-2 { --parallax-depth: 0.3; }
.parallax-layer-3 { --parallax-depth: 0.1; }

13.8.2 滚动触发动画

/* 滚动进度动画 */
@keyframes scrollProgress {
  from {
    stroke-dashoffset: 100;
  }
  to {
    stroke-dashoffset: 0;
  }
}

.scroll-progress-circle {
  animation: scrollProgress linear both;
  animation-timeline: scroll();
}

/* 基于滚动的变换 */
.scroll-transform {
  transform: 
    rotate(calc(var(--scroll) * 1deg))
    scale(calc(1 + var(--scroll) * 0.01));
}

/* 滚动颜色变化 */
.scroll-color {
  background: color-mix(
    in srgb,
    var(--start-color) calc(var(--scroll) * 100%),
    var(--end-color)
  );
}

13.9 无障碍滚动支持

13.9.1 键盘导航支持

/* 焦点滚动 */
.scrollable:focus {
  outline: 2px solid #007bff;
  outline-offset: 2px;
}

/* 键盘滚动提示 */
.scrollable::before {
  content: "使用方向键或 Page Up/Down 滚动";
  position: absolute;
  top: -30px;
  left: 0;
  background: #333;
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 12px;
  opacity: 0;
  transition: opacity 0.3s;
}

.scrollable:focus::before {
  opacity: 1;
}

/* 减少动画支持 */
@media (prefers-reduced-motion: reduce) {
  .smooth-scroll {
    scroll-behavior: auto;
  }
  
  .scroll-animation {
    animation: none;
  }
}

13.9.2 屏幕阅读器支持

/* 滚动区域标识 */
.scroll-region {
  aria-label: "可滚动内容区域";
}

/* 滚动状态提示 */
.scroll-status {
  position: absolute;
  left: -10000px;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

.scroll-status:focus {
  position: static;
  width: auto;
  height: auto;
}

13.10 实际应用案例

13.10.1 现代网站导航

/* 固定导航栏 */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  z-index: 1000;
}

.navbar.scrolled {
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

/* 平滑锚点滚动 */
html {
  scroll-behavior: smooth;
  scroll-padding-top: 80px;
}

.section {
  scroll-margin-top: 100px;
}

13.10.2 产品展示轮播

/* 水平滚动轮播 */
.carousel {
  scroll-snap-type: x mandatory;
  overflow-x: auto;
  display: flex;
  gap: 2rem;
  padding: 2rem;
  scroll-padding: 2rem;
}

.carousel-item {
  scroll-snap-align: center;
  flex: 0 0 300px;
  scroll-margin: 1rem;
}

/* 自定义滚动条 */
.carousel::-webkit-scrollbar {
  height: 8px;
}

.carousel::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.carousel::-webkit-scrollbar-thumb {
  background: #007bff;
  border-radius: 4px;
}

13.11 总结

滚动相关属性是现代CSS3中非常重要的一部分,它们不仅改善了用户体验,还提供了丰富的交互可能性。从基本的滚动行为控制到高级的滚动驱动动画,这些特性让开发者能够创建更加流畅、直观和吸引人的滚动体验。

关键要点:

  • 使用 scroll-behavior: smooth 提供平滑滚动体验
  • 利用滚动捕捉创建精确的滚动停止点
  • 自定义滚动条样式提升视觉一致性
  • 优化滚动性能确保流畅体验
  • 考虑无障碍访问支持所有用户
  • 结合JavaScript实现复杂的滚动交互

通过合理运用这些滚动相关属性,可以显著提升网站的用户体验和现代感。