Repository navigation
Expand file tree
/
Copy pathcarousel.html
More file actions
164 lines (156 loc) · 4.29 KB
/
Copy pathcarousel.html
File metadata and controls
164 lines (156 loc) · 4.29 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
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
<!DOCTYPE HTML>
<html>
<head>
<title>Carousel</title>
<style>
html, body {
height: 100%;
}
body {
margin: 0;
background: #656476;
background: -webkit-linear-gradient(top, #000, #656476);
background: -moz-linear-gradient(top, #000, #656476);
background: -ms-linear-gradient(top, #000, #656476);
}
#carousel{
width: 100%;
height: 100%;
position: relative;
}
#panels {
padding: 0;
margin: 0;
list-style-type:none;
width: 100%;
height: 310px;
position: absolute;
top: 50%;
margin-top: -150px;
overflow: hidden;
}
.panel {
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
background: #0065c0;
background: -webkit-linear-gradient(top, #0065c0, #0a4593);
background: -moz-linear-gradient(top, #0065c0, #0a4593);
background: -ms-linear-gradient(top, #0065c0, #0a4593);
box-shadow: 0 0 10px #000;
-webkit-box-shadow: 0 0 10px #000;
-moz-box-shadow: 0 0 10px #000;
width: 200px;
height: 200px;
cursor: pointer;
text-align: center;
color: #fff;
display: block;
}
.current {
width: 300px;
height: 300px;
position: absolute;
left: 50%;
margin-left: -150px;
}
.prev{
position: absolute;
left: -100px;
}
.next{
position: absolute;
right: -100px;
}
.prev, .next {
margin-top: 40px;
}
.hidden {
display: none;
}
</style>
</head>
<body onload="carousel.init()">
<div id="carousel">
<ul id="panels">
<li class="panel">0</li>
<li class="panel">1</li>
<li class="panel">2</li>
<li class="panel">3</li>
<li class="panel">4</li>
<li class="panel">5</li>
<li class="panel">6</li>
<li class="panel">7</li>
<li class="panel">8</li>
<li class="panel">9</li>
</ul>
</div>
<script type="text/javascript">
var carousel = {
// Array to keep track of the panels
panels: document.getElementsByClassName('panel'),
// Current index
current: 0,
init: function(){
document.addEventListener('keyup', carousel.keyControl);
carousel.resetPanels();
},
resetPanels: function(){
// Hide all the panels.
var panels = carousel.panels;
for(var i = 0; i < panels.length; i++){
panels[i].classList.add('hidden');
panels[i].classList.remove('current');
panels[i].classList.remove('next');
panels[i].classList.remove('prev');
panels[i].removeEventListener('click', carousel.goToNextSlide);
panels[i].removeEventListener('click', carousel.goToPreviousSlide);
}
// Fix current index if it is out of bounds.
if(carousel.current < 0){
carousel.current = panels.length - 1;
}
else if(carousel.current > panels.length - 1){
carousel.current = 0
}
// Expose current panel.
panels[carousel.current].classList.add('current');
panels[carousel.current].classList.remove('hidden');
// Fix/expose next panel.
var nextIndex = carousel.current + 1;
if(nextIndex > panels.length - 1){
nextIndex = 0;
}
panels[nextIndex].classList.add('next');
panels[nextIndex].classList.remove('hidden');
// Fix/expose previous panel.
var prevIndex = carousel.current - 1;
if(prevIndex < 0){
prevIndex = panels.length - 1;
}
panels[prevIndex].classList.add('prev');
panels[prevIndex].classList.remove('hidden');
// Add new event listeners.
document.getElementsByClassName('next')[0].addEventListener('click', carousel.goToNextSlide);
document.getElementsByClassName('prev')[0].addEventListener('click', carousel.goToPreviousSlide);
},
keyControl: function(event){
if(event.keyCode == 37){ // Left arrow
carousel.goToPreviousSlide();
}
else if(event.keyCode == 39){ // Right arrow
carousel.goToNextSlide();
}
},
goToNextSlide: function(){
carousel.current++;
carousel.resetPanels();
},
goToPreviousSlide: function(){
carousel.current--;
carousel.resetPanels();
}
};
</script>
</body>
</html>