示例工程 11-positioning
11-positioning/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>定位实验室</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header class="site-header">
<span class="brand">POSITION LAB</span>
<nav aria-label="页面导航">
<a href="#relative">relative</a>
<a href="#absolute">absolute</a>
<a href="#fixed">fixed</a>
<a href="#sticky">sticky</a>
</nav>
</header>
<main class="page">
<p class="eyebrow">LESSON / 11</p>
<h1>定位实验室</h1>
<p class="lead">
position 决定元素是否脱离正常流、以谁为参照。relative 保留位置,
absolute 找最近的定位祖先,fixed 跟视口,sticky 在滚动时粘住。
</p>
<section class="panel" id="relative">
<h2>1. relative:相对自己偏移</h2>
<p>下面的标签用 relative 向右下偏移 6px,原位置仍然保留。</p>
<div class="relative-demo">
<span class="relative-badge">relative</span>
<span>这段文字的位置没有因为标签偏移而改变。</span>
</div>
</section>
<section class="panel" id="absolute">
<h2>2. absolute:相对定位祖先</h2>
<p>卡片设为 relative,角标用 absolute 放到右上角。</p>
<article class="card">
<span class="corner-badge">NEW</span>
<h3>绝对定位卡片</h3>
<p>角标不占文档流,卡片内容仍然从正常位置开始。</p>
</article>
</section>
<section class="panel" id="fixed">
<h2>3. fixed:相对视口</h2>
<p>右下角的固定按钮始终停留在视口,不随页面滚动。</p>
<button class="to-top" type="button">↑ 顶部</button>
</section>
<section class="panel" id="sticky">
<h2>4. sticky:滚动到阈值后粘住</h2>
<p>下面这个小标题在滚动时会粘在容器顶部。</p>
<div class="sticky-scroll">
<h3 class="sticky-title">粘性标题</h3>
<p>滚动内容一。</p>
<p>滚动内容二。</p>
<p>滚动内容三。</p>
<p>滚动内容四。</p>
<p>滚动内容五。</p>
<p>滚动内容六。</p>
</div>
</section>
</main>
<footer class="site-footer">示例数据,仅用于教程演示。</footer>
</body>
</html>