This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowl.knob.js
243 lines (197 loc) · 6.8 KB
/
owl.knob.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Knob Plugin
* @version 0.1
* @author Norman Wink
* @license The MIT License (MIT)
* @since 2.3.3
*/
;(function ( $, window, document, undefined ) {
/**
* Allows navigating with a draggable knob.
* @class Knob
* @param {Owl} scope - The Owl Carousel
*/
var Knob = function(scope) {
/**
* Reference to the core.
* @protected
* @type {Owl}
*/
this.owl = scope;
this.owl._options = $.extend({}, Knob.Defaults, this.owl.options);
// default values
this.Defaults = {
knobTransition: '200ms cubic-bezier(0.645, 0.045, 0.355, 1)',
knobEasing: function(d) {return d < 0 ? -(d * d) : (d * d)}, // sensitivity curve
knobFPS: 60,
knobSpeed: 120, // pixel per tick at max velocity
hiSpeed: 'hi-speed', // element gets this class when fast
triggerSpeedClass: 0.7, // add hi-speed class at which velocity?
bounceDistance: 400, // px
trackActive: true, // disable to increase performance
}
// variables to work with
// NOT config
this.isDragging = false;
this.dragStart = 0;
this.knobSlide = 0;
this.bounds = {
left: 0,
right: 0
}
this.velocity = 0;
this.current = 0;
this.currX = 0;
this.max = 0;
// all event handlers
this._handlers = {
'initialized.owl.carousel': $.proxy(function(event) {
this.owl.$element.append('<div class="owl-knob"><div class="owl-knob-klickbox"><div class="owl-knob-button"></div></div></div>');
this.$knobWrap = this.owl.$element.find('.owl-knob');
this.$knob = this.owl.$element.find('.owl-knob-klickbox');
// take measurements
this.measure()
this.$knob.on('mousedown touchstart', $.proxy(function(e) {
e.preventDefault ? e.preventDefault() : (event.returnValue = false);
this.isDragging = true;
this.$knob.css('transition', 'none');
this.owl.$stage.css('transition', 'none');
this.dragStart = e.pageX;
this.owl.$element.addClass(this.owl.options.grabClass);
// schedule first animation frame
this.animate();
}, this));
$(window).on('mouseup touchend', $.proxy(function(e) {
this.isDragging = false;
this.owl.$element.removeClass(this.owl.options.grabClass);
}, this));
$(window).on('mousemove touchmove', $.proxy(function(e) {
if (this.isDragging) e.preventDefault ? e.preventDefault() : (event.returnValue = false);
this.update(e);
}, this));
}, this),
'resized.owl.carousel': $.proxy(function(e) {
// take measurements
this.measure();
}, this),
'changed.owl.carousel': $.proxy(function(e) {
this.current = e.item.index;
}, this),
}
// execute while dragging
// update knob position
this.update = $.proxy(function(e) {
if (this.isDragging) {
var draggedDistance = e.pageX - this.dragStart;
// limit drag distance within boundaries of the knob
var limitDrag = Math.max(this.bounds.left, Math.min(draggedDistance, this.bounds.right));
this.velocity = limitDrag / this.bounds.right;
// add or remove the hi-speed class
if (Math.abs(this.velocity) > this.Defaults.triggerSpeedClass) {
this.owl.$element.addClass(this.Defaults.hiSpeed);
} else {
this.owl.$element.removeClass(this.Defaults.hiSpeed);
}
this.$knob.css('transform', 'translateX(' + limitDrag + 'px)');
}
}, this);
// animate slider
this.animate = $.proxy(function() {
// var currX = this.current == 0 ? 0 : this.owl._coordinates[this.current - 1];
var currVelocity = this.Defaults.knobEasing(this.velocity);
this.currX = this.getX();
var movingLeft = currVelocity < 0;
var movingRight = currVelocity > 0;
var leftEnd = this.currX > 0;
var rightEnd = this.currX < this.max;
if ((leftEnd && movingLeft) || (rightEnd && movingRight) ) {
// add increasing resistance at end of slider
if (rightEnd) {
this.currX += ((this.max - this.Defaults.bounceDistance) - this.currX) * (currVelocity * 0.1);
} else {
this.currX -= (this.Defaults.bounceDistance - this.currX) * (currVelocity * 0.1);
}
} else {
this.currX -= currVelocity * this.Defaults.knobSpeed;
}
this.owl.$stage.css('transform', 'translate3d(' + this.currX + 'px, 0, 0)');
// find active while dragging
if (this.Defaults.trackActive) {
// find current slide by floating coordinate
var i = 0;
var coord = 0;
while ((this.currX + (0.5 * this.owl._width)) < coord) {
coord = this.owl._coordinates[i];
i++;
}
if (i >= this.owl._coordinates.length) {
i = this.owl._coordinates.length - 1;
}
// apply current
this.current = i;
this.owl.$element.find('.active').removeClass('active');
this.owl.$element.find('.owl-item').eq(i).addClass('active');
this.owl.$element.find('.owl-dot').eq(i).addClass('active');
}
// schedule next animation frame
if (this.isDragging) {
window.requestAnimationFrame(this.animate);
} else {
this.snap();
}
}, this);
// snap the knob back to place
// move slide to next
this.snap = $.proxy(function() {
this.$knob.css('transition', 'transform ' + this.Defaults.knobTransition);
this.$knob.css('transform', 'translateX(0px)');
this.owl.$stage.css('transition', this.owl._options.smartSpeed + 'ms');
var leftEnd = this.currX > 0;
var rightEnd = this.currX < this.max;
var dir = 'left';
// move slide some random place to force retriggering
this.owl.to(2);
// determine direction
if (leftEnd) {
dir = 'right';
} else if (rightEnd) {
dir = 'left';
} else {
dir = this.velocity < 0 ? 'right': 'left';
}
var closer = this.owl.closest(this.currX, dir);
this.owl.to(closer);
this.velocity = 0;
// this.owl.$stage.css('transform', 'translate3d(' + this.owl._coordinates[1] + 'px, 0, 0)');
}, this);
// take measurements
this.measure = $.proxy(function() {
this.knobSlide = this.$knobWrap.outerWidth();
this.bounds = {
left: this.knobSlide * -0.5,
right: this.knobSlide * 0.5
};
this.max = this.owl._coordinates[this.owl._coordinates.length - 2];
}, this);
this.getX = $.proxy(function() {
var matrix = this.owl.$stage.css('transform').replace(/[^0-9\-.,]/g, '').split(',');
var x = matrix[12] || matrix[4];
return parseInt(x);
}, this);
// register the event handlers
this.owl.$element.on(this._handlers);
// destroy
this.destroy = function() {
var handler, property;
$(window).off('owl.knob');
for (handler in this._handlers) {
this.owl.$element.off(handler, this._handlers[handler]);
}
for (property in Object.getOwnPropertyNames(this)) {
typeof this[property] != 'function' && (this[property] = null);
}
};
console.log(this.owl);
}
$.fn.owlCarousel.Constructor.Plugins.Knob = Knob;
})( window.Zepto || window.jQuery, window, document );