mirror of
https://github.com/freeCodeCamp/devdocs
synced 2024-11-16 19:48:10 +01:00
Improve $.scrollTo
- Fixes #596. - Fixes incorrect sidebar scrolling when sidebar has padding-top.
This commit is contained in:
parent
63bb8cd1e1
commit
188c9d0229
2 changed files with 5 additions and 4 deletions
|
@ -170,10 +170,11 @@ $.scrollTo = (el, parent, position = 'center', options = {}) ->
|
|||
return unless parent.scrollHeight > parentHeight
|
||||
|
||||
top = $.offset(el, parent).top
|
||||
offsetTop = parent.firstElementChild.offsetTop
|
||||
|
||||
switch position
|
||||
when 'top'
|
||||
parent.scrollTop = top - (if options.margin? then options.margin else 20)
|
||||
parent.scrollTop = top - offsetTop - (if options.margin? then options.margin else 0)
|
||||
when 'center'
|
||||
parent.scrollTop = top - Math.round(parentHeight / 2 - el.offsetHeight / 2)
|
||||
when 'continuous'
|
||||
|
@ -182,8 +183,8 @@ $.scrollTo = (el, parent, position = 'center', options = {}) ->
|
|||
|
||||
# If the target element is above the visible portion of its scrollable
|
||||
# ancestor, move it near the top with a gap = options.topGap * target's height.
|
||||
if top <= scrollTop + height * (options.topGap or 1)
|
||||
parent.scrollTop = top - height * (options.topGap or 1)
|
||||
if top - offsetTop <= scrollTop + height * (options.topGap or 1)
|
||||
parent.scrollTop = top - offsetTop - height * (options.topGap or 1)
|
||||
# If the target element is below the visible portion of its scrollable
|
||||
# ancestor, move it near the bottom with a gap = options.bottomGap * target's height.
|
||||
else if top >= scrollTop + parentHeight - height * ((options.bottomGap or 1) + 1)
|
||||
|
|
|
@ -102,7 +102,7 @@ class app.views.Content extends app.View
|
|||
return if @isLoading()
|
||||
if @routeCtx.hash and el = @findTargetByHash @routeCtx.hash
|
||||
$.scrollToWithImageLock el, @scrollEl, 'top',
|
||||
margin: 20 + if @scrollEl is @el then 0 else $.offset(@el).top
|
||||
margin: if @scrollEl is @el then 0 else $.offset(@el).top
|
||||
$.highlight el, className: '_highlight'
|
||||
else
|
||||
@scrollTo @scrollMap[@routeCtx.state.id]
|
||||
|
|
Loading…
Reference in a new issue