javascript position兼容性随笔
2013-11-09 01:29
471 查看
一、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>
相关文章推荐
- javascript Xml兼容性随笔
- javascript event兼容性随笔
- javascript opacity兼容性随笔
- 随笔-Javascript实现import动态导入文件方法及其切换
- Javascript的IE和Firefox兼容性汇编
- javascript不定参数&&兼容性样式设置与获取
- Javascript的IE和Firefox兼容性汇编
- IE和Firefox在JavaScript方面的兼容性(汇编)
- IE和Firefox在css,JavaScript方面的兼容性
- 【总结】IE和Firefox的Javascript兼容性总结
- Javascript的IE和Firefox兼容性汇编
- firefox与ie的javascript兼容性编程汇编
- javaScript学习随笔
- Javascript的IE和Firefox兼容性汇编
- javascript的history.back()兼容性问题
- 【总结】IE和Firefox的Javascript兼容性总结
- 一些Javascript的IE和Firefox(火狐)兼容性的问题总结及常用例子
- 复习HTML JavaScript 随笔
- Javascript的IE和Firefox兼容性汇编
- 前端工程师IE6兼容性问题随笔(未完待续)