jq实现吸附效果

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.top{
width: 100%;
height: 300px;
background: red;
}
.middle{
width: 100%;
height: 30px;
background: green;
}
.middle_active{
position: fixed;
width: 100%;
height: 30px;
top: 0;
z-index: 99;
}
.bottom{
width: 100%;
height: 1500px;
background: blue;
}
</style>
<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
</head>
<body>
<div class="top"></div>
<div class="middle"></div>
<div class="bottom"></div>
</body>
<script>
$(window).scroll(function(){
var juli=$(document).scrollTop();
if(juli>300){
$('.middle').addClass('middle_active');
}else{
$('.middle').removeClass('middle_active');
}
})
</script>
</html>

posted @ 2017-03-13 13:19  松前月下  阅读(470)  评论(1编辑  收藏  举报