-
Notifications
You must be signed in to change notification settings - Fork 2
/
seekbar.js
99 lines (86 loc) · 2.88 KB
/
seekbar.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
window.addEventListener('load', function () {
var pointer_down = false;
var seekbar = document.getElementsByClassName('seekbar');
for (var i = 0; i < seekbar.length; i++) {
/*
* Initial Seekbar
*/
seekbar[i].innerHTML = '<div class="seekbar_btn"></div><div class="seekbar_bg"></div>';
var seekbar_btn = seekbar[i].getElementsByClassName('seekbar_btn')[0];
var seekbar_bg = seekbar[i].getElementsByClassName('seekbar_bg')[0];
if (seekbar[i].hasAttribute('data-seekbar-value')) {
var posi = seekbar[i].getAttribute('data-seekbar-value');
seekbar_setposi(seekbar_btn, seekbar_bg, posi);
} else {
seekbar[i].setAttribute('data-seekbar-value', '0');
}
/*
* CLick Function
*/
seekbar[i].addEventListener('click', function (e) {
var posi = seekbar_getposi(this, e);
seekbar_setposi(ch_elm(this, 'seekbar_btn'), ch_elm(this, 'seekbar_bg'), posi);
}, false);
/*
* Move or Drag Function
*/
seekbar[i].addEventListener('mousemove', function (e) {
if (pointer_down === true) {
var posi = seekbar_getposi(this, e);
if (posi < 0 || posi > 100) {
remv_evt();
} else {
seekbar_setposi(ch_elm(this, 'seekbar_btn'), ch_elm(this, 'seekbar_bg'), posi);
}
} else {
remv_evt();
}
}, false);
}
window.addEventListener('mousedown', function (e) {
pointer_down = true;
}, false);
window.addEventListener('mouseup', function () {
pointer_down = false;
remv_evt();
}, false);
}, false);
function ch_elm(parent, child) {
return parent.getElementsByClassName(child)[0];
}
/*
* Get Position
*/
function seekbar_getposi(elem, e) {
if (elem !== undefined && e !== undefined) {
var prefix = parseInt(elem.offsetLeft);
var point_e = (((parseInt(e.pageX) - prefix) / parseInt(elem.clientWidth)) * 100);
if (point_e < 0) {
point_e = 0;
} else if (point_e > 100) {
point_e = 100;
}
console.log('get: ' + point_e);
return Math.round(point_e);
}
}
/*
* Set Position
*/
function seekbar_setposi(btn, bg, posi) {
if (btn !== undefined) {
btn.style.left = posi + "%";
bg.style.width = posi + "%";
btn.parentElement.setAttribute('data-seekbar-value', posi);
console.log('set: ' + posi);
}
}
/*
* Remove Events
*/
function remv_evt() {
window.removeEventListener('mousedown', seekbar_getposi, true);
window.removeEventListener('mousemove', seekbar_getposi, true);
window.removeEventListener('mousedown', seekbar_setposi, true);
window.removeEventListener('mousemove', seekbar_setposi, true);
}