Browse Source

Improve $.scrollTo

- Fixes #596.
- Fixes incorrect sidebar scrolling when sidebar has padding-top.
Thibaut Courouble 8 years ago
parent
commit
188c9d0229

+ 4 - 3
assets/javascripts/lib/util.coffee

@@ -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)

+ 1 - 1
assets/javascripts/views/content/content.coffee

@@ -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]