页面滚动到一定的距离时,导航条绝对定位

来源:互联网 发布:linux中ftp配置 编辑:程序博客网 时间:2024/06/07 11:27

window.onload=function(){
var node = document.getElementById(“node”)
H = 0
Y = node(node到body最上方的距离 求出H)
while (Y) {
H += Y.offsetTop; (子元素距离父元素的距离)
元素加的是这个元素到整个body的距离
Y = Y.offsetParent(不停的去寻找父元素)
}
window.onscroll = function(){
var s = document.body.scrollTop || document.documentElement.scrollTop//(网页被卷去的高 获取滚动条位置)
if(s>H) {
node.style = “position:fixed;top:0;”
}else{
node.style = ‘position:static;’
}
}
}

阅读全文
0 0