Skip to content

Instantly share code, notes, and snippets.

@nanna-dk
Created August 17, 2021 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanna-dk/7fe91d29d453e46881e53783a9c5644c to your computer and use it in GitHub Desktop.
Save nanna-dk/7fe91d29d453e46881e53783a9c5644c to your computer and use it in GitHub Desktop.
document.addEventListener('DOMContentLoaded', function() {
var lang = document.documentElement.lang;
var translations;
if (lang == 'da') {
translations = {
'pause': 'Stop afspilning (brug Enter tast)',
'play': 'Afspil (brug Enter tast)'
}
} else {
translations = {
'pause': 'Pause (use Enter key)',
'play': 'Play (use Enter bar)'
}
}
class Video {
constructor(video) {
this.videoContainer = video;
this.video = this.videoContainer.querySelector('.video');
this.toggleButton = this.videoContainer.querySelector('.play-pause-button');
this.prefersReducedMotion();
this.addEventListeners();
}
addEventListeners() {
this.toggleButton.addEventListener('click', () => {
this.togglePlayPause();
});
}
togglePlayPause() {
if (this.video.paused || this.video.ended) {
this.playVideo();
} else {
this.pauseVideo();
}
}
playVideo() {
this.video.play();
this.toggleButton.classList.remove('paused');
this.toggleButton.setAttribute('aria-pressed', 'false');
this.toggleButton.setAttribute('aria-label', translations.pause);
}
pauseVideo() {
this.video.pause();
this.toggleButton.classList.add('paused');
this.toggleButton.setAttribute('aria-pressed', 'true');
this.toggleButton.setAttribute('aria-label', translations.play);
}
prefersReducedMotion() {
if (matchMedia('(prefers-reduced-motion)').matches) {
this.video.removeAttribute('autoplay');
this.pauseVideo();
this.video.currentTime = 0;
}
}
}
const videos = document.querySelectorAll('.video-container');
Array.from(videos).forEach((video) => {
const videoEl = new Video(video);
});
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment