History

Improve $.scrollTo

- Fixes #596. - Fixes incorrect sidebar scrolling when sidebar has padding-top.

Thibaut Courouble committed 9 years ago

Commit 188c9d01 parent 63bb8cd2 files · +5 −4Browse files

Unified · Split

2 files
  1. assets/javascripts/lib/util.coffee +4 −3
  2. assets/javascripts/views/content/content.coffee +1 −1
assets/javascripts/lib/util.coffee+4−3
@@ -170,10 +170,11 @@ $.scrollTo = (el, parent, position = 'center', options = {}) -> · 169 unmodified lines
170170 return unless parent.scrollHeight > parentHeight
171171
172172 top = $.offset(el, parent).top
173+ offsetTop = parent.firstElementChild.offsetTop
173174
174175 switch position
175176 when 'top'
176− parent.scrollTop = top - (if options.margin? then options.margin else 20)
177+ parent.scrollTop = top - offsetTop - (if options.margin? then options.margin else 0)
177178 when 'center'
178179 parent.scrollTop = top - Math.round(parentHeight / 2 - el.offsetHeight / 2)
179180 when 'continuous'
@@ -182,8 +183,8 @@ $.scrollTo = (el, parent, position = 'center', options = {}) -> · 2 unmodified lines
182183
183184 # If the target element is above the visible portion of its scrollable
184185 # ancestor, move it near the top with a gap = options.topGap * target's height.
185− if top <= scrollTop + height * (options.topGap or 1)
186− parent.scrollTop = top - height * (options.topGap or 1)
186+ if top - offsetTop <= scrollTop + height * (options.topGap or 1)
187+ parent.scrollTop = top - offsetTop - height * (options.topGap or 1)
187188 # If the target element is below the visible portion of its scrollable
188189 # ancestor, move it near the bottom with a gap = options.bottomGap * target's height.
189190 else if top >= scrollTop + parentHeight - height * ((options.bottomGap or 1) + 1)
assets/javascripts/views/content/content.coffee+1−1
@@ -102,7 +102,7 @@ class app.views.Content extends app.View · 101 unmodified lines
102102 return if @isLoading()
103103 if @routeCtx.hash and el = @findTargetByHash @routeCtx.hash
104104 $.scrollToWithImageLock el, @scrollEl, 'top',
105− margin: 20 + if @scrollEl is @el then 0 else $.offset(@el).top
105+ margin: if @scrollEl is @el then 0 else $.offset(@el).top
106106 $.highlight el, className: '_highlight'
107107 else
108108 @scrollTo @scrollMap[@routeCtx.state.id]