Skip to content

Commit d201e45

Browse files
committed
And anotha one
you loyal
1 parent 79e7a22 commit d201e45

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed
Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
(() => {
23
const player = document.querySelector(".player"),
34
video = player.querySelector(".viewer"),
@@ -12,30 +13,38 @@
1213
handleProgress = () =>
1314
progressBar.style.flexBasis = `${video.currentTime / video.duration * 100}%`,
1415
scrub = e =>
15-
video.currentTime = e.offsetX / progress.offsetWidth * video.duration;
16-
17-
video.addEventListener("click", togglePlay);
18-
video.addEventListener("play", updateButton);
19-
video.addEventListener("pause", updateButton);
20-
video.addEventListener("timeupdate", handleProgress);
21-
22-
toggle.addEventListener("click", togglePlay);
16+
video.currentTime = e.offsetX / progress.offsetWidth * video.duration,
17+
progressMoved = e => mousedown && scrub(e),
18+
progressUp = e => mousedown = false,
19+
progressDown = e => mousedown = true,
20+
bClick = b => video.currentTime += parseFloat(b.dataset.skip),
21+
updateRange = (range, e) => video[range.name] = range.value;
2322

2423
let mousedown = false;
25-
progress.addEventListener("click", scrub);
26-
progress.addEventListener("mousemove", e => mousedown && scrub(e));
27-
progress.addEventListener("mousedown", () => mousedown = true);
28-
progress.addEventListener("mouseup", () => mousedown = false);
2924

30-
skipButtons.forEach(button => {
31-
button.addEventListener(
32-
"click",
33-
() => video.currentTime += parseFloat(button.dataset.skip)
34-
);
35-
});
25+
const events = [
26+
{ event: "click", handler: togglePlay, target: video },
27+
{ event: "play", handler: updateButton, target: video },
28+
{ event: "pause", handler: updateButton, target: video },
29+
{ event: "timeupdate", handler: handleProgress, target: video },
30+
{ event: "click", handler: togglePlay, target: toggle },
31+
{ event: "click", handler: scrub, target: progress },
32+
{ event: "mousedown", handler: progressDown, target: progress },
33+
{ event: "mousemove", handler: progressMoved, target: progress },
34+
{ event: "mouseup", handler: progressUp, target: progress },
35+
{ event: "click", handler: bClick, target: skipButtons },
36+
{ event: ["change", "mousemove"], handler: updateRange, target: ranges }
37+
];
3638

37-
ranges.forEach(range => {
38-
range.addEventListener("change", () => video[range.name] = range.value);
39-
range.addEventListener("mousemove", () => video[range.name] = range.value);
40-
});
39+
events.forEach(
40+
({ event: e, handler: h, target: t }) =>
41+
(t instanceof NodeList
42+
? t.forEach((el, i) => {
43+
el.addEventListener(
44+
typeof e === "string" ? e : e[i],
45+
typeof h === "function" ? h.bind(null, el) : h[i].bind(null, el)
46+
);
47+
})
48+
: t.addEventListener(e, h))
49+
);
4150
})();

exercises/13 - Slide in on Scroll/index.html

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,20 @@ <h1>Slide in on Scroll</h1>
139139
(() => {
140140
const sliderImages = document.querySelectorAll('.slide-in');
141141

142-
function debounce(func, wait = 20, immediate = true) {
142+
const debounce = (func, wait = 20, immediate = true) => {
143143
let timeout;
144-
return function () {
145-
const context = this, args = arguments;
146-
const later = function () {
144+
145+
return (...args)=> {
146+
const later = () => {
147147
timeout = null;
148-
if (!immediate) func.apply(context, args);
148+
if (!immediate) func.apply(this, args);
149149
};
150+
150151
const callNow = immediate && !timeout;
151152
clearTimeout(timeout);
152153
timeout = setTimeout(later, wait);
153-
if (callNow) func.apply(context, args);
154+
155+
if (callNow) func.apply(this, args);
154156
};
155157
};
156158

@@ -229,4 +231,4 @@ <h1>Slide in on Scroll</h1>
229231

230232
</body>
231233

232-
</html>
234+
</html>

0 commit comments

Comments
 (0)