From 8733352c201df6291b00f6b71a648550e6cbe5b6 Mon Sep 17 00:00:00 2001 From: steelsojka Date: Fri, 30 Aug 2013 14:15:47 -0500 Subject: [PATCH] feat(jqLite): add method offset as a getter to jqLite Add the method offset to jqlite to retrieve the elements top and left position. Using the method as a setter is not supported. --- src/jqLite.js | 22 ++++++++++++++++++++++ test/jqLiteSpec.js | 14 +++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/jqLite.js b/src/jqLite.js index 4a52cba018f8..6105d19eddf1 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -46,6 +46,7 @@ * - [next()](http://api.jquery.com/next/) - Does not support selectors * - [on()](http://api.jquery.com/on/) - Does not support namespaces, selectors or eventData * - [off()](http://api.jquery.com/off/) - Does not support namespaces or selectors + * - [offset()](http://api.jquery.com/offset/) - Does not support setter * - [parent()](http://api.jquery.com/parent/) - Does not support selectors * - [prepend()](http://api.jquery.com/prepend/) * - [prop()](http://api.jquery.com/prop/) @@ -439,6 +440,27 @@ forEach({ return val; } }, + + offset: function(element, name, value) { + var documentElem, + box = { top: 0, left: 0 }, + doc = element && element.ownerDocument; + + if (!doc || isDefined(name)) { + return; + } + + documentElem = doc.documentElement; + + if ( typeof element.getBoundingClientRect !== undefined ) { + box = element.getBoundingClientRect(); + } + + return { + top: box.top + (window.pageYOffset || documentElem.scrollTop) - (documentElem.clientTop || 0), + left: box.left + (window.pageXOffset || documentElem.scrollLeft) - (documentElem.clientLeft || 0) + }; + }, attr: function(element, name, value){ var lowercasedName = lowercase(name); diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 79c0d0c6a433..295a79593743 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -1288,7 +1288,19 @@ describe('jqLite', function() { expect(element.find('span').eq(20).length).toBe(0); }); }); - + + describe('offset', function() { + it("should retrieve the offset of the element", function() { + var element = jqLite("
"); + var doc = jqLite(document); + var body = doc.find("body"); + body.append(element); + + var offset = element.offset(); + expect(offset.top).toBe(300); + expect(offset.left).toBe(300); + }); + }); describe('triggerHandler', function() { it('should trigger all registered handlers for an event', function() {