Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(jqLite): Add offset method to jqLite #3799

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the if is necessary anymore; we even dropped it in jQuery and Angular supports fewer browsers.

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);
Expand Down
14 changes: 13 additions & 1 deletion test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div style='position: absolute; top: 300px; left: 300px'></div>");
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() {
Expand Down