getBoundingClientRect

本文介绍2011年W3C发布的CSSOM视图模块,该模块提供了一种方法来获取元素的边界矩形,有助于开发者通过编程方式调整页面布局。文章还分享了一个实际应用案例,展示如何利用此API实现响应式网页设计。

在2011年W3C出炉了CSSOM视图模块(CSS Object Model View)。CSSOM视图模块(CSSOM View Module)定义了一些 API,Web 开发人员使用这些 API 可以进行检查,也可以以编程方式更改文档及其内容的视觉属性。

后来此模块丰富了更多方法, 其中之一 https://drafts.csswg.org/cssom-view/#dom-element-getboundingclientrect

利用此方法在调整响应式页面的viewport很实用:

! function(a) {
    function d() {
        var e = g.getBoundingClientRect().width;
        750 < e / b && (e = 750 * b);
        a.rem = e / 7.5;
        g.style.fontSize = a.rem + "px";
        window.htmlFontSize = a.rem + "px"; }
    var b, c, f = a.document,
        g = f.documentElement;
    f.querySelector('meta[name="viewport"]');
    f.querySelector('meta[name="flexible"]');
    b || c || (c = (a.navigator.appVersion.match(/android/gi), a.navigator.appVersion.match(/iphone/gi)), b = a.devicePixelRatio, b = c ? 3 <= b ? 3 : 2 <= b ? 2 : 1 : 1, c = 1 / b);
    a.dpr = b;
    a.addEventListener("resize", function() { d() }, !1);
    a.addEventListener("pageshow", function(a) {
        a.persisted &&
            d()
    }, !1);
    d()
}(window);

相关延伸阅读:

阮一峰老师的文章http://www.ruanyifeng.com/blog/2015/09/web-page-performance-in-depth.html

移动web开发之像素和DPRhttp://www.cnblogs.com/xiaohuochai/p/5494624.html

转载于:https://www.cnblogs.com/haimingpro/p/6707736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值