| 5357 | } |
| 5358 | |
| 5359 | function scrollTo(elem) { |
| 5360 | if (elem) { |
| 5361 | elem.scrollIntoView(); |
| 5362 | |
| 5363 | var offset = getYOffset(); |
| 5364 | |
| 5365 | if (offset) { |
| 5366 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 5367 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 5368 | // top of the viewport. |
| 5369 | // |
| 5370 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 5371 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 5372 | // way down the page. |
| 5373 | // |
| 5374 | // This is often the case for elements near the bottom of the page. |
| 5375 | // |
| 5376 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 5377 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 5378 | // desired position. |
| 5379 | var elemTop = elem.getBoundingClientRect().top; |
| 5380 | $window.scrollBy(0, elemTop - offset); |
| 5381 | } |
| 5382 | } else { |
| 5383 | $window.scrollTo(0, 0); |
| 5384 | } |
| 5385 | } |
| 5386 | |
| 5387 | function scroll(hash) { |
| 5388 | // Allow numeric hashes |