From 188c9d0229f0a18cf7dcb28a38fbd6ba643d2b13 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 26 Mar 2017 12:07:29 -0400 Subject: [PATCH] Improve $.scrollTo - Fixes #596. - Fixes incorrect sidebar scrolling when sidebar has padding-top. --- assets/javascripts/lib/util.coffee | 7 ++++--- assets/javascripts/views/content/content.coffee | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/assets/javascripts/lib/util.coffee b/assets/javascripts/lib/util.coffee index ec834abb..12ba49f7 100644 --- a/assets/javascripts/lib/util.coffee +++ b/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) diff --git a/assets/javascripts/views/content/content.coffee b/assets/javascripts/views/content/content.coffee index ecbd8f15..a4187b66 100644 --- a/assets/javascripts/views/content/content.coffee +++ b/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]