导航全局滑动JavaScript
/// <summary> /// 前往某个指定ID的锚点 /// </summary> /// <param name="id">锚点ID</param> /// <param name="cutHeight">稍上或稍下的高度值</param> function goTo(id, cutHeight) { //IE6下会读不到值 // alert("ID=" + id + "的标签与顶部的实际高度距离为:" + document.getElementById(id).offsetTop); var h = document.getElementById(id).offsetTop; if (h == 0) h = 20300; scroll(0, h - cutHeight); } //↓↓↓↓↓导航全局滑动↓↓↓↓↓/////start var oldCurrent = 0; var oldtop; var sindex = 0; function movebox(id) { sindex++; var daohang = document.getElementById(id); var objleft = getObUpperLeft(daohang, 1); var objtop = getObUpperLeft(daohang, 2); if (sindex == 1) oldtop = objtop; var cha = getPageScroll2() - oldtop; if (cha <= oldCurrent) { if (cha > 0) { daohang.style.display = "Block"; daohang.style.position = "fixed"; daohang.style.top = "0px"; try { ////判断IE6.0不同写法 var browser = navigator.appName; var b_version = navigator.appVersion; var version = b_version.split(";"); var trim_Version = version[1].replace(/[ ]/g, ""); if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE6.0") { daohang.style.position = "absolute"; daohang.style.left = objleft + "px"; daohang.style.top = getPageScroll2() + "px"; } } catch (e) { } } else { daohang.style.position = "static"; } } else { daohang.style.display = "none"; } oldCurrent = cha; } window.onscroll = function() { movebox("daohang"); } //获取对象坐标 function getObUpperLeft(obj, type) { var curLeft = curTop = 0; if (obj.offsetParent) { curLeft = obj.offsetLeft; curTop = obj.offsetTop; while (obj = obj.offsetParent) { curLeft += obj.offsetLeft; curTop += obj.offsetTop; } } if (type == 1) return curLeft; else if (type == 2) return curTop; } //获取浏览器滚动条高度 function getPageScroll2() { var yScroll; if (self.pageYOffset) { yScroll = self.pageYOffset; } else if (document.documentElement && document.documentElement.scrollTop) { yScroll = document.documentElement.scrollTop; } else if (document.body) { yScroll = document.body.scrollTop; } return yScroll; }