position:sticky 粘性定位

news2024/10/5 14:46:04

1、什么是粘性定位?

粘性定位它基于用户的滚动位置来定位。

粘性定位的元素是依赖于用户的滚动,在 position:relative 与 position:fixed 定位之间切换。

它的行为就像 position:relative; 而当页面滚动超出目标区域时,它的表现就像 position:fixed;,它会固定在目标位置。

注意: Internet Explorer, Edge 15 及更早 IE 版本不支持 sticky 定位。 Safari 需要使用 -webkit- prefix 

 例如:

div.sticky {
    position: -webkit-sticky; /* Safari */
    position: sticky;
    top: 10px;
}

设置了以上元素,在屏幕范围(viewport)视口滚动到元素top距离小于10px之前,div为相对定位。之后元素将固定在与顶部距离10px的位置,直到viewport视口回滚到10px以内。

特点:

  • 元素定位表现为在跨越特定阈值前为相对定位,之后为固定定位。
  • 这个特定阈值指的是 top, right, bottom 或 left 之一,换言之,指定 top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效。否则其行为与相对定位相同。
  • 偏移值不会影响任何其他元素的位置。该值总是创建一个新的层叠上下文(stacking context)。
  • sticky元素会固定在离它最近的一个拥有滚动机制的祖先上,如果祖先元素都不可以滚动,那么是相对于viewport来计算元素的偏移量。

2、原理

视口元素:显示内容的区域。会设置宽高。一般会设置 overflow:hidden。             

容器元素:离 sticky 元素最近的能滚动的祖先元素。             

粘性约束元素:粘性定位的父元素。有时也会出现粘性约束元素就是容器元素的情况。         

sticky 元素:设置了 position: sticky; 的元素。

滚动时,sticky 元素设置的 left, right, top, bottom 的值相对的是容器元素。当粘性约束元素滚出视口时,sticky 元素也会滚出视口。

3、可能存在的不生效的情况

3.1 未指定 top, right, top 和 bottom 中的任何一个值

此时,设置 position: sticky 相当于设置 position: relative

要生效,要指定 top, right, top 或 bottom 中的任何一个值。

3.2 垂直滚动时,粘性约束元素高度小于等于 sticky 元素高度

当粘性约束元素滚出视口时,sticky 元素也会滚出视口。粘性约束元素比 sticky 元素还小,sticky 元素没有显示固定定位状态的机会。

水平滚动时,粘性约束元素宽度小于等于 sticky 元素宽度时,也不会生效。

3.3 粘性约束元素和容器元素之间存在 overflow: hidden 的元素

示例如下:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body {
				margin: 0;
				padding: 0;
			}

			.viewport {
				width: 375px;
				height: 300px;
				background-color: pink;
				padding: 20px;
				overflow: auto;
			}

			.container {
				height: 500px;
				background-color: skyblue;
				padding: 20px;
			}

			.box {
				height: 300px;
				background-color: lightgoldenrodyellow;
				padding: 20px;
			}

			.stick-elem {
				height: 50px;
				background-color: seagreen;
				padding: 20px;
				position: -webkit-sticky;
				position: sticky;
				top: 50px;
			}
		</style>
	</head>
	<body>
		<!-- 视口元素 -->
		<div class="viewport">
			<h3>视口元素</h3>
			<!-- 容器元素 -->
			<div class="container">
				<h3>容器元素</h3>
				<div style="overflow: hidden;">
					<!-- 粘性约束元素 -->
					<div class="box">
						<h3>粘性约束元素</h3>
						<!-- sticky 元素 -->
						<div class="stick-elem">
							<h3>sticky 元素</h3>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>

4、应用示例

4.1 头部固定

头部导航栏开始时相对定位顶部,当页面元素发生滚动时,变为和 fixed 类似的固定定位。

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body {
				margin: 0;
				padding: 0;
			}

			.main-container {
				max-width: 375px;
				height: 300px;
				overflow: auto;
			}

			.main-header {
				height: 50px;
				background: pink;
				position: -webkit-sticky;
				position: sticky;
				top: 0;
			}

			.main-content {
				min-height: 500px;
				background: skyblue;
			}

			.main-footer {
                height: 50px;
				background: seagreen;
			}
		</style>
	</head>
	<body>
		<main class="main-container">
			<header class="main-header">HEADER</header>
			<div class="main-content">MAIN CONTENT</div>
			<footer class="main-footer">FOOTER</footer>
		</main>
	</body>
</html>

 4.2 页脚固定

