获取窗口属性/脚本化CSS

获取窗口属性

查看滚动条的滚动距离

  1. window.pageXOffset window.pageYOffset (IE8及以下不兼容)
  2. document.body.scrollLeft/Top document.documentElenment.scrollLeft/Top

兼容性比较混乱,用时取两个值相加,因为不可能存在两个同时有值

  1. 封装兼容性方法,求滚动轮滚动距离 getScrollOffset()
    function getScrollOffset(){
        if(window.pageXOffset){
            return {
                x : window.pageXOffset,
                y : window.pageOYffset
            }
        }else{
            return {
                x : document.body.scrollLeft + document.documentElement.scrollLeft,
                y : ocument.body.scrollTop + document.documentElement.scrollTop
            }
        }
    }

查看视口的尺寸

  1. window.innerWidth/innerHeight (IE8及以下不兼容)
  2. document.documentElement.clientWidth/clientHeight (标准模式下。任意浏览器都兼容)
  3. document.body.clientWidth/clientHeight (怪异模式下的浏览器)
  4. 封装方法 返回浏览器视口尺寸 getViewportOffset()
       function getViewportOffset(){
            if(window.innerWidth){
                return {
                    w : window.innerWidth,
                    h : window.innerHeight
                }
            }else{
                if(document.compatMode === 'BackCompat'){
                    return {
                        w : document.body.clientWidth,
                        h : document.body.clientHeight
                    }
                }else{
                    return {
                        w : document.documentElement.cilentWidth,
                        h : document.documentElement.cilentHeight
                    }
                }
            }
        }

查看元素的尺寸

  1. dom.offsetWidth dom.offsetHeight

查看元素位置

  1. dom.offsetLeft dom.offsetTop
  2. 对于无定位父级的元素,返回相对文档的坐标,对于有定位父级的元素,返回相对于最近的有定位的父级的坐标
  3. dom.offsetParent 返回最近的有定位的父级,若无 返回body body.offsetParent 返回null

让滚动条滚动

  1. window 上有三个方法 scroll() scrollTo() 这连个方法一样 scrollBy()
  2. 三个方法功能类似,用法都是将X,Y坐标传入,即实现让滚动轮滚动到当前位置
  3. 区别scrollBy会在之前的数据基础上做累加

脚本化CSS

读写元素CSS属性

  1. dom.style.prop
  2. 可读写行间样式,没有兼容性问题,碰到float这样的保留字属性 前面应加css
  3. 复合属性必须拆解,组合单词变成小驼峰式写法
  4. 写入的值必须是字符串格式

查询计算样式

  1. window,getComputedStyle(ele,null); null 可以传入伪元素
  2. 计算样式只读
  3. 返回的计算样式的值都是绝对值,没有相对单位
  4. IE8 及以下不兼容
  5. IE独有属性 ele.currentStyle

计算样式只读 返回的计算样式的值不是经过转换的绝对值

  1. 封装函数
    function getStyle(ele,prop){
            if(window.getComputedStyle){
                return window.getComputedStyle(ele,null)[prop];
            }else{
                return ele.currentStyle[prop];
            }
        }

原文地址:https://www.qianqianhaiou.cn/index.php/archives/91/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值