| 4683 | } |
| 4684 | |
| 4685 | function scrollTo(elem) { |
| 4686 | if (elem) { |
| 4687 | elem.scrollIntoView(); |
| 4688 | |
| 4689 | var offset = getYOffset(); |
| 4690 | |
| 4691 | if (offset) { |
| 4692 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 4693 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 4694 | // top of the viewport. |
| 4695 | // |
| 4696 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 4697 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 4698 | // way down the page. |
| 4699 | // |
| 4700 | // This is often the case for elements near the bottom of the page. |
| 4701 | // |
| 4702 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 4703 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 4704 | // desired position. |
| 4705 | var elemTop = elem.getBoundingClientRect().top; |
| 4706 | $window.scrollBy(0, elemTop - offset); |
| 4707 | } |
| 4708 | } else { |
| 4709 | $window.scrollTo(0, 0); |
| 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | function scroll(hash) { |
| 4714 | hash = isString(hash) ? hash : $location.hash(); |