| 4711 | } |
| 4712 | |
| 4713 | function scrollTo(elem) { |
| 4714 | if (elem) { |
| 4715 | elem.scrollIntoView(); |
| 4716 | |
| 4717 | var offset = getYOffset(); |
| 4718 | |
| 4719 | if (offset) { |
| 4720 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 4721 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 4722 | // top of the viewport. |
| 4723 | // |
| 4724 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 4725 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 4726 | // way down the page. |
| 4727 | // |
| 4728 | // This is often the case for elements near the bottom of the page. |
| 4729 | // |
| 4730 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 4731 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 4732 | // desired position. |
| 4733 | var elemTop = elem.getBoundingClientRect().top; |
| 4734 | $window.scrollBy(0, elemTop - offset); |
| 4735 | } |
| 4736 | } else { |
| 4737 | $window.scrollTo(0, 0); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | function scroll(hash) { |
| 4742 | hash = isString(hash) ? hash : $location.hash(); |