获取当前scrollTop和scrollLeft,兼容XHTML

时间:2014-04-30 04:50:27   收藏:0   阅读:446

 

mamicode.com,码迷
 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};
mamicode.com,码迷

 

获取当前scrollTop和scrollLeft,兼容XHTML,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!