您的位置:首页 > 教程笔记 > 建站教程

网站侧边跟随滚动特效

2020-07-21 12:49:28 建站教程 58
网站侧边跟随滚动特效

代码
var sidebar = document.getElementById("guding");
var sidebarPos = sidebar.offsetTop;

window.onscroll = function() {
if (window.pageYOffset >= sidebarPos) {
sidebar.style.position = "fixed";
sidebar.style.top = "0";
} else {
sidebar.style.position = "relative";
sidebar.style.top = "auto";
}

// 判断侧边栏是否已经贴合顶部
if (window.pageYOffset <= sidebarPos) {
sidebar.style.position = "relative";
sidebar.style.top = "0";
}
};


相关推荐