what is
Viscous positioning position sticky
element using normal document flow layout ( static
), when its border ( border rectangle phase) to latest rolling ancestor the borders (i.e. Content rectangular is less than the specified threshold), the position sticky
The element is fixed nearest scroll ancestor element
how to use
To achieve tack positioned addition to specifying position: sticky;
needed outside the specified threshold, i.e., the horizontal scroll must specify left/right
, vertical scrolling must be specified top/bottom
.
like:
.some-component{
position: sticky;
top: 0px;
}
Sticky positioning position sticky
can be used to achieve some "ceiling" effects
Advanced
How is the threshold calculated?
for example:
.sticky-btn{
position: sticky;
top: 10px;
}
It means that .sticky-btn
border of the 061eaa04edc907 element and the upper inner border of the scrolling container (that is, the content rectangle ) edge is less than 10px
, the element is in a fixed position.
Multiple sticky
elements
- There can be multiple valid
sticky
elementssticky
elements are independent of each other; sticky
element is a positioning element. When multiplesticky
elements overlap, the overlapping principle of positioning elements is followed.
other
- The invalid
sticky
element behaves the same as therelative
location.
Invalid sticky
element
When I use position: sticky;
first time, I always find that the effect is not achieved. The common reasons are:
left/right
is specified when scrolling horizontally;- Vertical scrolling is checking if
top/bottom
is specified; sticky
an element's parent (or ancestor) specifiesoverflow/overflow-x/overflow-y
property, and the value ishidden/scroll/auto
;sticky
The parent element (not the ancestor element) of the element is not yet visible in the scroll element viewport.
You can see this demo
compatibility
Can I Use CSS position: sticky
As of 2020, 95% of browsers have some level of support forposition: sticky
Older versions of Safari will require the -webkit vendor-prefix
Usually written like this:
position: -webkit-sticky;
position: sticky;
Downgrade scheme (polyfill)
If you have to be compatible with various browsers, you have to use JS to achieve it. Fortunately, there are already many excellent libraries:
refer to
Organized from GitHub notes: parsing position: sticky;
.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。