这是一个结合了 position:relative 和 position:fixed 两种定位功能于一体的特殊定位,适用于一些特殊场景。( 即元素先按照普通文档流定位,之后为固定定位。)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>1111</title>
<style>
html {
box-sizing: border-box;
}
html,
body {
overflow: hidden;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
background-color: #196e76;
line-height: 1.2;
}
.cf:before,
.cf:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.cf:after {
clear: both;
}
/**
* For IE 6/7 only
* Include this rule to trigger hasLayout and contain floats.
*/
.cf {
*zoom: 1;
}
.scroll {
height: 500px;
overflow: scroll;
background-color: #e7ebef;
padding: 0 10px;
width: 100%;
max-width: 550px;
margin-right: auto;
margin-left: auto;
margin-top: 20px;
margin-bottom: 200px;
}
.wrapper {
margin-top: 310px;
}
.content {
padding: 0 15px;
width: 330px;
}
.content h1 {
margin-top: 0;
}
.content p,
.sidebar p{
font-family: "BLOKKNeue-Regular";
}
.sidebar {
padding: 20px;
width: 170px;
background-color: #434A54;
color: #fff;
}
.sidebar h3 {
margin: 0;
}
.content,
.sidebar {
float: left;
}
/* The sticky */
.sidebar {
position: -webkit-sticky;
position: sticky;
top: 15px;
}
</style>
</head>
<body>
<div class="scroll">
<div class="wrapper cf">
<div class="content">
<h1>Scroll Down!</h1>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
Mauris placerat eleifend leo.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque vero, maxime nulla dolore iure voluptatem itaque. Nisi commodi ut consectetur beatae mollitia. Ab, maxime quisquam ipsa, quibusdam itaque et eius.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita, molestiae.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus suscipit blanditiis delectus quos, soluta fuga voluptatem, facere inventore neque voluptate quaerat unde laboriosam molestiae repellat, sapiente accusamus cumque! Ipsam, illo!</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
Mauris placerat eleifend leo.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque vero, maxime nulla dolore iure voluptatem itaque. Nisi commodi ut consectetur beatae mollitia. Ab, maxime quisquam ipsa, quibusdam itaque et eius.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita, molestiae.</p>
</div>
</div>
</div>
<script>
</script>
</body>
</html>