Skip to navigation

In-video quiz for web

In-video quizzes pause a FastPix video at set times, ask the viewer a question, and block the controls until they answer. Use them for knowledge checks in training, onboarding, and course content, where you want to confirm the viewer understood a section before they move on.

This recipe builds the whole experience from the page, around an unmodified FastPix web player (fastpix-player). Nothing in the player source changes.


How it works

The quiz is a plain HTML element placed inside the player and assigned to one of the player’s named slots. Because it composes inside the player, it stays visible in fullscreen. The page drives everything through the player’s public API and the media events it already forwards:

  • It watches timeupdate to detect when playback reaches a checkpoint, then calls pause() to stop the video and show the question.
  • It calls play() to resume once every question in the checkpoint is answered.
  • It listens for ended to show the final score.

Questions are grouped into checkpoints. A checkpoint is a timestamp that holds one or more questions, asked in sequence before playback continues. Seeking is safe: if a viewer scrubs past a checkpoint they have not completed, it opens immediately instead of being skipped.


Before you begin

You need:

  • A FastPix playback ID for a video. See Play your videos.
  • The FastPix web player web component (fastpix-player), loaded from the CDN as shown in the code.

Customize the quiz

Edit the CHECKPOINTS array to set your own timestamps, questions, options, correct answers, and explanations. Set at to the time in seconds where each checkpoint fires, and list one or more questions per checkpoint. Replace your-playback-id on the fastpix-player element with your own playback ID.


Full example

The complete page is below. Each step explains one part of the code.

