| 4843 | } |
| 4844 | |
| 4845 | function scrollTo(elem) { |
| 4846 | if (elem) { |
| 4847 | elem.scrollIntoView(); |
| 4848 | |
| 4849 | var offset = getYOffset(); |
| 4850 | |
| 4851 | if (offset) { |
| 4852 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 4853 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 4854 | // top of the viewport. |
| 4855 | // |
| 4856 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 4857 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 4858 | // way down the page. |
| 4859 | // |
| 4860 | // This is often the case for elements near the bottom of the page. |
| 4861 | // |
| 4862 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 4863 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 4864 | // desired position. |
| 4865 | var elemTop = elem.getBoundingClientRect().top; |
| 4866 | $window.scrollBy(0, elemTop - offset); |
| 4867 | } |
| 4868 | } else { |
| 4869 | $window.scrollTo(0, 0); |
| 4870 | } |
| 4871 | } |
| 4872 | |
| 4873 | function scroll(hash) { |
| 4874 | hash = isString(hash) ? hash : $location.hash(); |