| 5383 | } |
| 5384 | |
| 5385 | function scrollTo(elem) { |
| 5386 | if (elem) { |
| 5387 | elem.scrollIntoView(); |
| 5388 | |
| 5389 | var offset = getYOffset(); |
| 5390 | |
| 5391 | if (offset) { |
| 5392 | // `offset` is the number of pixels we should scroll UP in order to align `elem` properly. |
| 5393 | // This is true ONLY if the call to `elem.scrollIntoView()` initially aligns `elem` at the |
| 5394 | // top of the viewport. |
| 5395 | // |
| 5396 | // IF the number of pixels from the top of `elem` to the end of the page's content is less |
| 5397 | // than the height of the viewport, then `elem.scrollIntoView()` will align the `elem` some |
| 5398 | // way down the page. |
| 5399 | // |
| 5400 | // This is often the case for elements near the bottom of the page. |
| 5401 | // |
| 5402 | // In such cases we do not need to scroll the whole `offset` up, just the difference between |
| 5403 | // the top of the element and the offset, which is enough to align the top of `elem` at the |
| 5404 | // desired position. |
| 5405 | var elemTop = elem.getBoundingClientRect().top; |
| 5406 | $window.scrollBy(0, elemTop - offset); |
| 5407 | } |
| 5408 | } else { |
| 5409 | $window.scrollTo(0, 0); |
| 5410 | } |
| 5411 | } |
| 5412 | |
| 5413 | function scroll(hash) { |
| 5414 | // Allow numeric hashes |