-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmateCarousel.js
154 lines (135 loc) · 5.19 KB
/
mateCarousel.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
/*jslint browser:true */
/* jshint loopfunc:true */
var mateCarousel = function (innerClass, objConfig) {
if (objConfig === undefined) { objConfig = {}; }
var config = {
bulletContainerId: objConfig.bulletContainerId || "menuContainer",
bulletMenuClass: objConfig.bulletMenuClass,
bulletContent: objConfig.bulletContent || "•",
autoplay: objConfig.autoplay || false,
autoplaySpeed: objConfig.autoplaySpeed,
buttonBeforeContent: objConfig.buttonBeforeContent || "←",
buttonAfterContent: objConfig.buttonAfterContent || "→",
buttonBeforeId: objConfig.buttonBeforeId,
buttonAfterId: objConfig.buttonAfterId,
initialPosition: objConfig.initialPosition || 0,
};
var bulletMenuChildren = document.getElementsByClassName(config.bulletMenuClass);
var innerDiv = document.getElementsByClassName(innerClass);
var repeater;
var actualPosition;
var createButtons = function(before, after, beforeId, afterId){
var beforeContent = document.createTextNode(before);
var beforeNode = document.createElement("button");
beforeNode.appendChild(beforeContent);
beforeNode.setAttribute("id", beforeId);
beforeNode.onclick = function(){
setPos(actualPosition -1);
};
var afterContent = document.createTextNode(after);
var afterNode = document.createElement("button");
afterNode.appendChild(afterContent);
afterNode.setAttribute("id", afterId);
afterNode.onclick = function(){
setPos(actualPosition +1);
};
innerDiv[0].parentNode.appendChild(beforeNode);
innerDiv[0].parentNode.appendChild(afterNode);
};
var setActiveBullet = function () {
for (var i = 0; i < bulletMenuChildren.length; i++) {
if (i === actualPosition) {
bulletMenuChildren[i].classList.add("active");
} else {
bulletMenuChildren[i].classList.remove("active");
}
}
};
var createMenuElements = function(){
if (document.getElementById(config.bulletContainerId)){
var menu = document.getElementById(config.bulletContainerId);
innerDiv[0].parentNode.removeChild(menu);
}
var menuContainer = document.createElement("ul");
menuContainer.setAttribute("id", config.bulletContainerId);
innerDiv[0].parentNode.appendChild(menuContainer);
for (var i=0; i<innerDiv.length; i++){
var node = document.createElement("li");
var textnode = document.createTextNode(config.bulletContent || "");
node.appendChild(textnode);
node.classList.add(config.bulletMenuClass);
node.dataset.index = i;
node.onclick = function(){
setPos(parseInt(this.dataset.index));
};
menuContainer.appendChild(node);
}
};
var setPos = function (mov) {
if (mov == innerDiv.length) {
mov = 0;
} else if (mov == -1) {
mov = innerDiv.length - 1;
}
for (var i = 0; i < innerDiv.length; i++) {
innerDiv[i].style.transform = "translateX(" + 100 * (i - mov) + "%)";
if (innerDiv[i].style.transform == "translateX(0%)"){
actualPosition = i;
}
}
actualPosition = mov;
setActiveBullet();
};
var startRepeat = function(ms){
repeater = setInterval(function(){
setPos(actualPosition + 1);
}, ms);
};
var removeMenu = function(){
var bulletMenuParent = document.getElementById(config.bulletContainerId);
innerDiv[0].parentNode.removeChild(bulletMenuParent);
};
var lateCreateMenu = function(bulletMenuClass, bulletContainerId, bulletCont){
config.bulletContainerId = bulletContainerId || "menuContainer";
config.bulletMenuClass = bulletMenuClass;
config.bulletContent = bulletCont || "•";
createMenuElements();
};
if (config.bulletMenuClass){
createMenuElements();
}
setPos(config.initialPosition);
if (config.autoplay){
startRepeat(config.autoplaySpeed);
}
if (config.buttonBeforeId && config.buttonAfterId){
createButtons (config.buttonBeforeContent, config.buttonAfterContent, config.buttonBeforeId, config.buttonAfterId);
}
return {
goToPosition: function (n) {
setPos(n);
},
next: function () {
setPos(actualPosition + 1);
},
before: function () {
setPos(actualPosition - 1);
},
play: function(ms){
startRepeat(ms);
},
pause: function(){
clearInterval(repeater);
},
getIndex: function(){return actualPosition;},
config: function(){
return config;
},
removeMenu: function(){
removeMenu();
},
lateCreateMenu: function(bulletMenuClass, bulletContainerId, bulletCont){
lateCreateMenu(bulletMenuClass, bulletContainerId, bulletCont);
}
};
};