-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.js
96 lines (72 loc) · 3.03 KB
/
slider.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
var Slider = (function () {
'use strict';
var SLIDER_THICKNESS = 2;
var LEFT_COLOR = 'white';
var RIGHT_COLOR = '#666666';
var HANDLE_COLOR = 'white';
function Slider (element, initialValue, minValue, maxValue, changeCallback) {
var div = element;
var height = element.offsetHeight;
var length = element.offsetWidth;
var sliderLeftDiv = document.createElement('div');
sliderLeftDiv.style.position = 'absolute';
sliderLeftDiv.style.width = length + 'px';
sliderLeftDiv.style.height = SLIDER_THICKNESS.toFixed(0) + 'px';
sliderLeftDiv.style.backgroundColor = LEFT_COLOR;
sliderLeftDiv.style.top = (height / 2 - 1) + 'px';
sliderLeftDiv.style.zIndex = 999;
div.appendChild(sliderLeftDiv);
var sliderRightDiv = document.createElement('div');
sliderRightDiv.style.position = 'absolute';
sliderRightDiv.style.width = length + 'px';
sliderRightDiv.style.height = SLIDER_THICKNESS.toFixed(0) + 'px';
sliderRightDiv.style.backgroundColor = RIGHT_COLOR;
sliderRightDiv.style.top = (height / 2 - 1) + 'px';
div.appendChild(sliderRightDiv);
var handleDiv = document.createElement('div');
handleDiv.style.position = 'absolute';
handleDiv.style.width = height + 'px';
handleDiv.style.height = height + 'px';
handleDiv.style.borderRadius = height * 0.5 + 'px';
handleDiv.style.cursor = 'ew-resize';
handleDiv.style.background = HANDLE_COLOR;
div.appendChild(handleDiv);
var value = initialValue;
var redraw = function () {
var fraction = (value - minValue) / (maxValue - minValue);
sliderLeftDiv.style.width = fraction * length + 'px';
sliderRightDiv.style.width = (1.0 - fraction) * length + 'px';
sliderRightDiv.style.left = Math.floor(fraction * length) + 'px';
handleDiv.style.left = (Math.floor(fraction * length) - div.offsetHeight / 2) + 'px';
sliderRightDiv.width = (1.0 - fraction) * length + 'px';
};
var onChange = function (event) {
var mouseX = Utilities.getMousePosition(event, div).x;
value = Utilities.clamp((mouseX / length) * (maxValue - minValue) + minValue, minValue, maxValue);
changeCallback(value);
redraw();
};
var mousePressed = false;
div.addEventListener('mousedown', function (event) {
mousePressed = true;
onChange(event);
});
document.addEventListener('mouseup', function (event) {
mousePressed = false;
});
document.addEventListener('mousemove', function (event) {
if (mousePressed) {
onChange(event);
}
});
this.setValue = function (newValue) {
value = newValue;
redraw();
};
this.getValue = function () {
return value;
};
redraw();
}
return Slider;
}());