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
170 return unless parent.scrollHeight > parentHeight170 return unless parent.scrollHeight > parentHeight
171 171
172 top = $.offset(el, parent).top172 top = $.offset(el, parent).top
173 offsetTop = parent.firstElementChild.offsetTop
173 174
174 switch position175 switch position
175 when 'top'176 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)
177 when 'center'178 when 'center'
178 parent.scrollTop = top - Math.round(parentHeight / 2 - el.offsetHeight / 2)179 parent.scrollTop = top - Math.round(parentHeight / 2 - el.offsetHeight / 2)
179 when 'continuous'180 when 'continuous'
@@ -182,8 +183,8 @@ $.scrollTo = (el, parent, position = 'center', options = {}) -> · 2 unmodified lines
182 183
183 # If the target element is above the visible portion of its scrollable184 # If the target element is above the visible portion of its scrollable
184 # ancestor, move it near the top with a gap = options.topGap * target's height.185 # ancestor, move it near the top with a gap = options.topGap * target's height.
185 if top <= scrollTop + height * (options.topGap or 1)186 if top - offsetTop <= scrollTop + height * (options.topGap or 1)
186 parent.scrollTop = top - height * (options.topGap or 1)187 parent.scrollTop = top - offsetTop - height * (options.topGap or 1)
187 # If the target element is below the visible portion of its scrollable188 # If the target element is below the visible portion of its scrollable
188 # ancestor, move it near the bottom with a gap = options.bottomGap * target's height.189 # ancestor, move it near the bottom with a gap = options.bottomGap * target's height.
189 else if top >= scrollTop + parentHeight - height * ((options.bottomGap or 1) + 1)190 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
102 return if @isLoading()102 return if @isLoading()
103 if @routeCtx.hash and el = @findTargetByHash @routeCtx.hash103 if @routeCtx.hash and el = @findTargetByHash @routeCtx.hash
104 $.scrollToWithImageLock el, @scrollEl, 'top',104 $.scrollToWithImageLock el, @scrollEl, 'top',
105 margin: 20 + if @scrollEl is @el then 0 else $.offset(@el).top105 margin: if @scrollEl is @el then 0 else $.offset(@el).top
106 $.highlight el, className: '_highlight'106 $.highlight el, className: '_highlight'
107 else107 else
108 @scrollTo @scrollMap[@routeCtx.state.id]108 @scrollTo @scrollMap[@routeCtx.state.id]