• 获取当前scrollTop和scrollLeft,兼容XHTML


     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};
  • 相关阅读:
    js中级-函数封装
    js中级-11.7
    js中级-11.5
    js中级-11.2
    js中级-this
    js中级-作用域链
    10.23
    10.22
    10.19js
    10.18
  • 原文地址:https://www.cnblogs.com/fyima/p/3695714.html
Copyright © 2020-2023  润新知