zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    IIS7.5 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面
    MVC3在IIS7.5发布(部署)报403.14错误的解决办法
    Access to the path '' is denied 解决
    log4net日志系统使用详解
    vs2012调试IIs网站
    EF框架核心--EDM设计器
    EF框架概念及三种模式
    EF简单增删改查与分页
    ServiceStack.OrmLite 学习记录6:查
    ServiceStack.OrmLite 学习记录5:改
  • 原文地址:https://www.cnblogs.com/jasenkin/p/javascript_position.html
Copyright © 2011-2022 走看看