forked from mahonnaise/cyclotron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.cyclotron.js
More file actions
57 lines (54 loc) · 1.2 KB
/
jquery.cyclotron.js
File metadata and controls
57 lines (54 loc) · 1.2 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(function ($) {
$.fn.cyclotron = function (options) {
var settings = $.extend({
dampingFactor: 0.93,
historySize: 5
}, options);
return this.each(function () {
var container, sx, dx = 0, armed, offset = 0, tick, prev, h = [];
container = $(this);
container.mousedown(function (e) {
sx = e.pageX - offset;
armed = true;
e.preventDefault();
});
container.mousemove(function (e) {
var px;
if (armed) {
px = e.pageX;
if (prev === undefined) {
prev = px;
}
offset = px - sx;
if (h.length > settings.historySize) {
h.shift();
}
h.push(prev - px);
container.css('background-position', offset);
prev = px;
}
});
container.bind('mouseleave mouseup', function () {
if (armed) {
var i, len = h.length, v = h[len - 1];
for (i = 0; i < len; i++) {
v = (v * len + (h[i])) / (len + 1);
}
dx = v;
}
armed = false;
});
tick = function () {
if (!armed && dx) {
dx *= settings.dampingFactor;
offset -= dx;
container.css('background-position', offset);
if (Math.abs(dx) < 0.001) {
dx = 0;
}
}
};
setInterval(tick, 16);
});
};
}(jQuery));