Skip to content

Commit e7917c3

Browse files
committed
Add pause button to videos
1 parent 8d2eaa4 commit e7917c3

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

src/css/custom.css

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,10 @@ article {
460460
}
461461

462462
article img,
463-
article video {
463+
article video:not(.video-player video),
464+
.video-player {
464465
vertical-align: top;
465466
margin: var(--ifm-leading) 0;
466-
border-radius: var(--ifm-pre-border-radius);
467467
box-shadow:
468468
0 0 0 1px rgba(0, 0, 0, 0.05),
469469
var(--ifm-global-shadow-lw);
@@ -492,18 +492,54 @@ article video[data-landscape] {
492492
}
493493
}
494494

495-
article video:has(+ video) {
495+
article video:not(.video-player video):has(+ video),
496+
article .video-player:has(+ .video-player) {
496497
margin-right: var(--ifm-leading);
497498
}
498499

499-
article p ~ video {
500+
article p ~ video:not(.video-player video),
501+
article p ~ .video-player {
500502
margin-top: 0;
501503
}
502504

503505
samp {
504506
display: none;
505507
}
506508

509+
.video-player {
510+
position: relative;
511+
display: inline-block;
512+
}
513+
514+
.video-player-icon {
515+
display: flex;
516+
align-items: center;
517+
justify-content: center;
518+
position: absolute;
519+
top: 0;
520+
left: 0;
521+
right: 0;
522+
bottom: 0;
523+
background: rgba(0, 0, 0, 0.3);
524+
transition: 300ms opacity ease;
525+
opacity: 0;
526+
}
527+
528+
.video-player-icon::after {
529+
content: '';
530+
height: 72px;
531+
aspect-ratio: 1 / 1;
532+
background-image: url("data:image/svg+xml,<svg viewBox='0 0 18 18' xmlns='http://www.w3.org/2000/svg'><defs><mask id='pause-mask'><circle cx='9' cy='9' r='9' fill='white'/><rect x='6' y='6' width='2' height='6' fill='black'/><rect x='10' y='6' width='2' height='6' fill='black'/></mask></defs><circle cx='9' cy='9' r='9' fill='white' mask='url(%23pause-mask)'/></svg>");
533+
}
534+
535+
.video-player.paused .video-player-icon::after {
536+
background-image: url("data:image/svg+xml,<svg viewBox='0 0 18 18' xmlns='http://www.w3.org/2000/svg'><defs><mask id='play-mask'><circle cx='9' cy='9' r='9' fill='white'/><polygon points='7.5,6 7.5,12 12.5,9' fill='black'/></mask></defs><circle cx='9' cy='9' r='9' fill='white' mask='url(%23play-mask)'/></svg>");
537+
}
538+
539+
.video-player:is(:hover, :focus, .paused) .video-player-icon {
540+
opacity: 1;
541+
}
542+
507543
.theme-code-block {
508544
box-shadow: none !important;
509545
}

static/js/video-playback.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
function fadeInOnLoad(video) {
1+
function setupPlayer(video) {
2+
const container = document.createElement('div');
3+
const icon = document.createElement('div');
4+
5+
video.parentNode.insertBefore(container, video);
6+
7+
icon.classList.add('video-player-icon');
8+
9+
container.classList.add('video-player');
10+
container.appendChild(video);
11+
container.appendChild(icon);
12+
13+
container.addEventListener('click', () => {
14+
if (video.paused) {
15+
video.play();
16+
container.classList.remove('paused');
17+
} else {
18+
video.pause();
19+
container.classList.add('paused');
20+
}
21+
});
22+
223
if (video.readyState >= 3) {
324
video.style.transition = 'opacity 1s';
425
video.style.opacity = '1';
@@ -24,16 +45,8 @@ document.addEventListener('DOMContentLoaded', () => {
2445
video.dataset.seen = true;
2546

2647
if (video.hasAttribute('playsinline')) {
27-
video.addEventListener('click', () => {
28-
if (video.paused) {
29-
video.play();
30-
} else {
31-
video.pause();
32-
}
33-
});
48+
setupPlayer(video);
3449
}
35-
36-
fadeInOnLoad(video);
3750
});
3851
});
3952

0 commit comments

Comments
 (0)