| 4803 | } |
| 4804 | |
| 4805 | function scrollTo(elem) { |
| 4806 | if (elem) { |
| 4807 | elem.scrollIntoView(); |
| 4808 | |
| 4809 | var offset = getYOffset(); |
| 4810 | |
| 4811 | if (offset) { |
| 4812 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 4813 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 4814 | // top of the viewport. |
| 4815 | // |
| 4816 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 4817 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 4818 | // way down the page. |
| 4819 | // |
| 4820 | // This is often the case for elements near the bottom of the page. |
| 4821 | // |
| 4822 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 4823 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 4824 | // desired position. |
| 4825 | var elemTop = elem.getBoundingClientRect().top; |
| 4826 | $window.scrollBy(0, elemTop - offset); |
| 4827 | } |
| 4828 | } else { |
| 4829 | $window.scrollTo(0, 0); |
| 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | function scroll(hash) { |
| 4834 | hash = isString(hash) ? hash : $location.hash(); |