-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
113 lines (91 loc) · 3.7 KB
/
scripts.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// scripts.js
document.addEventListener("DOMContentLoaded", function () {
const container = document.getElementById("shortsContainer");
const shortItems = document.querySelectorAll(".short-item");
let currentVideoIndex = 0;
const totalVideos = 7; // Adjust this variable to the total number of videos you have
const observerOptions = {
root: null,
rootMargin: "0px",
threshold: 0.5, // Adjust the threshold as needed
};
const observer = new IntersectionObserver(handleIntersection, observerOptions);
function handleIntersection(entries, observer) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
// Video is in view
setTimeout(() => {
entry.target.play();
}, 200); // Add a small delay (200 milliseconds) before playing
} else {
// Video is out of view
entry.target.pause();
}
});
}
for (let i = 1; i <= totalVideos; i++) {
const shortItem = document.createElement("div");
shortItem.className = "short-item";
const shortVideo = document.createElement("video");
shortVideo.className = "short-video";
shortVideo.src = `video${i}.mp4`; // Adjust the path to your video file
shortVideo.loop = true; // Add loop attribute
const playButton = document.createElement("button");
playButton.className = "play-button";
playButton.innerHTML = "▶"; // Play icon
const audioBar = document.createElement("input");
audioBar.type = "range";
audioBar.min = 0;
audioBar.max = 1;
audioBar.step = 0.01;
audioBar.value = shortVideo.volume;
audioBar.className = "audio-bar";
audioBar.style.display = "none";
// Hide play button initially
playButton.style.display = "none";
// Show play button and audio bar on hover
shortItem.addEventListener("mouseenter", function () {
playButton.style.display = "block";
audioBar.style.display = "block";
});
// Hide play button and audio bar when not hovering
shortItem.addEventListener("mouseleave", function () {
playButton.style.display = "none";
audioBar.style.display = "none";
});
// Play/pause functionality
playButton.addEventListener("click", function () {
if (shortVideo.paused) {
shortVideo.play();
} else {
shortVideo.pause();
}
});
// Volume control
audioBar.addEventListener("input", function () {
shortVideo.volume = audioBar.value;
});
shortItem.appendChild(shortVideo);
shortItem.appendChild(playButton);
shortItem.appendChild(audioBar);
container.appendChild(shortItem);
// Observe each video
observer.observe(shortVideo);
}
window.addEventListener("keydown", function (event) {
if (event.key === "ArrowDown") {
event.preventDefault(); // Prevent the default scroll behavior
currentVideoIndex++;
if (currentVideoIndex >= shortItems.length) {
currentVideoIndex = shortItems.length - 1;
}
const nextVideo = shortItems[currentVideoIndex];
container.scrollTo({
top: nextVideo.offsetTop,
behavior: "smooth",
});
}
});
// Autoplay the first video
shortItems[0].querySelector(".short-video").play();
});