<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style media="screen">
* {
padding: 0;
margin: 0;
}
header {
width: 100%;
height: 220px;
background-color: #454545;
}
footer {
width: 100%;
height: 400px;
background-color: #454545;
}
#content {
margin-top: 50px;
height: 1000px;
position: relative;
}
#content .side-bar {
float: left;
width: 80px;
height: 400px;
background-color: red;
}
#content .cont-right {
float: right;
height: 1000px;
width: 800px;
background-color: #999;
}
</style>
</head>
<body>
<header></header>
<div id="content">
<div class="side-bar"></div>
<div class="cont-right"></div>
</div>
<footer></footer>
</body>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<script type="text/javascript">
var $content = $('#content');
var $sideBar = $('#content .side-bar');
var $contRight = $('#content .cont-right');
var $header = $('header');
$(document).on('scroll load resize', function() {
// 貌似要按照从大到小来写if里面的判断
if ($(document).scrollTop() >= $header.height() + $contRight.height() - $sideBar.height()) { // 820
$sideBar.css({
'position': 'absolute',
'top': $contRight.height() - $sideBar.height() // 600
});
} else if ($(document).scrollTop() >= 220) {
$sideBar.css({
'position': 'fixed',
'top': 50
});
} else {
$sideBar.css({
'position': 'static'
})
}
})
</script>
</html>
$(window)更好。查一下