| 5337 | } |
| 5338 | |
| 5339 | function scrollTo(elem) { |
| 5340 | if (elem) { |
| 5341 | elem.scrollIntoView(); |
| 5342 | |
| 5343 | var offset = getYOffset(); |
| 5344 | |
| 5345 | if (offset) { |
| 5346 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 5347 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 5348 | // top of the viewport. |
| 5349 | // |
| 5350 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 5351 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 5352 | // way down the page. |
| 5353 | // |
| 5354 | // This is often the case for elements near the bottom of the page. |
| 5355 | // |
| 5356 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 5357 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 5358 | // desired position. |
| 5359 | var elemTop = elem.getBoundingClientRect().top; |
| 5360 | $window.scrollBy(0, elemTop - offset); |
| 5361 | } |
| 5362 | } else { |
| 5363 | $window.scrollTo(0, 0); |
| 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | function scroll(hash) { |
| 5368 | // Allow numeric hashes |