-
Notifications
You must be signed in to change notification settings - Fork 1
/
slider.js
128 lines (119 loc) · 3.8 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const GRID = 10;
let target = null;
let currentX = 0;
let currentY = 0;
let min = null;
let max = null;
let step = null;
const updateGuides = function(target) {
if (target.attr('data') === undefined) {
target.nextAll('.guide-up').html('<i class="fas fa-chevron-up"></i> ' + (target.text() - (-target.attr('step'))));
target.nextAll('.guide-down').html('<i class="fas fa-chevron-down"></i> ' + (target.text() - target.attr('step')));
} else {
let i = 0;
const data = JSON.parse(target.attr('data'));
for (i = 0; i < data.length; i++) {
if (target.text() === data[i]) break;
}
target.nextAll('.guide-up').html(data[i + 1] !== undefined ? ('<i class="fas fa-chevron-up"></i> ' + data[i + 1]) : '');
target.nextAll('.guide-down').html(data[i - 1] !== undefined ? ('<i class="fas fa-chevron-down"></i> ' + data[i - 1]) : '');
}
};
const MoveGuidesInward = function(target) {
target.nextAll('.guide-up').stop().animate({
top: target.position().top - 5,
opacity: 0,
}, 'fast');
target.nextAll('.guide-down').stop().animate({
top: target.position().top + target.height() - 5,
opacity: 0,
}, 'fast');
};
(function() {
$(document).on('mouseup', function(event) {
if (target !== null) {
MoveGuidesInward(target);
target.stop().animate({
color: '#000',
}, 'fast');
}
target = null;
});
$(document).on('mousemove', function(event) {
if (target !== null && (Math.abs(currentX - event.pageX) > GRID || Math.abs(currentY - event.pageY) > GRID)) {
if (target.attr('data') === undefined) {
let speed = step;
if (event.shiftKey) speed = step * 10;
let value = Math.round((target.text() - (event.pageY - currentY) * speed / GRID + (event.pageX - currentX) * speed / GRID) / speed) * speed;
if (value > max) value = max;
if (value < min) value = min;
target.text(value);
updateGuides(target);
currentX = event.pageX;
currentY = event.pageY;
} else {
let i = 0;
const data = JSON.parse(target.attr('data'));
for (i = 0; i < data.length; i++) {
if (target.text() === data[i]) break;
}
let value = Math.round(i - (event.pageY - currentY) / GRID + (event.pageX - currentX) / GRID);
if (value < 0) value = 0;
if (value > data.length - 1) value = data.length - 1;
target.text(data[value]);
updateGuides(target);
currentX = event.pageX;
currentY = event.pageY;
}
}
});
$('.current').on('mousedown', function(event) {
currentX = event.pageX;
currentY = event.pageY;
min = $(this).attr('min');
max = $(this).attr('max');
step = $(this).attr('step');
target = $(this);
$(this).stop().animate({
color: '#d9534f',
}, 'fast');
});
$('.current').on('mouseover', function(event) {
if (target === null) {
$(this).stop().animate({
color: '#428bca',
}, 'fast');
updateGuides($(this));
$(this).nextAll('.guide-up').css({
left: $(this).position().left,
top: $(this).position().top - 5,
display: 'block',
opacity: 0,
});
$(this).nextAll('.guide-down').css({
left: $(this).position().left,
top: $(this).position().top + $(this).height() - 5,
display: 'block',
opacity: 0,
});
$(this).nextAll('.guide-up').stop().animate({
top: '-=10',
opacity: 1,
}, 'fast');
$(this).nextAll('.guide-down').stop().animate({
top: '+=10',
opacity: 1,
}, 'fast');
}
});
$('.current').on('mouseout', function(event) {
if (target === null) {
MoveGuidesInward($(this));
if (target === null) {
$(this).stop().animate({
color: '#000',
}, 'fast');
}
}
});
})();