position:sticky;新属性小记

偶然得知了position:sticky这个新属性,想起了从前写这个功能时的蛋疼时光。。。一堆高度算来算去,比来比去,emmm。。。

position:sticky;基于用户的滚动位置来定位。

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

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

元素定位表现为在跨越特定阈值前为相对定位,之后为固定定位。

这个特定阈值指的是 top, right, bottom 或 left 之一,换言之,指定 top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效。否则其行为与相对定位相同。
 

下附demo

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>定位的新属性position:sticky;</title>
		<style type="text/css">
			.top {
				height: 500px;
				background: yellowgreen;
			}
			
			.pos {
				height: 100px;
				background: blue;
				position: -webkit-sticky;
				position: sticky;
				top: 0;
				/*阈值*/
			}
			
			.pos22 {
				height: 100px;
				background: yellow;
				position: -webkit-sticky;
				position: sticky;
				top: 100px;
				/*阈值*/
			}
			
			.bottom1 {
				height: 500px;
				background: grey;
			}
			
			.bottom {
				height: 1600px;
				background: grey;
			}
		</style>
	</head>

	<body>
		<div class="top">上边元素</div>
		<div class="pos">定位元素</div>
		<div class="bottom1">底部元素</div>
		<div class="pos22">定位元素222</div>
		<div class="bottom">底部元素</div>
		<script src="../lib/jquery-1.8.2.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			$('.pos22').click(function() {
				window.scrollTo(0, 1500)
			})
		</script>
	</body>

</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值