@@ -57,8 +57,8 @@ function $AnchorScrollProvider() {
57
57
* - **number**: A fixed number of pixels to be used as offset.<br /><br />
58
58
* - **function**: A getter function called everytime `$anchorScroll()` is executed. Must return
59
59
* 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 />
62
62
* **Note**: The element will be taken into account only as long as its `position` is set to
63
63
* `fixed`. This option is useful, when dealing with responsive navbars/headers that adjust
64
64
* their height and/or positioning according to the viewport's size.
@@ -196,10 +196,7 @@ function $AnchorScrollProvider() {
196
196
if ( style . position !== 'fixed' ) {
197
197
offset = 0 ;
198
198
} 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 ;
203
200
}
204
201
} else if ( ! isNumber ( offset ) ) {
205
202
offset = 0 ;
@@ -223,10 +220,10 @@ function $AnchorScrollProvider() {
223
220
// often the case for elements near the bottom of the page.
224
221
// In such cases we do not need to scroll the whole `offset` up, just the fraction of the
225
222
// 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 ;
230
227
231
228
$window . scrollBy ( 0 , - 1 * necessaryOffset ) ;
232
229
}
0 commit comments