2015-01-15 49 views
0

我們在我們的應用程序中轉換了一個頁面,該頁面先前將來自各個頁面的內容轉換爲使用Javascript平滑滾動到每個部分的單個頁面。平滑滾動不能在Rails中工作,除非頁面被刷新

除非刷新頁面,否則此功能不起作用。我相信它與turbolinks緩存頁面有關。

過渡cacheing在整個

Turbolinks.enableTransitionCache() 

平滑滾動功能

$(function() { 
    $('.scroll-button').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
     $('html,body').animate({ 
      scrollTop: target.offset().top - 160 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 

我試着選擇退出以各種方式turbolinks和過渡cacheing的啓用,最近標記每個環節,頁面容器和目標部分data-no-turbolink="true"

example_links.html.slim中的每個鏈接已被標記爲 李 =的link_to 'INFO',路徑,類: '標籤鏈路滾動按鈕', '數據沒有的TurboLink'=> '真'

show.html.slim具有頁容器標籤

.row data-no-transition-cache="true" data-no-turbolink="true" 

而且每個目標在該網頁部分進行標記

p.section-heading#someLabel data-no-turbolink="true" 

我如何能確保網頁時,通過單擊鏈接或使用返回按鈕,使平滑滾動功能是強制執行訪問?

回答

0

如果Turbolinks啓用(即您已經包括turbolinks.js),JavaScript的需要重寫,這樣像$('.scroll-button').click(...)事件:

$(document).on('click', '.scroll-button', function() { 
    ... 
}); 

Turbolinks改變你的應用程序的行爲來加載您的JavaScript只有一次,變化當加載新頁面時,<body>。因此,事件應附加到頁面加載之間不發生變化的$(document)

2

Turbolink發出的事件可讓您跟蹤導航生命週期並響應頁面加載。

更換

$(function() { 

$(document).on('turbolinks:load', function() { 

前者是當DOM完全加載時執行的事件偵聽器的簡寫。當turbolinks渲染一個頁面時(除了進行硬刷新時,即通過按F5鍵),該事件不會發出。

有關渦輪鏈接事件的更多信息,請參閱https://github.com/turbolinks/turbolinks#full-list-of-events

相關問題