position属性中最有意思的就是sticky了,设置了sticky的元素,在屏幕范围(viewport)时该元素的位置并不受到定位影响(设置是top、left等属性无效),当该元素的位置将要移出偏移范围时,定位又会变成fixed,根据设置的left、top等属性成固定位置的效果。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body{
margin:0;
padding:0;
width:100%;
height:2000px;
}
#box{
width:50px;
height:50px;
position: sticky;
top:10px;
margin:20px 10px;
background: red;
}
</style>
</head>
<body>
<div id="box">
</div>
</body>
</html>