优化公用函数

This commit is contained in:
Harvey Zack committed 2018-10-24 22:02:03 +08:00
1 parent a47d93355b
commit a738f97764
8 files changed
+52 -44

No files matched your search

-13
View File
@@ -1,4 +1,3 @@
import { secondToTime, clamp } from '../utils';
import Danmu from './danmu';
import Fullscreen from './fullscreen';
import Pip from './pip';
@@ -143,18 +142,6 @@ export default class Controls {
this.art.emit('control:show', control.option.$control);
}
});
Object.defineProperty(control, 'getPosFromEvent', {
value: event => {
const { $video, $progress } = this.art.refs;
const { left } = $progress.getBoundingClientRect();
const width = clamp(event.x - left, 0, $progress.clientWidth);
const second = width / $progress.clientWidth * $video.duration;
const time = secondToTime(second);
const percentage = clamp(width / $progress.clientWidth, 0, 1);
return { second, time, width, percentage };
}
});
}
show() {
+11 -1
View File
@@ -1,4 +1,4 @@
import { append } from '../utils';
import { append, clamp, secondToTime } from '../utils';
export default class Progress {
constructor(option) {
@@ -140,6 +140,16 @@ export default class Progress {
: 0;
}
getPosFromEvent(event) {
const { $video, $progress } = this.art.refs;
const { left } = $progress.getBoundingClientRect();
const width = clamp(event.x - left, 0, $progress.clientWidth);
const second = width / $progress.clientWidth * $video.duration;
const time = secondToTime(second);
const percentage = clamp(width / $progress.clientWidth, 0, 1);
return { second, time, width, percentage };
}
set(type, percentage) {
this[`$${type}`].style.width = `${percentage * 100}%`;
if (type === 'played') {
+2 -2
View File
@@ -52,8 +52,8 @@ export default class Thumbnails {
}
showThumbnails(event) {
const { $progress } = this.art.refs;
const { width: posWidth } = this.getPosFromEvent(event);
const { refs: { $progress }, controls } = this.art;
const { width: posWidth } = controls.progress.getPosFromEvent(event);
const { url, height, width, number, column } = this.art.option.thumbnails;
const perWidth = $progress.clientWidth / number;
const perIndex = Math.ceil(posWidth / perWidth);