In-video quiz for web
Pause a FastPix video at set times to ask the viewer a question and block the controls until they answer, built from the page around an unmodified fastpix-player.
1. Page head
A standard HTML5 page. The UTF-8 charset matters because the overlay copy uses an em dash and a middle dot, which garble without it. The viewport meta makes the player fit phone screens, and the FastPix player script loads with defer.
2. Styling the player and its slot
3. Styling the quiz overlay
4. The quiz markup in a named slot
5. Getting the player element
6. Defining checkpoints and their questions
7. Flattening the questions
8. Caching the overlay elements
9. Tracking quiz state
10. Rendering the progress dots
11. Finding the next question in a checkpoint
12. Opening a question
13. Recording an answer
14. Advancing or resuming
15. Firing checkpoints as the video plays
16. Showing the results
17. Gating the keyboard
18. Initial render
1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5 <meta charset="UTF-8" />
6 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7 <title>In-video quizzes — FastPix Player</title>
8 <script defer src="https://cdn.jsdelivr.net/npm/@fastpix/fp-player@latest"></script>
9 <style>
10 body {
11 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
12 max-width: 900px;
13 margin: 20px auto;
14 padding: 0 20px;
15 color: #111;
16 }
17
18 h1 { font-size: 1.25rem; font-weight: 600; margin-bottom: 8px; }
19
20 .hint { font-size: 14px; color: #444; line-height: 1.55; margin-bottom: 14px; }
21
22 .hint code {
23 font-size: 12px; background: #f4f4f5; padding: 1px 5px; border-radius: 4px;
24 }
25
26 /* Same no-source-changes technique as overlay.html: a light-DOM child assigned
27 to a named slot composes INSIDE the player, so the quiz survives fullscreen.
28 Only the two documented custom properties are set. */
29 fastpix-player {
30 width: 100%;
31 aspect-ratio: 16 / 9;
32 display: block;
33 background: #000;
34 --user-slot-z: 1700;
35 --user-slot-bottom-clearance: 0px;
36 }
37
38 /* `.fastpix-user-slots` is absolute/inset:0, so an absolutely positioned slotted
39 child covers the whole frame regardless of which corner region it is assigned to. */
40 .quiz {
41 position: absolute;
42 inset: 0;
43 display: grid;
44 place-items: center;
45 padding: 16px;
46 box-sizing: border-box;
47 background: rgba(9, 11, 16, 0.72);
48 backdrop-filter: blur(3px);
49 -webkit-backdrop-filter: blur(3px);
50 color: #fff;
51 -webkit-font-smoothing: antialiased;
52 }
53
54 .quiz[hidden] { display: none; }
55
56 .quiz__card {
57 width: min(560px, 100%);
58 max-height: 100%;
59 overflow: auto;
60 background: #14161d;
61 border: 1px solid rgba(255, 255, 255, 0.1);
62 border-radius: 12px;
63 padding: clamp(12px, 2.2cqw, 22px);
64 box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
65 }
66
67 .quiz__meta {
68 display: flex; align-items: center; justify-content: space-between;
69 gap: 10px; margin-bottom: 10px;
70 font-size: clamp(9px, 1.05cqw, 11px);
71 letter-spacing: 0.08em; text-transform: uppercase;
72 color: rgba(255, 255, 255, 0.55);
73 }
74
75 .quiz__dots { display: flex; gap: 5px; }
76
77 .quiz__dot {
78 width: 6px; height: 6px; border-radius: 50%;
79 background: rgba(255, 255, 255, 0.25);
80 }
81 .quiz__dot--done { background: #34d399; }
82 .quiz__dot--wrong { background: #f87171; }
83 .quiz__dot--current { background: #fff; }
84
85 .quiz__q {
86 font-size: clamp(13px, 1.85cqw, 19px);
87 line-height: 1.35; margin: 0 0 14px; font-weight: 600;
88 }
89
90 .quiz__options { display: grid; gap: 8px; }
91
92 .quiz__opt {
93 display: flex; align-items: center; gap: 10px;
94 width: 100%; text-align: left; cursor: pointer;
95 padding: clamp(8px, 1.2cqw, 12px) 14px;
96 font-size: clamp(11px, 1.35cqw, 14px);
97 color: #fff;
98 background: rgba(255, 255, 255, 0.06);
99 border: 1px solid rgba(255, 255, 255, 0.14);
100 border-radius: 8px;
101 font-family: inherit;
102 }
103
104 .quiz__opt:hover:not(:disabled) { background: rgba(255, 255, 255, 0.12); }
105 .quiz__opt:disabled { cursor: default; }
106
107 .quiz__opt-key {
108 flex: none;
109 width: 22px; height: 22px; border-radius: 5px;
110 display: grid; place-items: center;
111 font-size: 11px; font-weight: 700;
112 background: rgba(255, 255, 255, 0.12);
113 }
114
115 .quiz__opt--correct {
116 background: rgba(52, 211, 153, 0.16);
117 border-color: rgba(52, 211, 153, 0.65);
118 }
119 .quiz__opt--correct .quiz__opt-key { background: #34d399; color: #06281d; }
120
121 .quiz__opt--wrong {
122 background: rgba(248, 113, 113, 0.14);
123 border-color: rgba(248, 113, 113, 0.6);
124 }
125 .quiz__opt--wrong .quiz__opt-key { background: #f87171; color: #2b0a0a; }
126
127 .quiz__feedback {
128 margin: 12px 0 0;
129 font-size: clamp(10px, 1.25cqw, 13px);
130 line-height: 1.5;
131 color: rgba(255, 255, 255, 0.82);
132 }
133
134 .quiz__foot { display: flex; justify-content: flex-end; margin-top: 14px; }
135
136 .quiz__next {
137 border: 0; border-radius: 7px; cursor: pointer;
138 background: #fff; color: #111;
139 font-weight: 650; font-family: inherit;
140 font-size: clamp(10px, 1.2cqw, 13px);
141 padding: clamp(8px, 1.1cqw, 11px) clamp(14px, 1.8cqw, 20px);
142 }
143
144 .quiz__next:hover { background: #e9e9ee; }
145
146 .quiz__score { font-size: clamp(12px, 1.6cqw, 16px); margin: 0 0 6px; }
147 </style>
148</head>
149
150<body>
151 <h1>In-video quizzes</h1>
152 <p class="hint">
153 Questions fire at cue points, pause playback, and block the controls until answered.
154 Driven entirely from this page using events the player already forwards
155 (<code>timeupdate</code>, <code>ended</code>) and its public
156 <code>play()</code> / <code>pause()</code> / <code>currentTime</code> API —
157 nothing in <code>src/</code> is modified. While a question is open the player's keyboard
158 shortcuts are suppressed as well, and <kbd>A</kbd>–<kbd>D</kbd> pick an answer.
159 </p>
160
161 <fastpix-player id="player" playback-id="your-playback-id" preload="auto">
162 <div class="quiz" slot="top-left" role="dialog" aria-modal="true" aria-labelledby="quiz-q" hidden>
163 <div class="quiz__card">
164 <div class="quiz__meta">
165 <span id="quiz-step">Question 1 of 3</span>
166 <span class="quiz__dots" id="quiz-dots" aria-hidden="true"></span>
167 </div>
168 <p class="quiz__q" id="quiz-q"></p>
169 <div class="quiz__options" id="quiz-options"></div>
170 <p class="quiz__feedback" id="quiz-feedback" hidden></p>
171 <div class="quiz__foot">
172 <button class="quiz__next" id="quiz-next" type="button" hidden>Continue</button>
173 </div>
174 </div>
175 </div>
176 </fastpix-player>
177
178 <script>
179
180 const player = document.getElementById("player");
181
182 // Each CHECKPOINT is a cue point holding a SET of questions, asked in sequence.
183 // One question per checkpoint is just a set of length 1, so the simple case is
184 // unchanged — this only adds the ability to group several at the same timestamp.
185 const CHECKPOINTS = [
186 {
187 at: 8,
188 questions: [
189 {
190 q: "What does a cue point do in this demo?",
191 options: ["Mutes the player", "Pauses playback and asks a question", "Changes quality", "Skips the intro"],
192 answer: 1,
193 why: "Crossing a cue pauses the video and opens the checkpoint. Playback resumes only after every question in it is answered.",
194 },
195 {
196 q: "How many questions can one cue point hold?",
197 options: ["Exactly one", "As many as you list", "Two at most", "One per platform"],
198 answer: 1,
199 why: "A checkpoint owns an array of questions; they are asked one after another before playback continues.",
200 },
201 ],
202 },
203 {
204 at: 20,
205 questions: [
206 {
207 q: "Where does this quiz overlay actually live?",
208 options: ["In the player's source", "In a browser extension", "In a named slot inside the player", "In an iframe"],
209 answer: 2,
210 why: "It is a light-DOM child assigned to a slot, so it composes inside the player and keeps working in fullscreen.",
211 },
212 {
213 q: "Which player API drives the pause and resume?",
214 options: ["Only CSS", "play() and pause() on the element", "A private internal method", "The HLS manager"],
215 answer: 1,
216 why: "The element exposes play(), pause(), currentTime and duration publicly, so the page needs nothing internal.",
217 },
218 {
219 q: "Which events does the page listen to?",
220 options: ["timeupdate and ended", "click only", "scroll and resize", "none — it polls"],
221 answer: 0,
222 why: "The player forwards the media events from its internal <video>, so timeupdate drives the cue check and ended shows the results.",
223 },
224 ],
225 },
226 {
227 at: 34,
228 questions: [
229 {
230 q: "Why can't you scrub past an open question?",
231 options: [
232 "Seeking is disabled globally",
233 "The overlay covers the controls, and any missed cue fires immediately",
234 "The video is downloaded first",
235 "The player blocks all input",
236 ],
237 answer: 1,
238 why: "The overlay sits above the control bar, and the cue check picks the earliest checkpoint with unanswered questions at or before the current time.",
239 },
240 ],
241 },
242 ];
243
244 // Flatten to a single ordered list so progress, scoring and the dots stay simple.
245 const FLAT = CHECKPOINTS.flatMap((cp, ci) => cp.questions.map((question, qi) => ({ ci, qi, question })));
246 const keyOf = (ci, qi) => `${ci}.${qi}`;
247
248 const el = {
249 root: document.querySelector(".quiz"),
250 step: document.getElementById("quiz-step"),
251 dots: document.getElementById("quiz-dots"),
252 q: document.getElementById("quiz-q"),
253 options: document.getElementById("quiz-options"),
254 feedback: document.getElementById("quiz-feedback"),
255 next: document.getElementById("quiz-next"),
256 };
257
258 const state = { active: null, answered: new Map(), score: 0 };
259 const KEYS = ["A", "B", "C", "D", "E"];
260
261 function renderDots() {
262 el.dots.innerHTML = "";
263 FLAT.forEach(({ ci, qi }) => {
264 const dot = document.createElement("span");
265 const result = state.answered.get(keyOf(ci, qi));
266 const isCurrent = state.active && state.active.ci === ci && state.active.qi === qi;
267 // A result outranks "current": once answered, the dot must keep showing right/wrong.
268 // Adding both classes would let --current win on CSS order and hide the outcome.
269 dot.className =
270 "quiz__dot" +
271 (result === true
272 ? " quiz__dot--done"
273 : result === false
274 ? " quiz__dot--wrong"
275 : isCurrent
276 ? " quiz__dot--current"
277 : "");
278 el.dots.appendChild(dot);
279 });
280 }
281
282 /** The next unanswered question inside a checkpoint, or null when the set is complete. */
283 function nextInCheckpoint(ci) {
284 const qi = CHECKPOINTS[ci].questions.findIndex((_, i) => !state.answered.has(keyOf(ci, i)));
285 return qi === -1 ? null : qi;
286 }
287
288 function openQuestion(ci, qi) {
289 const { question } = FLAT.find((f) => f.ci === ci && f.qi === qi);
290 state.active = { ci, qi };
291 player.pause(); // a checkpoint is a hard stop, not an overlay you can play behind
292
293 const setSize = CHECKPOINTS[ci].questions.length;
294 el.step.textContent =
295 setSize > 1
296 ? `Checkpoint ${ci + 1} · Question ${qi + 1} of ${setSize}`
297 : `Checkpoint ${ci + 1} of ${CHECKPOINTS.length}`;
298 el.q.textContent = question.q;
299 el.feedback.hidden = true;
300 el.next.hidden = true;
301 el.options.innerHTML = "";
302 renderDots();
303
304 question.options.forEach((label, i) => {
305 const btn = document.createElement("button");
306 btn.type = "button";
307 btn.className = "quiz__opt";
308 btn.innerHTML = `<span class="quiz__opt-key">${KEYS[i]}</span><span>${label}</span>`;
309 btn.addEventListener("click", () => answer(ci, qi, i));
310 el.options.appendChild(btn);
311 });
312
313 el.root.hidden = false;
314 el.options.querySelector("button")?.focus();
315 }
316
317 function answer(ci, qi, chosen) {
318 const { question } = FLAT.find((f) => f.ci === ci && f.qi === qi);
319 const correct = chosen === question.answer;
320 if (!state.answered.has(keyOf(ci, qi))) {
321 state.answered.set(keyOf(ci, qi), correct);
322 if (correct) state.score += 1;
323 }
324
325 // Lock the choices and mark the right answer as well as the wrong pick, so a
326 // learner sees WHAT was right — not just that they missed it.
327 [...el.options.children].forEach((btn, i) => {
328 btn.disabled = true;
329 if (i === question.answer) btn.classList.add("quiz__opt--correct");
330 else if (i === chosen) btn.classList.add("quiz__opt--wrong");
331 });
332
333 el.feedback.textContent = (correct ? "Correct. " : "Not quite. ") + question.why;
334 el.feedback.hidden = false;
335
336 // Stay inside the checkpoint until its whole set is done; only then resume.
337 const remaining = nextInCheckpoint(ci);
338 el.next.textContent = remaining !== null ? "Next question" : "Continue";
339 el.next.hidden = false;
340 el.next.focus();
341 renderDots();
342 }
343
344 el.next.addEventListener("click", () => {
345 const { ci } = state.active ?? {};
346 const remaining = ci === undefined ? null : nextInCheckpoint(ci);
347 if (remaining !== null) {
348 openQuestion(ci, remaining); // next question in the same checkpoint — stay paused
349 return;
350 }
351 el.root.hidden = true;
352 state.active = null;
353 renderDots();
354 player.play();
355 });
356
357 // Pick the EARLIEST checkpoint with unanswered questions at or before the current
358 // time. Using "at or before" rather than an exact crossing is what makes seeking
359 // safe: jump past a checkpoint and it opens immediately instead of being skipped.
360 player.addEventListener("timeupdate", () => {
361 if (state.active !== null || el.root.hidden === false) return;
362 const t = player.currentTime;
363 const ci = CHECKPOINTS.findIndex((cp, i) => t >= cp.at && nextInCheckpoint(i) !== null);
364 if (ci !== -1) openQuestion(ci, nextInCheckpoint(ci));
365 });
366
367 player.addEventListener("ended", () => {
368 state.active = null;
369 el.step.textContent = "Results";
370 el.q.textContent = `You scored ${state.score} of ${FLAT.length}.`;
371 el.options.innerHTML = "";
372 el.feedback.textContent =
373 state.answered.size < FLAT.length
374 ? "Some questions were never reached — seek back to pick them up."
375 : state.score === FLAT.length
376 ? "Full marks."
377 : "Review the explanations and try again.";
378 el.feedback.hidden = false;
379 el.next.hidden = true;
380 renderDots();
381 el.root.hidden = false;
382 });
383
384 // ── Keyboard gate ────────────────────────────────────────────────────────
385 // The overlay hides the control BAR, but the player also binds its shortcuts to
386 // `document`, so K / M / F / C and the arrows would still drive playback behind an
387 // open question. The public `disable-keyboard-controls` attribute cannot help: it is
388 // read once during setup (utils/index.ts) and decides whether the listener is ever
389 // attached, so flipping it at runtime does nothing.
390 //
391 // Instead we listen on `document` in the CAPTURE phase. The player's listener is a
392 // bubble-phase listener on the same node, and capture at the root runs first, so
393 // stopping propagation there means the player never sees the key. The player
394 // registers no capture-phase listeners, so nothing can outrun this.
395 const PLAYER_HOTKEYS = new Set([
396 "KeyK", "KeyM", "KeyF", "KeyC",
397 "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight",
398 ]);
399
400 document.addEventListener(
401 "keydown",
402 (event) => {
403 if (el.root.hidden) return; // quiz closed -> the player keeps every shortcut
404
405 // Answer with the letter keys the options already advertise (A, B, C, …).
406 const letter = KEYS.indexOf(event.key.toUpperCase());
407 if (letter !== -1 && state.active) {
408 const btn = el.options.children[letter];
409 if (btn && !btn.disabled) {
410 event.preventDefault();
411 btn.click();
412 }
413 }
414
415 // Swallow only the player's own keys. Enter / Space / Tab are deliberately left
416 // alone so the question stays operable by keyboard — stopping propagation for
417 // those would also stop the event reaching our own buttons.
418 if (PLAYER_HOTKEYS.has(event.code)) {
419 event.preventDefault();
420 event.stopImmediatePropagation();
421 }
422 },
423 true, // capture
424 );
425
426
427 renderDots();
428
429 </script>
430</body>
431
432</html>
433