position:sticky粘性定位的⼏种巧妙应⽤详解
背景:position: sticky ⼜称为粘性定位,粘性定位的元素是依赖于⽤户的滚动,在 position:relative 与 position:fixed 定位之间切换。元素根据正常⽂档流进⾏定位,然后相对它的最近滚动祖先(nearest
scrolling ancestor)和 containing block (最近块级祖先 nearest block-level ancestor),包括table-related 元素,基于 top, right, bottom, 和 left的值进⾏偏移。
粘性定位可以被认为是相对定位和固定定位的混合。元素在跨越特定阈值前为相对定位,之后为固定定位。例如:
#one { position: sticky; top: 10px; }
设置了以上样式的元素,在 viewport 视⼝滚动到元素 top 距离⼩于 10px 之前,元素为相对定位。之后,元素将固定在与顶部距离 10px 的位置,直到 viewport 视⼝回滚到阈值以下。
注意:
元素定位表现为在跨越特定阈值前为相对定位,之后为固定定位。
须指定 top, right, bottom 或 left 四个阈值其中之⼀,才可使粘性定位⽣效。否则其⾏为与相对定位相同。
偏移值不会影响任何其他元素的位置。该值总是创建⼀个新的层叠上下⽂(stacking context)。
⼀个 sticky元素会固定在离它最近的⼀个拥有滚动机制的祖先上(当该祖先的 overflow 是 hidden, scroll, auto, 或 overlay时),即便这个祖先不是最近的真实可滚动祖先。
应⽤⽰例
1. 基础:头部固定
头部导航栏开始时相对定位顶部,当页⾯元素发⽣滚动时,变为和 fixed 类似的固定定位。
<main class="main-container">
<header class="main-header">HEADER</header>
<div class="main-content">MAIN CONTENT</div>
<footer class="main-footer">FOOTER</footer>
</main>
.main-container {
max-width: 500px;
height: 500px;
margin: 0 auto;
margin-top: 40px;
overflow: auto;
}
.main-container *+* {
margin-top: 20px;
}
.
main-header {
height: 50px;
}
.main-content {
min-height: 600px;
}
.main-header {
position: -webkit-sticky;
position: sticky;
top: 0;
}
2. 基础:页脚固定
页脚固定效果,开始时也较为固定定位效果,当页⾯滚动超过页脚时,页脚定位效果变为相对定位效果,可⽤于显⽰⼀些底部信息或⼴告。
<main class="main-container">
<header class="main-header">HEADER</header>
<div class="main-content">MAIN CONTENT</div>
<footer class="main-footer">FOOTER</footer>
</main>
<div class="devide"></div>
.main-container *+* {
margin-top: 20px;
}
.main-header {
height: 50px;
}
.main-content {
min-height: 600px;
}
.main-footer {
position: -webkit-sticky;
position: sticky;
bottom: 0;
border-color: red;
}
.devide {
height: 600px;
}
3. 基础:侧边栏固定
当页⾯产⽣滚动,位置超过侧边栏的顶部阈值后,侧边栏变为固定定位,可⽤于实现侧边导航栏或侧边提⽰信息及⼴告展⽰。
<div class="scroll">
<div class="wrapper cf">
<div class="content">
<h1>Scroll Down!</h1>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Ma      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus suscipit blanditiis delectus quos, soluta fuga voluptatem, facere inventore neque voluptate quaerat unde laboriosam molestiae repellat, sapiente accusamus cumque! Ipsam, illo!</p>
</div>
<div class="sidebar">
<h3>Sidebar</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab maxime fugiat perspiciatis.</p>
</div>
</div>
</div>
.cf:before, .cf:after {
content: " ";
display: table;
clear: both;
}
.cf {
*zoom: 1;
}
overflow: scroll;
padding: 0 10px;
max-width: 500px;
margin: 40px auto;
background: #FFFFFF;
}
.content {
padding: 0 15px;
width: 280px;
}
.sidebar {
padding: 20px;
width: 170px;
background: #2D2D2D;
color: #FFFFFF;
}
.content, .sidebar {
float: left;
}
.sidebar {
position: -webkit-sticky;
position: sticky;
top: 15px;
}
4. 基础:列表锚点
仅使⽤ css 就可实现页⾯滚动锚点固定效果,可⽤于实现通讯录滚动、⽇志记录滚动、其他分类列表滚动效果。
<div class="container">
<div class="list">
<div class="list-group">
<div class="list-header">A</div>
<div class="list-content">
<div>Apple</div>
<div>Artichoke</div>
<div>Aardvark</div>
<div>Ant</div>
<div>Acorn</div>
</div>
</div>
<!-- ... -->
<div class="list-group">
<div class="list-header">D</div>
<div class="list-content">
<div>Dog</div>
<div>Date</div>
<div>Danish</div>
<div>Dandelion</div>
</div>
</div>
</div>
</div>
@supports CSS at-rule 您可以指定依赖于浏览器中的⼀个或多个特定的CSS功能的⽀持声明。这被称为特性查询。该规则可以放在代码的顶层,也可以嵌套在任何其他条件组规则中。@supports (position: sticky) {
.list-header {
position: sticky;
top: 0;
}
}
.container {
width: 500px;
height: 500px;
margin: 40px auto;
position: relative;
overflow: auto;
}
.list {
min-height: 1600px;
background: #FFFFFF;
}
.list-content {
padding: 10px 20px;
}
.list-header {
padding: 10px;
background: #2D2D2D;
color: #FFFFFF;
position: relative;
font-weight: bold;
}
5. 进阶:表格表头固定
对 table 元素的 th 或 tr 设置 position: sticky; 可以实现表格头部或某⾏固定,也可将多个表格合并到⼀起,当滚动到当前表格是,固定头部⾃动变为当前表格的表头。
<div class="container">
<table>
<thead>
<tr class="red">
<th>Name</th>
<th>Age</th>
<th>Job</th>
<th>Color</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lorem.</td>
<td>Ullam.</td>
<td>Vel.</td>
<td>At.</td>
<td>Quis.</td>
</tr>
<!-- ... -->
<tr class="green">
<th>Name</th>
<th>Age</th>
<th>Job</th>
<th>Color</th>
<th>URL</th>
</tr>
</table>
</div>
.container {
height: 500px;
width: fit-content;
margin: 40px auto;
x  overflow: auto;
}
table {
text-align: left;
position: relative;
border-collapse: collapse;
}
th, td {
padding: 0.25rem;
}
tr:nth-child(even) {
background: #EFEFEF;
}
background: #dd4a63;
color: white;
}
< th {
css固定定位
background: #03C03C;
color: white;
}
tr.blue th {
background: #1580d8;
color: white;
}
th {
background: white;
position: sticky;
top: 0;
}
6. 进阶:页⾯进度条(简易)
利⽤ position: sticky; 定位,可以实现页⾯浏览进度条效果,以下是简易进度条的演⽰,实际实现中可将未滚动到顶部的元素设置为透明⾊,滚动到顶部时变为蓝⾊。
<div class="container">
<h1>Sticky Progress</h1>
<div class="sticky"></div>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatem, cumque? A, est perferendis explicabo odit possimus quisquam rem ad tempore ipsa, obcaecati ex culpa similique, aliquam corporis. Quis, nostrum expedita.</p>
<!-- ... -->
<div class="sticky"></div>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatem, cumque? A, est perferendis explicabo odit possimus quisquam rem ad tempore ipsa, obcaecati ex culpa similique, aliquam corporis. Quis, nostrum expedita.</p>
</div>
.container {
width: 500px;
height: 500px;
overflow: auto;
margin: 40px auto 40px;
padding-bottom: 500px;
box-sizing: border-box;
}
.sticky {
width: 50px;
height: 10px;
background: rgba(36, 167, 254, 0.979);
position: -webkit-sticky;
position: sticky;
top: 0;
}
.sticky:nth-of-type(2) {
transform: translateX(50px);
}
.sticky:nth-of-type(3) {
transform: translateX(100px);
}
.sticky:nth-of-type(4) {
// ...
.
sticky:nth-of-type(10) {
transform: translateX(450px);
}
7. 进阶:页⾯进度条(优化)
优化版的进度条⽀持浏览进度百分⽐显⽰,助于提升⽤户体验。
<article>
<div class="article-title">
<h1>Page Progress Bar Example</h1>
<div class="progress-wrapper">
<div class="progress-label"></div>
<progress></progress>
</div>
</div>
<img src=""/>
<p><em>All images provided at random from Codepen assets. All ipsum text provided by officeipsum.</em></p>
<p>Face time level the playing field highlights. Bake it in quick win bench mark, or paddle on both sides. Re-inventing the wheel. What do you feel you would bring to the table if you were hired for this position drink from the firehose, but quarterly sales are at a  <!-- ... -->
<img src=""/>
<p>Meeting assassin enough to wash your face so after I ran into Helen at a restaurant, I realized she was just office pretty good optics put a record on and see who dances, yet we're ahead of the curve on that one, or personal development. Bench mark bee </article>
:root {
--progress-bar-height: 4px;
--progress-bar-color: gainsboro;
--progress-bar-value-color: dodgerblue;
--progress-bar-value: 20%;
}
article {
position: relative;
padding: 24px;
width: 100%;
max-width: 700px;
margin: 60px auto;
}
.article-title {
position: sticky;
}
img {
width: 100%;
margin-bottom: 18px;
}
.progress-wrapper {
position: relative;
}
.
progress-label {
position: absolute;
right: 0;
bottom: 0;
font-size: 14px;
}
progress {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
position: absolute;
width: 100%;
height: var(--progress-bar-height);
background-color: var(--progress-bar-color);
border: none;
}
progress::-moz-progress-bar {
background-color: var(--progress-bar-value-color);
}
progress::-webkit-progress-bar {
background-color: var(--progress-bar-color);
}
progress::-webkit-progress-value {
background-color: var(--progress-bar-value-color);
}
progress::-ms-fill {
background-color: var(--progress-bar-value-color);
}
计算并显⽰百分⽐
$(document).ready(function() {
const win = $(window);
const doc = $(document);
const progressBar = $('progress');
const progressLabel = $('.progress-label');
const setValue = () => win.scrollTop();
const setMax = () => doc.height() - win.height();
const setPercent = () => und(win.scrollTop() / (doc.height() - win.height()) * 100);
<(setPercent() + '%');
progressBar.attr({ value: setValue(), max: setMax() });
<('scroll', () => {
<(setPercent() + '%');
progressBar.attr({ value: setValue() });
});
<('resize', () => {
<(setPercent() + '%');
progressBar.attr({ value: setValue(), max: setMax() });
})
});
8. 进阶:时间轴
时间轴相当于上述列表锚点的升级版,可⽤于年鉴展⽰、记事本、tudo list 等应⽤中。<div id="wrapper">
<div id="container">
<h1>Timeline</h1>
<ol class="timeline">
<li>
<h2>1997</h2>
<ol>
<li>
<h3>Lorem ipsum dolor sit amet, consectetur adipiscing elit</h3>
<p>Nam non purus vel orci molestie consequat.</p>
</li>
<li>
<h3>Etiam et velit in arcu consectetur aliquet et eu metus</h3>
<p>Sed vitae diam rhoncus, imperdiet nunc quis, lacinia nulla.</p>
</li>
</ol>
</li>
<!-- ... -->
<li>
<h2>Today</h2>
</li>
</ol>
</div>
</div>
ol.timeline ol, ol.timeline, html, body {
margin: 0;
padding: 0;
}
*, *:before, *:after {
box-sizing: border-box;
}
#wrapper {
margin: 0 auto;
max-width: 64em;
}
#container {
float: left;
padding: 1em;
width: 100%;
}
h1, h2 {
text-align: center;
}
ol.timeline, ol.timeline ol {
list-style: none;
}
ol.timeline>li {
padding-left: 2px;
position: relative;
}
ol.timeline>li:before {
background-color: #a2ed56;
content: "";
height: 100%;
top: 0;
width: 2px;
}
@media only screen and (min-width: 40em) {
ol.timeline>li:before {
left: 50%;
transform: translateX(-50%);
}
}
ol.timeline>li>h2 {
background-color: #a2ed56;
color: #1d1f20;
margin: 0;
position: -webkit-sticky;
position: sticky;
text-transform: uppercase;
top: 0;
}
@media only screen and (min-width: 40em) {
ol.timeline>li>h2 {
border-radius: 0.25em;
margin: 0 auto;
margin-bottom: 1em;
max-width: 200px;
}
}
ol.timeline>li>ol {
display: flex;
flex-wrap: wrap;
}
ol.timeline>li>ol>li {
border-top: 2px solid #a2ed56;
flex: 0 0 100%;
padding: 0 0 0.5em 1em;
}
@media only screen and (min-width: 40em) {
ol.timeline>li>ol>li {
flex-basis: 50%;
}
ol.timeline>li>ol>li:nth-child(odd) {
padding-left: 0;
padding-right: 1em;
}
ol.timeline>li>ol>li:nth-child(even) {
margin-top: 2em;
padding-right: 0;
}
}
ol.timeline>li>ol>li>h3:first-child {
color: #a2ed56;
margin-bottom: -0.75em;
}
ol.timeline>li:nth-child(6n+2):before,
ol.timeline>li:nth-child(6n+2)>h2 {
background-color: #83e4e2;
}
ol.timeline>li:nth-child(6n+2)>ol>li {
border-color: #83e4e2;
}
ol.timeline>li:nth-child(6n+2)>ol>li h3 {
color: #83e4e2;
}
ol.timeline>li:nth-child(6n+3):before,
ol.timeline>li:nth-child(6n+3)>h2 {
background-color: #fd6470;
}
ol.timeline>li:nth-child(6n+3)>ol>li {
border-color: #fd6470;
}
ol.timeline>li:nth-child(6n+3)>ol>li h3 {
color: #fd6470;
}
ol.timeline>li:nth-child(6n+4):before,
ol.timeline>li:nth-child(6n+4)>h2 {
background-color: #fca858;
}
ol.timeline>li:nth-child(6n+4)>ol>li {
border-color: #fca858;
}
ol.timeline>li:nth-child(6n+4)>ol>li h3 {
color: #fca858;
}
ol.timeline>li:nth-child(6n+5):before,
ol.timeline>li:nth-child(6n+5)>h2 {
background-color: #fddc32;
}
ol.timeline>li:nth-child(6n+5)>ol>li {
border-color: #fddc32;
}
ol.timeline>li:nth-child(6n+5)>ol>li h3 {
color: #fddc32;
}
ol.timeline>li:nth-child(6n+6):before,
ol.timeline>li:nth-child(6n+6)>h2 {
background-color: #fafafa;
}
ol.timeline>li:nth-child(6n+6)>ol>li {
border-color: #fafafa;
}
ol.timeline>li:nth-child(6n+6)>ol>li h3 {
color: #fafafa;
}
9. 进阶:⽂字堆积效果
<p>A <b>scroll</b> (from the Old French <i>escroe</i> or <i>escroue</i>), also known as a <b>roll</b>, is a roll of papyrus, parchment, or paper containing writing.</p> <h2>Structure</h2>
<p>A scroll is usually divided up into pages, which are sometimes separate sheets of papyrus or parchment glued
together at the edges, or may be marked divisions of a continuous roll of writing material. The scroll is usually
unrolled so that one page is exposed at a time, for writing or reading, with the remaining pages rolled up to the
left and right of the visible page. It is unrolled from side to side, and the text is written in lines from the top
to the bottom of the page. Depending on the language, the letters may be written left to right, right to left, or
alternating in direction (boustrophedon).</p>
<p>Some scrolls are simply rolled up pages; others may have wooden rollers on each end: Torah scrolls have rather
elaborate rollers befitting their ceremonial function.</p>
<h2>History of scroll use</h2>
<p>Scrolls were the first form of editable record keeping texts, used in Eastern Mediterranean ancient Egyptian
civilizations. Parchment scrolls were used by the Israelites among others before the codex or bound book with

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。