R Shiny Dashboard Scroll to Top on Button Click

北战南征 提交于 2021-02-18 12:46:11

问题


Is there any way to scroll to the top of the page in ShinyDashboard on a button click?

I've added the following lines in ui.R under dashboardSidebar( :

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

and the following in server.R under observeEvent(input$button, {:

js$button()

This seems to work in the app preview in RStudio, but not in browser.


回答1:


Using the first Google hit for "javascript scroll to top", the most upvoted answer is window.scrollTo(x-coord, y-coord);

I tried replacing your document.body.scrollTop = 0; with window.scrollTo(0, 0) and it worked.

FYI, if you only have that one very simple function to define, you can also get away with shinyjs::runjs("window.scrollTo(0, 50)") in the server code



来源:https://stackoverflow.com/questions/49137032/r-shiny-dashboard-scroll-to-top-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!