| 4398 | } |
| 4399 | |
| 4400 | function scrollTo(elem) { |
| 4401 | if (elem) { |
| 4402 | elem.scrollIntoView(); |
| 4403 | |
| 4404 | var offset = getYOffset(); |
| 4405 | |
| 4406 | if (offset) { |
| 4407 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 4408 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 4409 | // top of the viewport. |
| 4410 | // |
| 4411 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 4412 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 4413 | // way down the page. |
| 4414 | // |
| 4415 | // This is often the case for elements near the bottom of the page. |
| 4416 | // |
| 4417 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 4418 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 4419 | // desired position. |
| 4420 | var elemTop = elem.getBoundingClientRect().top; |
| 4421 | $window.scrollBy(0, elemTop - offset); |
| 4422 | } |
| 4423 | } else { |
| 4424 | $window.scrollTo(0, 0); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | function scroll() { |
| 4429 | var hash = $location.hash(), elm; |