Skip to content

Commit 3f38023

Browse files
committed
add readme file of challenge 28
1 parent 458c1e6 commit 3f38023

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
> 在Github上看到了[wesbos](https://twitter.com/wesbos)的一个Javascript30天挑战的[repo](https://github.com/wesbos/JavaScript30),旨在使用纯Js来进行练习,不允许使用任何其他的库和框架,该挑战共30天,我会在这里记录下自己练习的过程和遇到的问题。
2+
3+
4+
第28天的练习是一个视频播放速率控制的效果。
5+
6+
> [线上DEMO](http://htmlpreview.github.io/?https://github.com/winar-jin/JavaScript30-Challenge/blob/master/28%20-%20Video%20Speed%20Controller/index.html)
7+
8+
## 源码
9+
```javascript
10+
const video = document.querySelector('.flex');
11+
const speed = document.querySelector('.speed');
12+
const speedBar = speed.querySelector('.speed-bar');
13+
14+
function handelMove(e) {
15+
const y = e.pageY - this.offsetTop;
16+
const percent = y / this.offsetHeight;
17+
const height = Math.ceil(percent * 100) + '%';
18+
const min = 0.4;
19+
const max = 4;
20+
const playBackRate = (max - min) * percent + min;
21+
speedBar.style.height = height;
22+
speedBar.textContent = playBackRate.toFixed(2) + '×';
23+
video.playbackRate = playBackRate;
24+
}
25+
26+
speed.addEventListener('mousemove', handelMove, false);
27+
```
28+
目的是要通过视频右边的拖动条来控制视频的播放速率,因此首先要监听右侧`speedBar``mousemove`事件,然后通过计算鼠标当前所在的位置占滚动条的距离的百分比,通过此百分比映射到播放速率的最大值和最小值,最后改变右侧滚动条的高度和`video``playbackRate`属性即可完成对视频播放速率的控制。
29+
30+
## 部分代码解析
31+
* `e.pageY`: 鼠标当前所在位置距离页面顶端的距离
32+
* `this.offsetTop`: 当前元素顶端距离页面顶端的距离
33+
* `this.offsetHeightH`: 当前元素的高度
34+
* `const playBackRate = (max - min) * percent + min;`: 通过这句话能够将百分比映射到最大值和最小值之间,由于最小值不是从0开始的,因此应该用它们的跨度乘以百分比,在加上最小值,这种思想也可以当做一个公式,在求最小值到最大值之间的随机数时也会用到这句话。
35+
* `video.playbackRate = playBackRate;`: `playbackRate`属性代表视频的播放速率,是一个可读可写的属性,因此我们可直接为该属性赋值。
36+
* 有一点需要注意,在我们为`speed`元素绑定`mousemove`事件的时候,若在事件监听器中有用的`this`,建议不要使用箭头函数,因为es6的箭头函数会绑定父级作用域,若在这种情况下使用箭头函数会发生意外的错误。
37+
38+
39+
END! 💯

0 commit comments

Comments
 (0)