Skip to content

Tailwind CSS - 滚动外边距

Tailwind CSS:掌握 Scroll Margin(滚动边距)

Section titled “Tailwind CSS:掌握 Scroll Margin(滚动边距)”

Tailwind CSS 中的 Scroll Margin(滚动边距)工具类允许你在元素滚动到视图中时控制其周围的边距。这对于创建精致的用户体验至关重要,尤其是在包含固定元素(如粘性导航栏)的布局中。

设想一个带有粘性页眉的单页网站。当你点击导航链接跳转到某个部分(例如 #about)时,浏览器会将 #about 部分的顶部直接滚动到视口顶部。然而,你的粘性页眉会遮挡住该部分的标题!滚动边距通过在目标元素的顶部创建一个无形的“缓冲区域”来解决这个问题,确保它恰好滚动到页眉下方可见。

Tailwind 提供了一整套工具类,用于在所有侧面或单个侧面应用 scroll-margin。这里列出了一些最常见的。完整的范围可在官方文档中查阅。

类名CSS 属性
scroll-m-{amount}scroll-margin: {value};
scroll-mt-{amount}scroll-margin-top: {value};
scroll-mb-{amount}scroll-margin-bottom: {value};
scroll-ml-{amount}scroll-margin-left: {value};
scroll-mr-{amount}scroll-margin-right: {value};
scroll-mx-{amount}scroll-margin-left: {value}; scroll-margin-right: {value};
scroll-my-{amount}scroll-margin-top: {value}; scroll-margin-bottom: {value};
scroll-ms-{amount}scroll-margin-inline-start: {value}; (起始方向的逻辑属性)
scroll-me-{amount}scroll-margin-inline-end: {value}; (结束方向的逻辑属性)

{amount} 对应于 Tailwind 的默认间距比例(例如,4 代表 1rem,16 代表 4rem)。

实际应用:用粘性页眉修正导航

Section titled “实际应用:用粘性页眉修正导航”

此示例演示了 scroll-margin 的主要用例。我们有一个顶部的粘性导航栏。如果没有 scroll-mt-16,点击链接会将部分标题隐藏在导航栏后面。通过在每个部分上使用 scroll-mt-16,它们将完美地贴合在页眉下方。

<!DOCTYPE html>
<html lang="en" class="scroll-smooth">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>带粘性页眉的滚动边距</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* 对于现代开发,通常你会通过 npm 安装 Tailwind */
/* 并构建你的 CSS。CDN 非常适合像这样的快速演示。 */
</style>
</head>
<body class="bg-gray-100 text-gray-800">
<header class="bg-white/90 backdrop-blur-sm shadow-md sticky top-0 z-10">
<nav class="container mx-auto px-4 h-16 flex justify-center items-center space-x-8">
<a href="#section-1" class="hover:text-blue-600 transition-colors">第一部分</a>
<a href="#section-2" class="hover:text-blue-600 transition-colors">第二部分</a>
<a href="#section-3" class="hover:text-blue-600 transition-colors">第三部分</a>
</nav>
</header>
<main class="container mx-auto p-8 space-y-8">
<section id="section-1" class="scroll-mt-16 h-screen bg-blue-100 p-8 rounded-lg">
<h2 class="text-3xl font-bold">第一部分</h2>
<p class="mt-4">此部分具有 `scroll-mt-16` 类(4rem),它与页眉高度匹配,可防止其标题在导航到时被隐藏。</p>
</section>
<section id="section-2" class="scroll-mt-16 h-screen bg-green-100 p-8 rounded-lg">
<h2 class="text-3xl font-bold">第二部分</h2>
<p class="mt-4">此部分也具有 `scroll-mt-16` 以实现正确对齐。</p>
</section>
<section id="section-3" class="scroll-mt-16 h-screen bg-yellow-100 p-8 rounded-lg">
<h2 class="text-3xl font-bold">第三部分</h2>
<p class="mt-4">`<html>` 标签上的 `scroll-smooth` 类提供了优雅的滚动动画。</p>
</section>
</main>
</body>
</html>

同样的原则也适用于水平滚动。当项目在水平滚动容器中就位时,scroll-mr-* 或 scroll-ml-* 工具类可以添加边距。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>水平滚动边距</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-8">
<h2 class="text-2xl font-bold mb-4">水平滚动画廊</h2>
<div class="flex space-x-4 overflow-x-auto snap-x snap-mandatory p-4 bg-gray-200 rounded-lg">
<!-- 每个项目都吸附到起始位置,但 scroll-ml-4 增加了偏移量 -->
<div class="snap-start scroll-ml-4 w-80 h-48 flex-shrink-0 bg-red-300 rounded-lg flex items-center justify-center font-bold text-xl">卡片 1</div>
<div class="snap-start scroll-ml-4 w-80 h-48 flex-shrink-0 bg-blue-300 rounded-lg flex items-center justify-center font-bold text-xl">卡片 2</div>
<div class="snap-start scroll-ml-4 w-80 h-48 flex-shrink-0 bg-green-300 rounded-lg flex items-center justify-center font-bold text-xl">卡片 3</div>
<div class="snap-start scroll-ml-4 w-80 h-48 flex-shrink-0 bg-yellow-300 rounded-lg flex items-center justify-center font-bold text-xl">卡片 4</div>
<div class="snap-start scroll-ml-4 w-80 h-48 flex-shrink-0 bg-purple-300 rounded-lg flex items-center justify-center font-bold text-xl">卡片 5</div>
</div>
<p class="mt-4 text-gray-700">请注意,由于 `scroll-ml-4` 工具类,每张卡片在就位时左侧都有一个小边距。</p>
</body>
</html>
  • 与 scroll-smooth 结合使用:在 <html> 或滚动容器上使用 scroll-smooth 工具类,以获得更好的用户体验。
  • 匹配偏移量:确保你的滚动边距值(例如 scroll-mt-16)与粘性元素的高度匹配,以实现像素级完美对齐。
  • 响应式偏移量:如果你的粘性页眉在不同屏幕尺寸下高度发生变化,请使用响应式变体,例如 md:scroll-mt-20。