[英]How to detect a slide change in Slidy?
I use rmarkdown
to produce a Slidy slide show using slidy_presentation
. 我用rmarkdown
生产使用Slidy幻灯片slidy_presentation
。 I would like to execute some Javascript when a slide is shown. 显示幻灯片时,我想执行一些Javascript。 In ioslides_presentation
there's a slideenter
event triggered when a page is shown (see Event when slide is displayed in ioslides? ), but I don't see anything like that in Slidy. 在ioslides_presentation
,显示页面时会触发一个slideenter
事件(请参阅在ioslides中显示幻灯片时发生的事件? ),但是在Slidy中我看不到任何类似的事件。
I can detect whether a particular slide is being shown (by looking at the class of its div
), but I don't know how to trigger the code to do that. 我可以检测是否正在显示特定幻灯片(通过查看其div
的类),但是我不知道如何触发代码来执行此操作。
Is there some way to detect that a slide change is happening? 是否有某种方法可以检测到正在发生幻灯片更改?
I researched a little and found an easy hack on how to detect which slide is currently shown: 我进行了一些研究,发现了一个有关如何检测当前显示的幻灯片的简单技巧:
In the following script we use a MutationObserver (see here and here ) to observe changes in the content of the title element. 在以下脚本中,我们使用MutationObserver (请参阅此处和此处 )观察title元素内容的变化。 Having the title we can extract the integer part of it, which represents a slide number. 有了标题,我们可以提取其中的整数部分,它表示幻灯片编号。 In this example we just show an alert containing the text "Current Slide: " followed by the number we extracted. 在此示例中,我们仅显示一个警报,其中包含文本“当前幻灯片:”,后跟提取的数字。
Here ist the JavaScript snippet: 这里是JavaScript代码段:
<script>
var target = document.querySelector('head > title');
var observer = new window.WebKitMutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var title = mutation.target.textContent;
var current = +title.toString().match(/\d+/g);
alert('Current Slide: ' + current);
});
});
observer.observe(target, { subtree: true, characterData: true, childList: true });
</script>
Since we don't want the snippet to be interpreted as the content of a slide itself, we include it via the YAML header. 由于我们不希望将片段解释为幻灯片本身的内容,因此我们通过YAML标头将其包含在内。 Check the reproducible example below, where header.html contains only the the javascript snippet above: 请查看下面的可复制示例,其中header.html仅包含上面的javascript代码段:
---
title: "Untitled"
author: "Martin Schmelzer"
date: "18 3 2017"
output:
slidy_presentation:
includes:
in_header: header.html
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## R Markdown
Slide 2 (Title slide is #1!)
## Slide with Bullets
This is slide 3!
## Slide with R Output
...and number 4!
If somehow needed in ioslides, you can modify the JavaScript snippet the following way: 如果在ioslides中需要某种方式,则可以通过以下方式修改JavaScript代码段:
<script>
$(document).ready(function() {
var target = document.querySelector('slides');
var observer = new window.WebKitMutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.target.getAttribute('class').indexOf('current') != -1 ) {
alert(mutation.target.getAttribute('data-slide-num'));
}
});
});
observer.observe(target, { subtree: true, attributes: true});
});
</script>
In slidy each slide consists of its own div container. 在滑动中,每个幻灯片都包含其自己的div容器。 The approach in ioslides is different. ioslides中的方法是不同的。 Here we only have a couple of <slide>
elements for the past, current, next and far-next slides. 在这里,对于过去,当前,下一个和下一个幻灯片,我们只有几个<slide>
元素。 The current slide element is the one which has the class current
. 当前幻灯片元件是具有所述类中的一个current
。 So we just search for that class inside the forEach loop and grab it's data-slide-num
attribute. 因此,我们只需要在forEach循环中搜索该类并获取其data-slide-num
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.