This project is archived and is in readonly mode.

#356 ✓invalid
Daniel Steigerwald

Mootools getPosition ported from JQuery

Reported by Daniel Steigerwald | September 3rd, 2008 @ 12:17 AM | in 2.0 (closed)



Element.implement({

    getPosition2: function(fn) {
        var left = 0,
            top = 0,
            safari = Browser.Engine.webkit,
            safari2 = Browser.Engine.webkit && Browser.Engine.version == 419,
            mozilla = Browser.Engine.gecko,
            parent = this.parentNode,
            offsetChild = this,
            offsetParent = this.offsetParent,
            doc = this.ownerDocument,
            css = Element.getComputedStyle,
            fixed = css(this, "position") == 'fixed';

        function border(el) {
            add(css(el, 'borderLeftWidth'), css(el, 'borderTopWidth'));
        }

        function add(l, t) {
            left += parseInt(l, 10) || 0;
            top += parseInt(t, 10) || 0;
        }

        if (!(mozilla && this == doc.body) && this.getBoundingClientRect) {
            var box = this.getBoundingClientRect();
            add(box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
				box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop));
            add(-doc.documentElement.clientLeft, -doc.documentElement.clientTop);
        } else {
            add(this.offsetLeft, this.offsetTop);
            while (offsetParent) {
                add(offsetParent.offsetLeft, offsetParent.offsetTop);
                if (mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2)
                    border(offsetParent);
                if (!fixed && css(offsetParent, 'position') == 'fixed')
                    fixed = true;
                offsetChild = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
                offsetParent = offsetParent.offsetParent;
            }
            while (parent && parent.tagName && !/^body|html$/i.test(parent.tagName)) {
                if (!/^inline|table.*$/i.test(css(parent, 'display'))) add(-parent.scrollLeft, -parent.scrollTop);
                if (mozilla && css(parent, 'overflow') != 'visible') border(parent);
                parent = parent.parentNode;
            }
            if ((safari2 && (fixed || css(offsetChild, 'position') == 'absolute')) ||
				(mozilla && css(offsetChild, 'position') != 'absolute'))
                add(-doc.body.offsetLeft, -doc.body.offsetTop);
            if (fixed)
                add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
					Math.max(doc.documentElement.scrollTop, doc.body.scrollTop));
        }
        return { x: left, y: top };
    }
});



Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Referenced by

Pages