| 4448 | } |
| 4449 | |
| 4450 | function scrollTo(elem) { |
| 4451 | if (elem) { |
| 4452 | elem.scrollIntoView(); |
| 4453 | |
| 4454 | var offset = getYOffset(); |
| 4455 | |
| 4456 | if (offset) { |
| 4457 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 4458 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 4459 | // top of the viewport. |
| 4460 | // |
| 4461 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 4462 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 4463 | // way down the page. |
| 4464 | // |
| 4465 | // This is often the case for elements near the bottom of the page. |
| 4466 | // |
| 4467 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 4468 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 4469 | // desired position. |
| 4470 | var elemTop = elem.getBoundingClientRect().top; |
| 4471 | $window.scrollBy(0, elemTop - offset); |
| 4472 | } |
| 4473 | } else { |
| 4474 | $window.scrollTo(0, 0); |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | function scroll() { |
| 4479 | var hash = $location.hash(), elm; |