页面上滑到顶部固定在顶部

window.onload = function () {
var pos = $('#div1').offset();// offset() 获得div1当前的位置,左上角坐标(x,y)
$(window).scroll(function () { //滚动条滚动事件
if ($(this).scrollTop() > pos.top) {
$('#div1').css('width', '100%').css('top', $(this).scrollTop() - pos.top);
}else if ($(this).scrollTop() <= pos.top) {
$('#div1').css('width', '100%').css('top', 0).css('position', 'relative');
}
})
}

原文地址:https://www.cnblogs.com/shark1100913/p/11392935.html