粘性定位
bootstrap的官方解释: Position an element at the top of the viewport, from edge to edge, but only after you scroll past it. The .sticky-top
utility uses CSS’s position: sticky
, which isn’t fully supported in all browsers.
IE11 and IE10 will render position: sticky
as position: relative
. As such, we wrap the styles in a @supports
query, limiting the stickiness to only browsers that can render it properly.
sticky在mdn上的解释为
如果是固定在视口的导这也是为什吗在boostrap中需要把它放在body下的第一层级中
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
height: 5000px;
}
.container{
height: 300px;
border: 1px solid black;
margin-top: 100px;
overflow: scroll;
}
.sticky{
display: inline-block;
position: sticky;
left: 0;
top: 0;
width: 100px;
height: 100px;
background: red;
}
.sticky2{
left: 100px;
background: gray;
}
</style>
</head>
<body>
<div class="container">
<div class="sticky"></div>
<div style="height: 3000px;"></div>
</div>
<div class="sticky sticky2"></div>
</body>
</html>