Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

iPhone Mobile Safari seems to be missing element.getBoundingClientRect. What is the equivalent method on iPhone Mobile Safari? This method exists on the iPad.

like image 424
newtonapple Avatar asked May 07 '10 23:05

newtonapple


People also ask

What is getBoundingClientRect()?

The getBoundingClientRect() method returns the size of an element and its position relative to the viewport. The getBoundingClientRect() method returns a DOMRect object with eight properties: left, top, right, bottom, x, y, width, height.

Does getBoundingClientRect include padding?

Border, padding and margin are not included. This means when you set a width, That width is set to the content only then you add the padding and border.


2 Answers

For anyone looking for the ele.getBoundingClientRect().width method on iOS 3, you can use 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
}
like image 86
redochka Avatar answered Oct 18 '22 22:10

redochka


Edit 1: This code (webkitConvertPointFromNodeToPage) is only required for very old and out-of-date phones... see these comments.

EDIT 2: I wouldn't recommend you use this code... I recall changing it to deal with some problem with IE10 with touch zoom. 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:

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 (e.g. 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).

like image 29
robocat Avatar answered Oct 18 '22 21:10

robocat