-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
171 lines (159 loc) · 6.94 KB
/
index.html
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
165
166
167
168
169
170
171
<!doctype html>
<html lang="en" data-theme="light">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@1.4.4/css/pico.min.css">
<title>Alpine.js tab slider</title>
<style>
[x-cloak] {
display: none !important;
}
nav[role=tablist] {
justify-content: flex-start;
user-select: none;
border-bottom: 1px solid var(--muted-border-color);
margin-bottom: var(--spacing);
}
a[role=tab] {
color: var(--form-element-disabled-border-color);
border-bottom: 1px solid transparent;
transition: all var(--transition);
min-width: 120px;
text-align: center;
user-select: none;
}
a[role=tab].active {
color: var(--contrast);
border-radius: 0;
border-bottom: 1px solid var(--contrast);
}
a[role=tab]:focus {
background: transparent;
}
div.grid div {
min-height: 200px;
animation-duration: 0.45s;
-webkit-animation-duration: 0.45s;
animation-fill-mode: both;
-webkit-animation-fill-mode: both;
-webkit-animation-name: slideIn;
animation-name: slideIn;
}
@keyframes slideIn {
0% {
transform: translateY(1rem);
opacity: 0;
}
100% {
transform:translateY(0rem);
opacity: 1;
}
0% {
transform: translateY(1rem);
opacity: 0;
}
}
@-webkit-keyframes slideIn {
0% {
-webkit-transform: transform;
-webkit-opacity: 0;
}
100% {
-webkit-transform: translateY(0);
-webkit-opacity: 1;
}
0% {
-webkit-transform: translateY(1rem);
-webkit-opacity: 0;
}
}
</style>
</head>
<body>
<main class="container">
<section x-data="slider()">
<article x-ref="slider" data-interval="6000">
<nav role="tablist">
<ul>
<li><a href="#" role="tab">First</a></li>
<li><a href="#" role="tab">Second</a></li>
<li><a href="#" role="tab">Third</a></li>
<li><a href="#" role="tab">Fourth</a></li>
</ul>
</nav>
<div class="grid">
<div x-show="tab == 0" x-cloak>
<h4>First content</h4>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Totam quo odit veritatis distinctio mollitia error, delectus minima exercitationem vitae minus maiores sapiente praesentium ut velit impedit beatae commodi soluta magni!</p>
</div>
<div x-show="tab == 1" x-cloak>
<h4>Second content</h4>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Totam quo odit veritatis distinctio mollitia error, delectus minima exercitationem vitae minus maiores sapiente praesentium ut velit impedit beatae commodi soluta magni!</p>
</div>
<div x-show="tab == 2" x-cloak>
<h4>Third content</h4>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Totam quo odit veritatis distinctio mollitia error, delectus minima exercitationem vitae minus maiores sapiente praesentium ut velit impedit beatae commodi soluta magni!</p>
</div>
<div x-show="tab == 3" x-cloak>
<h4>Fourth content</h4>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Totam quo odit veritatis distinctio mollitia error, delectus minima exercitationem vitae minus maiores sapiente praesentium ut velit impedit beatae commodi soluta magni!</p>
</div>
</div>
</article>
</section>
</main>
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('slider', () => ({
// set initial tab
tab: 0,
// slider tabs
tabs: [...document.querySelectorAll('nav[role=tablist] a[role=tab]')],
init() {
// initialize main function
this.changeSlide()
},
// main function
changeSlide() {
let timeInterval = this.$refs.slider.dataset.interval;
this.tabs[this.tab].setAttribute('class', 'active')
// set interval to change slide
let startInterval = () => {
this.tab = (this.tab < this.tabs.length - 1)? this.tab + 1 : 0;
this.tabs.forEach( (tab)=> {
(this.tab == this.tabs.indexOf(tab)) ? tab.setAttribute('class', 'active') : tab.removeAttribute('class')
})
}
// start interval to change slide
let slideInterval = setInterval(startInterval, timeInterval);
// mouse over slider stops slide
this.$refs.slider.onmouseover = () => {
if (slideInterval) {
clearInterval(slideInterval)
slideInterval = null;
}
}
// mouse out slider starts again slide
this.$refs.slider.onmouseout = () => {
if (slideInterval === null) {
slideInterval = setInterval(startInterval, timeInterval);
}
}
// slider tabs click event
this.tabs.forEach( (tab)=> {
tab.addEventListener('click', (e)=> {
e.preventDefault()
this.tab = this.tabs.indexOf(e.target)
this.tabs.forEach( (tab)=> {
(this.tab == this.tabs.indexOf(tab)) ? tab.setAttribute('class', 'active') : tab.removeAttribute('class')
})
})
})
}
}))
})
</script>
</body>
</html>