页脚固定效果,开始时页脚为固定定位效果,当页面滚动超过页脚时,页脚定位效果变为相对定位效果,可用于显示一些底部信息或广告。

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body {
				margin: 0;
				padding: 0;
			}

			.main-container {
				max-width: 375px;
				height: 300px;
				overflow: auto;
			}

			.main-header {
				height: 50px;
				background: pink;
				position: -webkit-sticky;
			}

			.main-content {
				min-height: 500px;
				background: skyblue;
			}

			.main-footer {
				height: 50px;
				background: seagreen;
				position: sticky;
				bottom: 0;
			}
		</style>
	</head>
	<body>
		<main class="main-container">
			<header class="main-header">HEADER</header>
			<div class="main-content">MAIN CONTENT</div>
			<footer class="main-footer">FOOTER</footer>
		</main>
	</body>
</html>

4.3 侧边栏固定

当页面产生滚动,位置超过侧边栏的 顶部阈值 后,侧边栏变为固定定位,可用于实现侧边导航栏或侧边提示信息及广告展示。

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body {
				margin: 0;
				padding: 0;
			}

			.scroll {
				width: 375px;
				height: 300px;
				overflow: scroll;
				padding: 0 10px;
				box-sizing: border-box;
				background: pink;
			}

			.wrapper {
				display: flex;
			}

			.content {
				padding: 0 15px;
				width: 200px;
			}

			.sidebar {
				width: 175px;
				height: 100%;
				padding: 20px;
				background: #2D2D2D;
				color: #FFFFFF;
				box-sizing: border-box;
				position: -webkit-sticky;
				position: sticky;
				top: 15px;
			}
		</style>
	</head>
	<body>
		<div class="scroll">
			<div class="wrapper">
				<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. Mauris placerat eleifend leo.</p>
					<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>侧边栏</h3>
				</div>
			</div>
		</div>
	</body>
</html>

4.4 列表描点

仅使用 css 就可实现页面滚动锚点固定效果,可用于实现通讯录滚动、日志记录滚动、其他分类列表滚动效果。

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body {
				margin: 0;
				padding: 0;
			}

			.container {
				width: 375px;
				height: 300px;
				position: relative;
				overflow: auto;
			}

			.list {
				min-height: 500px;
				background: pink;
			}

			.list-content,
			.list-content>div {
				padding: 10px 20px;
			}

			.list-header {
				padding: 10px;
				background: #2D2D2D;
				color: #FFFFFF;
				font-weight: bold;
				position: sticky;
				top: 0;
			}
		</style>
	</head>
	<body>
		<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">B</div>
					<div class="list-content">
						<div>Big</div>
						<div>Body</div>
						<div>Base</div>
						<div>Baby</div>
						<div>But</div>
					</div>
				</div>
				<div class="list-group">
					<div class="list-header">C</div>
					<div class="list-content">
						<div>Car</div>
						<div>Cat</div>
						<div>Cute</div>
						<div>Can</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>
	</body>
</html>

 4.5 表格表头固定

对 table 元素的 th 或 tr 设置 position: sticky; 可以实现表格头部或某行固定,也可将多个表格合并到一起,当滚动到当前表格是,固定头部自动变为当前表格的表头。

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body {
				margin: 0;
				padding: 0;
			}

			.container {
				width: 375px;
				height: 300px;
				width: fit-content;
				overflow: auto;
			}

			table {
				text-align: left;
				position: relative;
				border-collapse: collapse;
			}

			th,
			td {
				padding: 30px 17px;
			}

			tr:nth-child(even) {
				background: #EFEFEF;
			}

			tr.red th {
				background: #dd4a63;
				color: white;
			}

			tr.green th {
				background: #03C03C;
				color: white;
			}

			tr.blue th {
				background: #1580d8;
				color: white;
			}

			th {
				background: white;
				position: sticky;
				top: 0;
			}
		</style>
	</head>
	<body>
		<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>
						<td>Lorem.</td>
						<td>Ullam.</td>
						<td>Vel.</td>
						<td>At.</td>
						<td>Quis.</td>
					</tr>
					<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>
					<tr>
						<td>Lorem.</td>
						<td>Ullam.</td>
						<td>Vel.</td>
						<td>At.</td>
						<td>Quis.</td>
					</tr>
					<tr>
						<td>Lorem.</td>
						<td>Ullam.</td>
						<td>Vel.</td>
						<td>At.</td>
						<td>Quis.</td>
					</tr>
					<tr class="blue">
						<th>Name</th>
						<th>Age</th>
						<th>Job</th>
						<th>Color</th>
						<th>URL</th>
					</tr>
					<tr>
						<td>Lorem.</td>
						<td>Ullam.</td>
						<td>Vel.</td>
						<td>At.</td>
						<td>Quis.</td>
					</tr>
					<tr>
						<td>Lorem.</td>
						<td>Ullam.</td>
						<td>Vel.</td>
						<td>At.</td>
						<td>Quis.</td>
					</tr>
					<tr>
						<td>Lorem.</td>
						<td>Ullam.</td>
						<td>Vel.</td>
						<td>At.</td>
						<td>Quis.</td>
					</tr>
				</tbody>
			</table>
		</div>
	</body>
