Skip to content

Commit bbca848

Browse files
committed
Merge branch 'master' of github.com:nitishdayal/JavaScript30
2 parents 6c0985d + 85fee69 commit bbca848

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

‎exercises/11 - Custom Video Player/README.md‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,124 @@ Iterate through the variable referencing the volume & playback speed sliders and
5454
a 'change' event listener that will update the _property value of the video element corresponding with the
5555
'name' property of the slider_ to reflect the _'value' property of the slider_, and a 'mousemove' event listener
5656
that will do the same.
57+
58+
**Steps:**
59+
60+
1. Declare seven constants that will be defined as references to the following HTML elements:
61+
- HTML media element (video) with class `player`
62+
- Child of `player` class element with class `viewer` (Example assuming first constant is named `player`: `player.querySelector('.viewer')`)
63+
- Child of `player` element with class `progress`
64+
- Child of `player` element with class `progress__filled` <-- TWO lower dashes
65+
- Child of `player` element with class `toggle`
66+
- **ALL** children of `player` element with a `data-skip` property
67+
- **ALL** children of `player` element with class `player__slider` <-- TWO lower dashes
68+
69+
```JavaScript
70+
const player = document.querySelector('.player'),
71+
video = player.querySelector('.viewer'),
72+
progress = player.querySelector('.progress'),
73+
progressBar = player.querySelector('.progress__filled'),
74+
toggle = player.querySelector('.toggle'),
75+
skipButtons = player.querySelectorAll('[data-skip]'),
76+
ranges = player.querySelectorAll('.player__slider')
77+
```
78+
79+
2. Declare four `let` variables that will be defined as _arrow functions to be used as event handlers_:
80+
- `togglePlay` variable defined as an _arrow function_ that will call on either the `.play()` or `.pause()`
81+
method of the HTMLElement with class `viewer`, depending on whether the `paused` property of that element is
82+
`true` or `false`.
83+
84+
```JavaScript
85+
let togglePlay = () => (video[video.paused ? 'play' : 'pause']())
86+
```
87+
- `updateButton` variable defined as an _arrow function_ that will update the `textContent` property of the HTMLElement
88+
with class `toggle` to be '►' or '❚ ❚' depending on the `boolean` value of the `viewer` HTMLElement's `paused` property.
89+
90+
```JavaScript
91+
let updateButton = () => toggle.textContent = video.paused ? '►' : '❚ ❚';
92+
```
93+
- `handleProgress` variable defined as an _arrow function_ that will set the `flexBasis` style attribute of the
94+
`progressBar` HTMLElement to reflect the current video progress.
95+
96+
```JavaScript
97+
let handleProgress =
98+
() => progressBar.style.flexBasis = `$(video.currentTime/video.duration) * 100}%`
99+
```
100+
- `scrub` variable defined as an _arrow function_ which accepts an _event parameter_, and updates the `currentTime` property
101+
of the `viewer` HTMLElement to reflect the position of the progress bar.
102+
103+
```JavaScript
104+
let scrub = (e) => video.currentTime = ((e.offsetX / progress.offsetWidth) * video.duration)
105+
```
106+
107+
3. Attach four _event listeners_ to the `viewer` HTMLElement:
108+
- A 'click' event listener which will call the `togglePlay` function as an _event handler_.
109+
- A 'play' event listener which will call the `updateButton` function as an _event handler_.
110+
- A 'pause' event listener which will call the `updateButton` function as an _event handler_.
111+
- A 'timeupdate` event listener which will call the `handleProgress` function as an _event handler_.
112+
113+
```JavaScript
114+
video.addEventListener('click', togglePlay)
115+
video.addEventListener('play', updateButton)
116+
video.addEventListener('pause', updateButton)
117+
video.addEventListener('timeupdate', handleProgress)
118+
```
119+
120+
4. Attach an _event listener_ to the `toggle` HTMLElement (in this example, the variable name is `toggle` as well) which will
121+
call the `togglePlay` function as an _event handler_.
122+
123+
```JavaScript
124+
toggle.addEventListener('click', togglePlay)
125+
```
126+
127+
5. Declare a `let` variable `mousedown` and define it as `false`.
128+
129+
```JavaScript
130+
let mousedown = false
131+
```
132+
133+
6. Attach four _event listeners_ to the `progress` HTMLElement:
134+
- A 'click' event listener which will call the `scrub` function as an _event handler_.
135+
- A 'mousemove' event listener which will call the `scrub` function as an _event handler_ **IF** `mousedown` is `true`.
136+
- A 'mousedown' event listener with the _event handler_ defined as an _arrow function_ that
137+
will set `mousedown` to true.
138+
- A 'mouseup` event listener with the _event handler_ defined as an _arrow function_ that
139+
will set `mousedown` to false.
140+
141+
```JavaScript
142+
progress.addEventListener('click', scrub)
143+
progress.addEventListener('mousemove', (e) => mousedown && scrub(e))
144+
progress.addEventListener('mousedown', () => mousedown = true)
145+
progress.addEventListener('mouseup', () => mousedown = false)
146+
```
147+
148+
7. Iterate through the `skipButtons` variable that contains a _NodeList, an array-like object,_ and attach a 'click'
149+
_event listener_ to each button with the _event handler_ defined as an _arrow function_ that increments the `currentTime`
150+
property of the `viewer` HTMLElement with the `data-skip` attribute of that button.
151+
152+
```JavaScript
153+
skipButtons.forEach(button => {
154+
button.addEventListener(
155+
//HTML attributes are string values. We want a number, so we parse the value into a `float`
156+
'click', () => video.currentTime += parseFloat(button.dataset.skip))
157+
})
158+
```
159+
160+
8. Iterate through the `ranges` variable that contains a _NodeList, an array-like object_, and attach two _event listeners_
161+
to each item:
162+
- A 'change' _event listener_ with the _event handler_ defined as an _arrow function_ that updates a property on the
163+
`viewer` HTMLElement _corresponding with the `name` attribute of the given NodeList item_ to the `value` attribute of the
164+
given item.
165+
- A 'mousemove' _event listener_ that does the same thing.
166+
167+
```JavaScript
168+
ranges.forEach(range => {
169+
range.addEventListener('change', () => video[range.name] = range.value)
170+
range.addEventListener('mousemove', () => video[range.name] = range.value)
171+
})
172+
```
173+
174+
9. Wrap all that up in a nice litte IIFE _(immediately invoked function expression)_ to avoid polluting the global namespace
175+
(getting tired of reading that yet?)
176+
177+
**Another day, another challenge, another success! Congratulations! <3**

0 commit comments

Comments
 (0)