/
index.js
67 lines (58 loc) · 1.89 KB
/
index.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
(function() {
var Carousel, domready;
domready = require('domready');
module.exports = Carousel = (function() {
function Carousel(selector) {
var _this = this;
this.selector = selector;
if (!this.selector) {
console.error("Please specify a CSS selector when creating a new Carousel, e.g. new Carousel('#my-carousel')");
}
domready(function() {
_this.slide_count = document.querySelectorAll("" + _this.selector + " > *").length;
document.body.addEventListener('keydown', function(event) {
var _ref, _ref1;
if ((_ref = event.keyCode) === 39 || _ref === 40 || _ref === 74 || _ref === 32) {
event.preventDefault();
_this.next();
}
if ((_ref1 = event.keyCode) === 37 || _ref1 === 38 || _ref1 === 75) {
event.preventDefault();
return _this.prev();
}
});
return _this.getOffset();
});
}
Carousel.prototype.getOffset = function() {
this.offset = parseInt(document.body.scrollTop / window.innerHeight);
if ((document.body.scrollTop % window.innerHeight) > window.innerHeight / 2) {
return this.offset++;
}
};
Carousel.prototype.next = function() {
this.getOffset();
if (this.offset !== this.slide_count - 1) {
this.offset++;
}
return this.animate();
};
Carousel.prototype.prev = function() {
this.getOffset();
if (this.offset !== 0) {
this.offset--;
}
return this.animate();
};
Carousel.prototype.animate = function() {
if (typeof jQuery !== "undefined") {
return $("html, body").animate({
scrollTop: window.innerHeight * this.offset
});
} else {
return document.body.scrollTop = window.innerHeight * this.offset;
}
};
return Carousel;
})();
}).call(this);