mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-23 22:01:20 -08:00
优化公用函数
This commit is contained in:
1 parent
a47d93355b
commit
a738f97764
8 files changed
+52
-44
No files matched your search
@@ -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() {
|
||||
|
||||
@@ -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') {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user