简体   繁体   English

Rails:Turbolinks 返回页面顶部

[英]Rails: Turbolinks back to top of page

I am using turbolinks for rails;我正在为轨道使用 turbolinks; whenever I click on a link the page scrolls down to the previous page's scroll position. I want all my pages to load such that it doesn't scroll down (stays at the top of the page).每当我单击链接时,页面都会向下滚动到上一页的滚动条 position。我希望加载所有页面,这样它就不会向下滚动(停留在页面顶部)。 I don't need turbolinks to remember my previous scroll position. So how do I force the pages to load with scroll position at the top (0,0)?我不需要 turbolinks 来记住我以前的卷轴 position。那么如何强制页面加载顶部 (0,0) 的卷轴 position?

I've tried我试过了

$ ->
    window.scrollTo 0,0

But I'm probably not binding this right.但我可能没有约束这个权利。

You might be using an older version of turbolinks.您可能正在使用旧版本的 turbolinks。 This issue was fixed .此问题已修复

Try updating your Gemfile to use a newer version.尝试更新您的 Gemfile 以使用更新的版本。 I had the same issue using '1.1.1' but it was resolved after switching to '2.2.3'.我在使用“1.1.1”时遇到了同样的问题,但在切换到“2.2.3”后解决了。

The reason is Turbolink replaced the DOM element without refreshing page so your scrolling stays at the same position.原因是 Turbolink 在没有刷新页面的情况下替换了 DOM 元素,因此您的滚动保持在同一位置。

To change, you need to watch the event emitted by Turbolinks indicated the page loading is done, and then start your scrolling action.要进行更改,您需要观看 Turbolinks 发出的指示页面加载完成的事件,然后开始滚动操作。

Try this:试试这个:

$ ->
  $(document).on 'page:load', ->
    window.scrollTo 0, 0

I was facing the same problem in Rails 6 and solved it by adding我在 Rails 6 中遇到了同样的问题,并通过添加解决了它

document.addEventListener("turbolinks:load", function() {
    window.scrollTo(0,0)
});

under <script> tag of application.html.erb.在 application.html.erb 的<script>标签下。

This basically addEventListener to the DOM, whenever turbolinks load, it automatically scrolls to the top of the page.这基本上是将事件监听器添加到 DOM,每当 turbolinks 加载时,它会自动滚动到页面顶部。

am facing same issues with rails 6. to solve it, you have to tell the browser not to handle scrolling when restoring via the history or when reloading.我面临与 Rails 6 相同的问题。要解决它,您必须告诉浏览器在通过历史记录恢复或重新加载时不要处理滚动。 below is a comprehensive script to handle position下面是处理 position 的综合脚本

;(function () {
  // Tell the browser not to handle scrolling when restoring via the history or when reloading
  if ('scrollRestoration' in history) history.scrollRestoration = 'manual'

  var SCROLL_POSITION = 'scroll-position'
  var PAGE_INVALIDATED = 'page-invalidated'

  // Patch the reload method to flag that the following page load originated from the page being invalidated
  Turbolinks.BrowserAdapter.prototype.reload = function () {
    sessionStorage.setItem(PAGE_INVALIDATED, 'true')
    location.reload()
  }

  // Persist the scroll position when leaving a page
  addEventListener('beforeunload', function () {
    sessionStorage.setItem(
      SCROLL_POSITION,
      JSON.stringify({
        scrollX: scrollX,
        scrollY: scrollY,
        location: location.href
      })
    )
  })

  // When a page is fully loaded:
  // 1. Get the persisted scroll position
  // 2. If the locations match and the load did not originate from a page invalidation, scroll to the persisted position
  // 3. Remove the persisted information
  addEventListener('DOMContentLoaded', function (event) {
    var scrollPosition = JSON.parse(sessionStorage.getItem(SCROLL_POSITION))

    if (shouldScroll(scrollPosition)) {
      scrollTo(scrollPosition.scrollX, scrollPosition.scrollY)
    }

    sessionStorage.removeItem(SCROLL_POSITION)
    sessionStorage.removeItem(PAGE_INVALIDATED)
  })

  function shouldScroll (scrollPosition) {
    return (
      scrollPosition &&
      scrollPosition.location === location.href &&
      !JSON.parse(sessionStorage.getItem(PAGE_INVALIDATED))
    )
  }
})()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM