Skip to content

Commit 73c44c8

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix($anchorScroll): add missing tests and do overall clenup
Add tests for <body> with border/margin/padding and boxSizing. Simplify and fix a couple of bugs.
1 parent 709bdbe commit 73c44c8

File tree

2 files changed

+221
-128
lines changed

2 files changed

+221
-128
lines changed

src/ng/anchorScroll.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ function $AnchorScrollProvider() {
5757
* - **number**: A fixed number of pixels to be used as offset.<br /><br />
5858
* - **function**: A getter function called everytime `$anchorScroll()` is executed. Must return
5959
* a number representing the offset (in pixels).<br /><br />
60-
* - **jqLite**: A jqLite/jQuery element to be used for specifying the offset. The sum of the
61-
* element's height and its distance from the top of the page will be used as offset.<br />
60+
* - **jqLite**: A jqLite/jQuery element to be used for specifying the offset. The distance from
61+
* the top of the page to the element's bottom will be used as offset.<br />
6262
* **Note**: The element will be taken into account only as long as its `position` is set to
6363
* `fixed`. This option is useful, when dealing with responsive navbars/headers that adjust
6464
* their height and/or positioning according to the viewport's size.
@@ -196,10 +196,7 @@ function $AnchorScrollProvider() {
196196
if (style.position !== 'fixed') {
197197
offset = 0;
198198
} else {
199-
var rect = elem.getBoundingClientRect();
200-
var top = rect.top;
201-
var height = rect.height;
202-
offset = top + height;
199+
offset = elem.getBoundingClientRect().bottom;
203200
}
204201
} else if (!isNumber(offset)) {
205202
offset = 0;
@@ -223,10 +220,10 @@ function $AnchorScrollProvider() {
223220
// often the case for elements near the bottom of the page.
224221
// In such cases we do not need to scroll the whole `offset` up, just the fraction of the
225222
// offset that is necessary to align the top of `elem` at the desired position.
226-
var elemTop = elem.getBoundingClientRect().top;
227-
var bodyTop = document.body.getBoundingClientRect().top;
228-
var scrollTop = $window.pageYOffset;
229-
var necessaryOffset = offset - (elemTop - (bodyTop + scrollTop));
223+
// ---
224+
// Note: getBoundingClientRect()'s top is relative to the top of the viewport
225+
// (not the page), which is exactly what interests us.
226+
var necessaryOffset = offset - elem.getBoundingClientRect().top;
230227

231228
$window.scrollBy(0, -1 * necessaryOffset);
232229
}

0 commit comments

Comments
 (0)