-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutils.ts
63 lines (51 loc) · 1.71 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { onMounted, onUnmounted } from 'vue';
export const videoColorModeSrcSwitcher = () => {
let classObserver;
onMounted(() => {
const switchSrc = () => {
const suffix = document.documentElement.classList.contains('dark')
? 'dark'
: 'light';
const videos = document.body.querySelectorAll('video');
for (const video of videos) {
const name = video.dataset.name;
video
.querySelector('source')
?.setAttribute('src', `/videos/${name}-${suffix}.mp4`);
if (video.getAttribute('poster')) {
video.setAttribute('poster', `/images/${name}-${suffix}.jpg`);
}
video.pause();
video.currentTime = 0;
video.load();
}
}
classObserver = new window.MutationObserver((mutations) => {
mutations.forEach((mu) => {
if (mu.type !== 'attributes' && mu.attributeName !== 'class') return;
switchSrc();
});
});
classObserver.observe(document.documentElement, {attributes: true});
switchSrc();
});
onUnmounted(() => {
if (classObserver) classObserver.disconnect();
});
}
export const videoPlayOnHover = () => {
function mouseEnterHandler() { this.play(); }
function mouseLeaveHandler() { this.pause(); }
onMounted(() => {
for (const video of document.body.querySelectorAll('video')) {
video.addEventListener('mouseenter', mouseEnterHandler);
video.addEventListener('mouseleave', mouseLeaveHandler);
}
});
onUnmounted(() => {
for (const video of document.body.querySelectorAll('video')) {
video.removeEventListener('mouseenter', mouseEnterHandler);
video.removeEventListener('mouseleave', mouseLeaveHandler);
}
});
}