js代码:
$(function () {
//获取.title的上偏移量
var startoffset = $(".title").offset().top;
//滚动时执行
window.onscroll = function () {
//获取当前滚动条的纵坐标位置||被卷走的高度
var stop = document.documentElement.scrollTop || document.body.scrollTop;
if (stop >= startoffset) {
$(".title").addClass("title_fixed");
}
else {
$(".title").removeClass("title_fixed");
}
}
})css代码:
.title_fixed {
position: fixed;
top: 0;
z-index: 100;
}
End!