R/scrollama.R

Defines functions scrollama_setup scrollama_sidebar scrollama

Documented in scrollama scrollama_setup scrollama_sidebar

#' R Markdown output formats for using Scrollama
#'
#' Include the Scrollama JS library in the HTML output from R Markdown.
#' @param ...,template Arguments to be passed to [bookdown::html_document2()].
#' @references The Scrollama library:
#'   <https://github.com/russellsamora/scrollama>.
#' @return `scrollama()` and `scrollama_sidebar()` return R Markdown output
#'   formats; `scrollama_setup()` is a helper function to return the JavaScript
#'   code for initializing Scrollama.
#' @importFrom htmltools htmlDependency tags HTML
#' @export
scrollama <- function(...) {
  html_format(..., .dependencies = list(
    htmlDependency(
      'scrollama', '2.0.0', pkg_resource('scrollama'), script = 'scrollama.min.js')
  ))
}

#' @rdname scrollama
#' @export
scrollama_sidebar <- function(..., template = pkg_resource('html', 'scrollama-side.html')) {
  scrollama(..., template = template)
}

#' @param options A list of initialization options for Scrollama.
#' @rdname scrollama
#' @export
#' @examples rolldown::scrollama_setup(list(step = '.level1', offset = .2))
scrollama_setup <- function(
  options = list(step = '.section, img, video, embed', offset = .5)
) {
  init = sprintf('(function() {
  var scroller = scrollama();
  scroller.setup(%s)
    .onStepEnter(res => {
      res.element.classList.add("is-active");
    })
    .onStepExit(res => {
      res.element.classList.remove("is-active");
    });
  window.addEventListener("resize", scroller.resize);
})();', to_json(options))
  tags$script(HTML(init))
}

Try the rolldown package in your browser

Any scripts or data that you put into this service are public.

rolldown documentation built on April 4, 2025, 1:01 a.m.