| 5011 | } |
| 5012 | |
| 5013 | function scrollTo(elem) { |
| 5014 | if (elem) { |
| 5015 | elem.scrollIntoView(); |
| 5016 | |
| 5017 | var offset = getYOffset(); |
| 5018 | |
| 5019 | if (offset) { |
| 5020 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 5021 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 5022 | // top of the viewport. |
| 5023 | // |
| 5024 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 5025 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 5026 | // way down the page. |
| 5027 | // |
| 5028 | // This is often the case for elements near the bottom of the page. |
| 5029 | // |
| 5030 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 5031 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 5032 | // desired position. |
| 5033 | var elemTop = elem.getBoundingClientRect().top; |
| 5034 | $window.scrollBy(0, elemTop - offset); |
| 5035 | } |
| 5036 | } else { |
| 5037 | $window.scrollTo(0, 0); |
| 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | function scroll(hash) { |
| 5042 | // Allow numeric hashes |