javascript position兼容性随笔
一、Javascript源码
if (!window.jasen.core.Position) { window.jasen.core.Position = {}; } function Size(width, height) { this.width = parseFloat(width); this.height = parseFloat(height); } Size.prototype.toString = function () { return "width=" + this.width + ";height=" + this.height; } function Point(x, y) { this.x = parseFloat(x); this.y = parseFloat(y); } Point.prototype.toString = function () { return "x=" + this.x + ";y=" + this.y; } // 获取窗口的左上角的偏移量(相对屏幕) function getScreenOffset() { if (window.screenLeft) { return new Point(window.screenLeft, window.screenTop); } else if (window.screenX) { // Firefox return new Point(window.screenX, window.screenY); } return new Point(0, 0); } // 获取屏幕的大小 function getScreenSize() { return new Size(window.screen.width, window.screen.height); } // 获取窗口中的文档显示区的大小 function getVisibleSize() { if (window.innerWidth) { return new Size(window.innerWidth, window.innerHeight); } else if (document.documentElement && document.documentElement.clientWidth) { return new Size(document.documentElement.clientWidth, document.documentElement.clientHeight); } else if (document.body && document.body.clientWidth) { return new Size(document.body.clientWidth, document.body.clientHeight); } return new Size(0, 0); } // 获取窗口中的文档的偏移量(滚动条的值) function getPageOffset() { if (window.pageXOffset) { return new Point(window.pageXOffset, window.pageYOffset); } else if (document.documentElement && document.documentElement.scrollLeft) { return new Point(document.documentElement.scrollLeft, document.documentElement.scrollTop); } else if (document.body && document.body.scrollLeft) { return new Point(document.body.scrollLeft, document.body.scrollTop); } return new Point(0, 0); } var position = window.Position = window.jasen.core.Position; position.getScreenOffset = getScreenOffset; position.getScreenOffsetX = function () { return getScreenOffset().x; }; position.getScreenOffsetY = function () { return getScreenOffset().y; }; position.getScreenSize = getScreenSize; position.getScreenWidth = function () { return getScreenSize().width; }; position.getScreenHeight = function () { return getScreenSize().height; }; position.getVisibleSize = getVisibleSize; position.getVisibleWidth = function () { return getVisibleSize().width; }; position.getVisibleHeight = function () { return getVisibleSize().height; }; position.getPageOffset = getPageOffset; position.getPageOffsetX = function () { return getPageOffset().x; }; position.getPageOffsetY = function () { return getPageOffset().y; };
二、范例
<script type="text/javascript"> document.body.onclick = function () { alert(window.Position.getVisibleSize()); alert(window.Position.getPageOffset()); } </script>