-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
132 lines (106 loc) · 3.05 KB
/
main.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
(function() {
function init(item) {
var items = item.querySelectorAll('li'),
current = 0,
autoUpdate = true,
timeTrans = 4000;
//create nav
var nav = document.createElement('nav');
nav.className = 'nav_arrows';
//create button prev
var prevbtn = document.createElement('button');
prevbtn.className = 'prev';
prevbtn.setAttribute('aria-label', 'Prev');
//create button next
var nextbtn = document.createElement('button');
nextbtn.className = 'next';
nextbtn.setAttribute('aria-label', 'Next');
//create counter
var counter = document.createElement('div');
counter.className = 'counter';
counter.innerHTML = "<span>1</span><span>"+items.length+"</span>";
if (items.length > 1) {
nav.appendChild(prevbtn);
nav.appendChild(counter);
nav.appendChild(nextbtn);
item.appendChild(nav);
}
items[current].className = "current";
if (items.length > 1) items[items.length-1].className = "prev_slide";
var navigate = function(dir) {
items[current].className = "";
if (dir === 'right') {
current = current < items.length-1 ? current + 1 : 0;
} else {
current = current > 0 ? current - 1 : items.length-1;
}
var nextCurrent = current < items.length-1 ? current + 1 : 0,
prevCurrent = current > 0 ? current - 1 : items.length-1;
items[current].className = "current";
items[prevCurrent].className = "prev_slide";
items[nextCurrent].className = "";
//update counter
counter.firstChild.textContent = current + 1;
}
item.addEventListener('mouseenter', function() {
autoUpdate = false;
});
item.addEventListener('mouseleave', function() {
autoUpdate = true;
});
setInterval(function() {
if (autoUpdate) navigate('right');
},timeTrans);
prevbtn.addEventListener('click', function() {
navigate('left');
});
nextbtn.addEventListener('click', function() {
navigate('right');
});
//keyboard navigation
document.addEventListener('keydown', function(ev) {
var keyCode = ev.keyCode || ev.which;
switch (keyCode) {
case 37:
navigate('left');
break;
case 39:
navigate('right');
break;
}
});
// swipe navigation
// from http://stackoverflow.com/a/23230280
item.addEventListener('touchstart', handleTouchStart, false);
item.addEventListener('touchmove', handleTouchMove, false);
var xDown = null;
var yDown = null;
function handleTouchStart(evt) {
xDown = evt.touches[0].clientX;
yDown = evt.touches[0].clientY;
};
function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
/* left swipe */
navigate('right');
} else {
navigate('left');
}
}
/* reset values */
xDown = null;
yDown = null;
};
}
[].slice.call(document.querySelectorAll('.cd-slider')).forEach( function(item) {
init(item);
});
})();