滚动到特定位置菜单固定的效果

.chan-top{top: 416px;}
.abs-top{
position: absolute;
margin-left: -691px;
}
.fixed-top{
position: fixed;
margin-left: -600px;
top: 0;
}
<div class="ans-box chan-top abs-top" style="height: 312px; left: 50%;"></div>

$(window).scroll(function(){
var s=$(window).scrollTop();
console.log(s);
if(s >= 416){
$('.ans-box').removeClass('chan-top');
$('.ans-box').removeClass('abs-top');
$('.ans-box').addClass('fixed-top');

}else {
$('.ans-box').addClass('chan-top');
$('.ans-box').removeClass('fixed-top');
$('.ans-box').addClass('abs-top');
}
});
原文地址:https://www.cnblogs.com/opcec/p/8327876.html