</html>

4.6 页面进度条(简易)

利用 position: sticky; 定位,可以实现页面浏览进度条效果,以下是简易进度条的演示,实际实现中可将未滚动到顶部的元素设置为透明色,滚动到顶部时变为蓝色。

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body {
				margin: 0;
				padding: 0;
			}

			.container {
				width: 400px;
				height: 300px;
				overflow: auto;
				padding-bottom: 500px;
				box-sizing: border-box;
				background: pink;
			}

			.sticky {
				width: 100px;
				height: 10px;
				background: rgba(36, 167, 254, 0.979);
				position: -webkit-sticky;
				position: sticky;
				top: 0;
			}

			.sticky:nth-of-type(2) {
				transform: translateX(100px);
			}

			.sticky:nth-of-type(3) {
				transform: translateX(200px);
			}

			.sticky:nth-of-type(4) {
				transform: translateX(300px);
			}
		</style>
	</head>
	<body>
		<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 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>
	</body>
</html>

参考资料:javascript - position:sticky 粘性定位的几种巧妙应用 - 个人文章 - SegmentFault 思否

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/385983.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

图机器学习-图神经网络

图神经网络 前面讲了图机器学习的一些传统方法&#xff0c;现在正式进入到课程的核心部分&#xff1a;图神经网络。 Design of GNN 那么图神经网络和我们之前接触的一些深度神经网络有什么不同呢&#xff1f; 对于别的类型的神经网络&#xff0c;往往我们都是处理一些类似网…

win环境nginx下载安装和基本操作使用解析

win环境nginx下载安装和基本操作是我们技术人员必备的技能&#xff0c;今天我们大概梳理一下&#xff1a; 下载 地址&#xff1a;nginx: download 进入后点击&#xff1a; 选择某个版本&#xff0c;就可以下载了&#xff0c;然后解压到某个目录&#xff0c;进入配置文件&…

RAM IP Core

官方文档&#xff1a;ZYNQ 存储资源指导手册 (DS109) RAM 全称 Random Access Memory&#xff0c;随机存取存储器。 随时将数据写入任意指定地址的存储单元&#xff0c;或从任意地址读出数据。读写的速度是由时钟频率决定的。 RAM主要用于存放程序运行的中间数据、运算结果等…

MySQL实战45讲深入浅出索引下

select * from T where k between 3 and 5这个语句的执行流程是&#xff1a; 在 k 索引树上找到 k3 的记录&#xff0c;取得 ID 300&#xff1b;再到 ID 索引树查到 ID300 对应的 R3&#xff1b;在 k 索引树取下一个值 k5&#xff0c;取得 ID500.再回到 ID 索引树查到 ID500 …

深度学习-第T1周——实现mnist手写数字识别

深度学习-第T1周——实现mnist手写数字识别深度学习-第P1周——实现mnist手写数字识别一、前言二、我的环境三、前期工作1、导入依赖项并设置GPU2、导入数据集3、归一化4、可视化图片5、调整图片格式四、构建简单的CNN网络五、编译并训练模型1、设置超参数2、编写训练函数六、预…

【Python】Jupyter .ipynb

Jupyter启动Hello JupyterMarkdown纵然 Anaconda Pycharm 非常好用&#xff0c;但是既然学到 Jupyter&#xff0c;那就多掌握一份技能&#xff0c;毕竟 Jupyter 的确有他的优势在。 我认为 Jupyter 对于 Python 初学者来说&#xff0c;非常友善&#xff0c;他将一整个代码划分…

c#学习笔记

目录1.语句块2.Write和WriteLine的区别&#xff1a;3.params4.托管代码和非托管代码5.DllImport的使用&#xff1a;6.WriteLine、ReadLine和ReadKey&#xff1a;7.C#中访问修饰符8.类型的实例化9.成员可以分为两种&#xff1a;数据成员和函数成员10.枚举enum和结构struct的区别…

使用Debussy加载设计项目

Debussy是NOVAS Software, Inc(思源科技)用来进行HDL Debug & Analysis的工具&#xff0c;这套软体主要不是用来跑模拟或看波形&#xff0c;它最强大的功能是&#xff1a;能够在HDL source code、schematic diagram、waveform、state bubble diagram之间&#xff0c;即时做…

蓝桥杯-考勤刷卡

蓝桥杯-考勤刷卡1、问题描述2、解题思路3、代码实现1、问题描述 小蓝负责一个公司的考勤系统, 他每天都需要根据员工刷卡的情况来确定 每个员工是否到岗。 当员工刷卡时, 会在后台留下一条记录, 包括刷卡的时间和员工编号, 只 要在一天中员工刷过一次卡, 就认为他到岗了。 现在…

