zoukankan      html  css  js  c++  java
  • 原生js--兼容获取窗口滚动条位置和窗口大小的方法

    各个浏览器对获取获取窗口滚动条位置和窗口大小没有提供统一的API,以下是对其封装,解决兼容性问题

    /**
     * 获取浏览器视口的大小(显示文档的部分)
     *
     */
    function getViewPortSize(){
        // 除IE8及更早的版本以外的浏览器
        if( window.innerWidth != null ){
            return {
                w : window.innerWidth,
                h : window.innerHeight
            }
        }
        // 标准模式下的IE
        if( document.compatMode == "css1Compat" ){
            return {
                w : document.documentElement.clientWidth,
                h : document.documentElement.clientHeight
            }
        }
        // 怪异模式下的浏览器
        return {
            w : document.body.clientWidth,
            h : document.body.clientHeight
        }
    }

    /**
     *  获取窗口滚动条的位置
     */
    function getScrollOffset(){
        // 除IE8及更早版本
        if( window.pageXOffset != null ){
            return {
                x : window.pageXOffset,
                y : window.pageYOffset
            }
        }
        // 标准模式下的IE
        if( document.compatMode == "css1Compat" ){
            return {
                x : document.documentElement.scrollLeft,
                y : document.documentElement.scrollTop
            }
        }
        // 怪异模式下的浏览器
        return {
            x : document.body.scrollLeft,
            y : document.body.scrollTop
        }
    }

  • 相关阅读:
    jdk1.8 操作List<Map> 多个map 具有相同的key 进行分组合并重组数据
    js获取字符中连续的值
    Java线程ABA问题
    Oracle递归查询语句
    Oracle学习笔记表连接(十六)
    Docker For Mac没有docker0网桥
    awk 和 sed (Stream Editor)
    WARNING: firstResult/maxResults specified with collection fetch; applying in memory!
    iptables编写规则
    InnoDB Next-Key Lock
  • 原文地址:https://www.cnblogs.com/charling/p/3545342.html
Copyright © 2011-2022 走看看