R Shiny Dashboard 在按钮单击时滚动到顶部

R Shiny Dashboard Scroll to Top on Button Click

在 ShinyDashboard 中有没有什么方法可以在单击按钮时滚动到页面顶部?

我在 dashboardSidebar( 下的 ui.R 中添加了以下行:

useShinyjs(), 
extendShinyjs(text = "shinyjs.button = function() {document.body.scrollTop = 0;}"),

以及 observeEvent(input$button, {server.R 中的以下内容:

js$button()

这似乎在 RStudio 的应用程序预览中有效,但在浏览器中无效。

first Google hit 用于 "javascript scroll to top",投票最多的答案是 window.scrollTo(x-coord, y-coord);

我尝试用 window.scrollTo(0, 0) 替换你的 document.body.scrollTop = 0; 并且成功了。

仅供参考,如果您只有一个非常简单的函数要定义,您也可以在服务器代码中使用 shinyjs::runjs("window.scrollTo(0, 50)")