获取页面元素的滚动位置,兼容各浏览器

   /**
        *获取页面元素的滚动位置
        @param {DOM} el Dom元素
        *return {Object} 元素滚动位置对象
        */
        function getScroll (el) {
            var d = el,
                doc = document,
                body = doc.body,
                docElement = doc.documentElement,
                l,
                t,
                ret,
                isStrict = document.compatMode == "CSS1Compat",
                isIE = /msie/.test(navigator.userAgent);

            if (d == doc || d == body) {
                if (isIE && isStrict) {
                    l = docElement.scrollLeft;
                    t = docElement.scrollTop;
                } else {
                    l = window.pageXOffset;
                    t = window.pageYOffset;
                }
                ret = {
                    left: l || (body ? body.scrollLeft : 0),
                    top: t || (body ? body.scrollTop : 0)
                };
            } else {
                ret = {
                    left: d.scrollLeft,
                    top: d.scrollTop
                };
            }

            return ret;
        }
posted @ 2012-10-13 10:59  静水渊  阅读(1109)  评论(1编辑  收藏  举报