获取滚动条顶部到网页顶部的距离
用JS获取滚动条顶部到网页顶部的距离,各浏览器下都可用:
1 var scrollPos;
2 if (typeof window.pageYOffset != 'undefined') //针对Netscape 浏览器
3 {
4 scrollPos = window.pageYOffset;
5 }
6 else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
7 {
8 scrollPos = document.documentElement.scrollTop;
9 }
10 else if (typeof document.body != 'undefined')
11 {
12 scrollPos = document.body.scrollTop;
13 }
2 if (typeof window.pageYOffset != 'undefined') //针对Netscape 浏览器
3 {
4 scrollPos = window.pageYOffset;
5 }
6 else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
7 {
8 scrollPos = document.documentElement.scrollTop;
9 }
10 else if (typeof document.body != 'undefined')
11 {
12 scrollPos = document.body.scrollTop;
13 }