居中css:
#timeWrap {
position: absolute;
width: 500px;
height: 250px;
left: 50%;
top: 300px;
margin-left: -250px;
margin-top: -125px;
border: 1px solid #2c72a8;
background-color: #fff;
}
js动态获取滚动的距离,然后改变css:
$(function () {
//滚动条滚动事件
$(window).scroll(function () {
let top = $(window).scrollTop();
//每次滚动重新设置div的位置
$("#timeWrap").css("top", 300 + top + "px");
});
})