1 // JavaScript Document
2 /* 用法与测试:
3 var myScroll = getScroll();
4 alert("myScroll.scrollTop:" + myScroll.scrollTop);
5 alert("myScroll.scrollLeft:" + myScroll.scrollLeft);
6 */
7 function getScroll(){
8 var scrollTop,scrollLeft;
9 scrollTop = (document.body.scrollTop > document.documentElement.scrollTop) ? document.body.scrollTop : document.documentElement.scrollTop;//兼容浏览器
10
11 if(isNaN(scrollTop) || scrollTop < 0){ scrollTop = 0;};//如果获取到非数字或者是小于0,那么等于0
12
13 scrollLeft = (document.body.scrollLeft > document.documentElement.scrollLeft) ? document.body.scrollTop : document.documentElement.scrollLeft;
14
15 if(isNaN(scrollLeft) || scrollLeft < 0){ scrollLeft = 0;};
16
17 return{scrollTop:scrollTop,scrollLeft:scrollLeft};
获取当前scrollTop和scrollLeft,兼容XHTML,码迷,mamicode.com
时间: 2024-10-24 02:11:09