-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (32 loc) · 1.36 KB
/
index.js
File metadata and controls
41 lines (32 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(function() {
const video = document.querySelector('video')
const isTouchScreen = ('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0)
const events = {
start: isTouchScreen ? 'touchstart' : 'mousedown',
move: isTouchScreen ? 'touchmove' : 'mousemove',
end: isTouchScreen ? 'touchend' : 'mouseup',
}
const params = { 'timeValue': 0, 'mouseX': 0 }
video.addEventListener(events.start, dragStart, false)
video.addEventListener(events.end, dragEnd, false)
function dragStart(event) {
video.addEventListener(events.move, dragProgress, false)
params.mouseX = event.pageX ? event.pageX : event.touches[0].pageX
params.timeValue = video.currentTime
}
function dragProgress(event) {
if (video.seeking) {
return // to reduce lag in chrome and firefox
}
const step = video.getBoundingClientRect().width / video.duration
const currentX = event.pageX ? event.pageX : event.touches[0].pageX
const timeChange = (currentX - params.mouseX) / (2 * step)
const change = params.timeValue + timeChange
video.currentTime = change < 0
? change % video.duration + video.duration
: change % video.duration
}
function dragEnd() {
video.removeEventListener(events.move, dragProgress, false)
}
})()