| 5448 | } |
| 5449 | |
| 5450 | function scrollTo(elem) { |
| 5451 | if (elem) { |
| 5452 | elem.scrollIntoView(); |
| 5453 | |
| 5454 | var offset = getYOffset(); |
| 5455 | |
| 5456 | if (offset) { |
| 5457 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 5458 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 5459 | // top of the viewport. |
| 5460 | // |
| 5461 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 5462 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 5463 | // way down the page. |
| 5464 | // |
| 5465 | // This is often the case for elements near the bottom of the page. |
| 5466 | // |
| 5467 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 5468 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 5469 | // desired position. |
| 5470 | var elemTop = elem.getBoundingClientRect().top; |
| 5471 | $window.scrollBy(0, elemTop - offset); |
| 5472 | } |
| 5473 | } else { |
| 5474 | $window.scrollTo(0, 0); |
| 5475 | } |
| 5476 | } |
| 5477 | |
| 5478 | function scroll(hash) { |
| 5479 | // Allow numeric hashes |