Browse Source

Optimize scrolling code during page transitions

Thibaut Courouble 8 years ago
parent
commit
f891e16f39
1 changed files with 8 additions and 3 deletions
  1. 8 3
      assets/javascripts/views/content/content.coffee

+ 8 - 3
assets/javascripts/views/content/content.coffee

@@ -99,14 +99,12 @@ class app.views.Content extends app.View
     return
 
   scrollToTarget: ->
-    return if @isLoading()
     if @routeCtx.hash and el = @findTargetByHash @routeCtx.hash
       $.scrollToWithImageLock el, @scrollEl, 'top',
         margin: if @scrollEl is @el then 0 else $.offset(@el).top
       $.highlight el, className: '_highlight'
     else
       @scrollTo @scrollMap[@routeCtx.state.id]
-    clearTimeout @scrollTimeout
     return
 
   onReady: =>
@@ -120,21 +118,28 @@ class app.views.Content extends app.View
 
   onEntryLoading: =>
     @showLoading()
+    if @scrollToTargetTimeout
+      clearTimeout @scrollToTargetTimeout
+      @scrollToTargetTimeout = null
     return
 
   onEntryLoaded: =>
     @hideLoading()
+    if @scrollToTargetTimeout
+      clearTimeout @scrollToTargetTimeout
+      @scrollToTargetTimeout = null
     @scrollToTarget()
     return
 
   beforeRoute: (context) =>
     @cacheScrollPosition()
     @routeCtx = context
-    @scrollTimeout = @delay @scrollToTarget
+    @scrollToTargetTimeout = @delay @scrollToTarget
     return
 
   cacheScrollPosition: ->
     return if not @routeCtx or @routeCtx.hash
+    return if @routeCtx.path is '/'
 
     unless @scrollMap[@routeCtx.state.id]?
       @scrollStack.push @routeCtx.state.id