简体   繁体   English

iPhone Mobile Safari 3上的getBoundingClientRect相当于什么?

[英]What is the equivalent of getBoundingClientRect on iPhone Mobile Safari 3?

iPhone Mobile Safari seems to be missing element.getBoundingClientRect. iPhone Mobile Safari似乎缺少element.getBoundingClientRect。 What is the equivalent method on iPhone Mobile Safari? iPhone Mobile Safari上的等效方法是什么? This method exists on the iPad. 这种方法存在于iPad上。

Edit 1: This code (webkitConvertPointFromNodeToPage) is only required for very old and out-of-date phones... see these comments . 编辑1:此代码(webkitConvertPointFromNodeToPage)仅适用于非常旧的和过时的手机...请参阅这些注释

EDIT 2: I wouldn't recommend you use this code... I recall changing it to deal with some problem with IE10 with touch zoom. 编辑2:我不建议您使用此代码...我记得更改它以处理IE10与触摸缩放的一些问题。 I will try to remember to update the code with the fix. 我将尝试记住使用修复程序更新代码。

Was: I think the following works on IE6+, FF3+, Safari 2+ (Desktop & Mobile), Chrome (Desktop & Android), Opera: 是:我认为以下适用于IE6 +,FF3 +,Safari 2+(桌面和移动),Chrome(桌面和Android),Opera:

function offset(el) {
    var convertPoint = window.webkitConvertPointFromNodeToPage;
    if ('getBoundingClientRect' in el) {
        var
            boundingRect = el.getBoundingClientRect(),
            body = document.body || document.getElementsByTagName("body")[0],
            clientTop = document.documentElement.clientTop || body.clientTop || 0,
            clientLeft = document.documentElement.clientLeft || body.clientLeft || 0,
            scrollTop = (window.pageYOffset || document.documentElement.scrollTop || body.scrollTop),
            scrollLeft = (window.pageXOffset || document.documentElement.scrollLeft || body.scrollLeft);
        return {
            top: boundingRect.top + scrollTop - clientTop,
            left: boundingRect.left + scrollLeft - clientLeft
        }
    } else if (convertPoint) {
        var
            zeroPoint = new WebKitPoint(0, 0),
            point = convertPoint(el, zeroPoint),
            scale = convertPoint(document.getElementById('scalingEl'), zeroPoint);
        return {
            top: Math.round(point.y * -200/scale.y),
            left: Math.round(point.x * -200/scale.x)
        }
    }
}

where the following is a child of the body: 以下是身体的孩子:

<div id="scalingEl" style="position:absolute;top:-200px;left:-200px;visibility:hidden;"></div>

Method does need some error checking (eg element must be in document). 方法确实需要一些错误检查(例如,元素必须在文档中)。 Scale makes it work when page zoomed, but may not be required (I did need it when testing webkitConvertPointFromNodeToPage in Windows Safari). 缩放使页面缩放时可以工作,但可能不需要(我在Windows Safari中测试webkitConvertPointFromNodeToPage时确实需要它)。

For anyone looking for the ele.getBoundingClientRect().width method on iOS 3, you can use ele.offsetWidth 对于在iOS 3上寻找ele.getBoundingClientRect()。width方法的任何人,您可以使用ele.offsetWidth

 if("getBoundingClientRect" in this.container) {
    this.width = this.container.getBoundingClientRect().width ;    
}else {
    this.width = this.container.offsetWidth;    //http://help.dottoro.com/ljvmcrrn.php
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM