CSS中的sticky定位


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/03/28/CSS%E4%B8%AD%E7%9A%84sticky%E5%AE%9A%E4%BD%8D/

摘要

本文主要讲述了:

  1. 效果

正文

效果

首先想象元素是static定位:

  • 当元素距离 viewport 指定边界的距离大于等于指定的值时,表现得像是static定位
  • 当元素距离 viewport 指定边界的距离小于指定的值时,表现得像是特殊的relative定位。之所以说特殊,是因为sticky定位会使得元素黏在 viewport 上(就像fixed定位那样)。黏性定位也由此得名

示例:一进入页面表现得像是static定位,滚动后表现得像是特殊的relative定位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1 class="sticky">hello, jsweibo</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<style>
.sticky {
position: -webkit-sticky;
position: sticky;
top: 100px;
background-color: #639;
color: #fff;
}
</style>
<script>
window.addEventListener('load', function () {
console.log(document.querySelector('.sticky').getBoundingClientRect());
console.log(
Number.parseInt(
getComputedStyle(document.querySelector('.sticky')).top,
10
)
);
});
window.addEventListener('scroll', function () {
console.log(document.querySelector('.sticky').getBoundingClientRect());
console.log(
Number.parseInt(
getComputedStyle(document.querySelector('.sticky')).top,
10
)
);
});
</script>
</body>
</html>

示例:一进入页面表现得像是特殊的relative定位,滚动后仍表现得像是特殊的relative定位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1 class="sticky">hello, jsweibo</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<style>
.sticky {
position: -webkit-sticky;
position: sticky;
top: 400px;
background-color: #639;
color: #fff;
}
</style>
<script>
window.addEventListener('load', function () {
console.log(document.querySelector('.sticky').getBoundingClientRect());
console.log(
Number.parseInt(
getComputedStyle(document.querySelector('.sticky')).top,
10
)
);
});
window.addEventListener('scroll', function () {
console.log(document.querySelector('.sticky').getBoundingClientRect());
console.log(
Number.parseInt(
getComputedStyle(document.querySelector('.sticky')).top,
10
)
);
});
</script>
</body>
</html>

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/03/28/CSS%E4%B8%AD%E7%9A%84sticky%E5%AE%9A%E4%BD%8D/


本文对你有帮助?请支持我


支付宝
微信