电子技术——数字逻辑反相器

电子技术——数字逻辑反相器 在学习完如何通过CMOS数字电路实现组合逻辑&#xff0c;接下来我们评估这种数字CMOS电路的性能。首先&#xff0c;我们考虑最基本的部件——反相器。 电压传导特性 下图是一个反相器的原理图&#xff1a; 在之前&#xff0c;我们已经介绍了MOSFE…

ATTCK v12版本战术介绍持久化(三)

一、引言在前几期文章中我们介绍了ATT&CK中侦察、资源开发、初始访问、执行战术、持久化战术&#xff08;一&#xff09;及&#xff08;二&#xff09;知识&#xff0c;本期我们为大家介绍ATT&CK 14项战术中持久化战术&#xff08;三&#xff09;涉及的剩余子技术&…

汇编语言程序设计(一)

前言 在学习汇编语言之前&#xff0c;我们应该要知道汇编语言他是一门怎么样的语言。汇编语言是直接工作在硬件上的一门编程语言&#xff0c;学习汇编语言之前最好先了解一下计算机硬件系统的结构和工作原理。学习汇编语言的重点是学习如何利用硬件系统的编程结构和指令集进而…

高通平台开发系列讲解(显示篇)Gralloc模块

文章目录 一、什么是Gralloc模块二、Gralloc加载流程三、Gralloc模块的加载四、Gralloc设备的加载五、 fb设备的加载沉淀、分享、成长,让自己和他人都能有所收获!😄 📢本篇将介绍显示过程中Gralloc模块。 一、什么是Gralloc模块 通过加载Gralloc抽象层,可以打开fb设备和…

【游戏逆向】寻路函数隐藏检测点分析

案例&#xff1a; 某游戏出现调用寻路函数失败异常崩溃。 基本情况分析&#xff1a; 在刚登陆游戏的时候直接调用寻路函数崩溃。 手动寻路以后再调用寻路不崩溃。(排除了函数编写错误的可能) 猜测可能检测方法&#xff1a; 有某一个标志位(全局类型)在游戏刚登陆的时候没…

【VS】【Qt】vs+ qt .natvis 失效问题

【VS】【Qt】vs qt .natvis 失效问题 .natvis文件用于调试时候自定义显示自定义类型的可视化提示。 一般这类文件存在 C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\Common7\Packages\Debugger\Visualizers路径下。 .natvis文件的规则在此不介绍&#xf…

剑指 Offer 23 链表中环的入口结点

摘要 链表中环的入口结点_牛客题霸_牛客网 剑指 Offer II 022. 链表中环的入口节点 141. 环形链表 142. 环形链表 II 一、是否有环&#xff08;快慢指针&#xff09; 我们可以根据上述思路来解决本题。具体地&#xff0c;我们定义两个指针&#xff0c;一快一慢。慢指针每次…

了解Cesium的笛卡尔类型和位置变量的单位

var position Cesium.Cartesian3.fromDegrees(100, 100, 2); 前文输出了position变量&#xff0c;是一个六位数&#xff0c;还带有多位小数&#xff1b;下面来看一下相关类的定义和position的单位&#xff1b;单位如果不对的话放置的模型可能到屏幕外面&#xff1b; 看一下相…

太赫兹频段耦合器设计相关经验总结

1拿到耦合器的频段后&#xff0c;确定中心频率和波导的宽度和高度 此处贴一张不同频段对应的波导尺寸图 需要注意的是1英寸 2.54厘米&#xff0c;需注意换算 具体网址&#xff1a;矩形波导尺寸 | 扩维 (qualwave.com) 仅列举我比较常用的太赫兹频段部分 2.以220~320GHz频段&a…

《后疫情时代大众行为及情感变化研究报告》|人们的饮食、工作、运动、社交、娱乐、学习、购物有哪些改变?

疫情三年&#xff0c;改变了很多人的生命轨迹。有人长期居家&#xff0c;宅出了一身的厨艺&#xff1b;有人启动线上模式&#xff0c;习惯了居家办公&#xff1b;有人失去了工作&#xff0c;生活一度陷入困境&#xff1b;有人痛定思痛&#xff0c;准备换个城市换个活法。 个体…

项目管理工具dhtmlxGantt甘特图入门教程(十六):数据序列转化为XML和JSON

这篇文章给大家讲解dhtmlxGantt将数据转化为XML何JSON格式。 dhtmlxGantt是用于跨浏览器和跨平台应用程序的功能齐全的Gantt图表&#xff0c;可满足应用程序的所有需求&#xff0c;是完善的甘特图图表库 DhtmlxGantt正版试用下载&#xff08;qun&#xff1a;764148812&#…