-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
209 lines (197 loc) · 7.75 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./dist/main.css">
<title>Animate loading demo</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body.show-drawer-button .load-btn.outside {
display: none;
}
body.show-drawer-button #open-drawer {
display: flex;
}
</style>
</head>
<body class="relative">
<section class="text-gray-600 body-font min-h-screen pb-20">
<div class="container px-2 md:px-5 py-5 md:py-16 mx-auto">
<div class="flex flex-wrap w-full mb-8 md:mb-20 flex-col items-center text-center">
<h1 class="sm:text-3xl text-2xl font-medium title-font mb-2 text-gray-900">Animate Loading Bar</h1>
<p class="lg:w-1/2 w-full leading-relaxed text-gray-500">
A tiny loading bar like Shopify, Github, JSFiddle... that works!
</p>
<div class="mt-4 md:mt-8 flex items-center">
<span class="mr-4">Loading type:</span>
<select name="loading-type" id="loading-type" class="border border-gray-300 p-2 rounded pr-4">
<option value="basic">Basic</option>
<option value="with-overlay">With an overlay</option>
<option value="drawer">Inside a drawer</option>
</select>
</div>
</div>
<div id="container">
<div class="flex flex-wrap md:-m-4">
<div id="card-template" class="hidden w-full md:w-1/2 xl:w-1/4 p-2 md:p-4">
<div class="bg-gray-100 rounded-lg overflow-hidden flex flex-row-reverse w-full md:block">
<div class="w-1/3 md:w-full flex-shrink-0">
<img class="aspect-square h-full w-full md:h-48 object-cover object-center" src="https://picsum.photos/id/100/350/200"
alt="content">
</div>
<div class="p-6 w-full">
<h3 class="tracking-widest text-indigo-500 text-xs font-medium title-font">SUBTITLE</h3>
<h2 class="img-title text-lg text-gray-900 font-medium title-font mb-4">Chichen Itza</h2>
<p class="leading-relaxed text-base hidden md:block">Fingerstache flexitarian street art 8-bit waistcoat. Distillery
hexagon disrupt edison bulbche.</p>
</div>
</div>
</div>
</div>
<button
class="load-btn outside flex mx-auto mt-8 md:mt-16 text-white border-0 py-2 px-8 focus:outline-none rounded text-lg bg-gradient-to-r from-green-400 to-blue-500 hover:from-pink-500 hover:to-yellow-500">Load
something</button>
<button id="open-drawer"
class="hidden mx-auto mt-16 text-white bg-blue-500 border-0 py-2 px-8 focus:outline-none hover:bg-blue-600 rounded text-lg">Open drawer</button>
</div>
</div>
</section>
<footer class="fixed bottom-0 inset-x-0">
<div class="bg-gray-200">
<div class="container mx-auto py-4 px-5 flex flex-wrap flex-col sm:flex-row">
<p class="text-gray-500 text-sm text-center sm:text-left">© 2022 made by Leo @
<a href="https://leohuynh.dev" rel="noopener noreferrer" class="text-gray-600 ml-1" target="_blank">
https://leohuynh.dev
</a>
</p>
<span class="text-sm inline-flex sm:ml-auto sm:mt-0 mt-2 justify-center sm:justify-start">
<a href="https://twitter.com/hta218_" rel="noopener noreferrer" class="text-gray-600 ml-1" target="_blank">
@twitter
</a>
<a href="https://github.com/hta218" rel="noopener noreferrer" class="text-gray-600 ml-1" target="_blank">
@github
</a>
</span>
</div>
</div>
</footer>
<section class="hidden fixed inset-0 bg-black transition-all duration-300" style="--tw-bg-opacity: 0;">
<div id="drawer"
class="transition-transform duration-300 translate-x-full w-96 h-full bg-white ml-auto py-4 px-2 flex flex-col justify-between relative"
style="contain: content; transition: translateX(100%);">
<div class="drawer-container mt-8">
<div id="drawer-card-template" class="m-4">
<div class="bg-gray-100 flex">
<img class="h-24 w-1/3 object-cover object-center" src="https://picsum.photos/id/100/350/200" alt="content">
<div class="p-4 w-2/3">
<p class="tracking-widest text-indigo-500 text-xs font-medium title-font">Subtitle</p>
<p class="img-title text-gray-900 font-medium title-font mb-4">Chichen Itza</p>
</div>
</div>
</div>
</div>
<button id="close-drawer" class="text-2xl absolute right-0 top-0 py-2 px-4 cursor-pointer">x</button>
<button
class="load-btn justify-end flex mx-auto mt-16 text-white border-0 py-2 px-8 focus:outline-none rounded text-lg bg-gradient-to-r from-green-400 to-blue-500 hover:from-pink-500 hover:to-yellow-500">Load
something</button>
</div>
</section>
<script src="./dist/main.js"></script>
<script>
const AnimateLoading = window['animate-loading'].default;
const loadingData = [
{
type: 'basic',
nodes: {
cardContainer: document.getElementById('container'),
cardTemplate: document.getElementById('card-template'),
}
},
{
type: 'with-overlay',
nodes: {
loadingOverlay: document.getElementById('container'),
cardContainer: document.getElementById('container'),
cardTemplate: document.getElementById('card-template')
}
},
{
type: 'drawer',
nodes: {
loadingTarget: document.getElementById('drawer'),
cardContainer: document.getElementById('drawer'),
cardTemplate: document.getElementById('drawer-card-template')
}
},
]
let loading, cardContainer, cardTemplate
const initLoading = (_type = 'basic') => {
if (_type === 'drawer') {
document.body.classList.add('show-drawer-button')
} else {
document.body.classList.remove('show-drawer-button')
}
const {
nodes: { loadingTarget, cardContainer: _container, cardTemplate: _template, loadingOverlay: overlay }
} = loadingData.find(({ type }) => type === _type)
cardContainer = _container
cardTemplate = _template
loading = new AnimateLoading(loadingTarget, { overlay })
}
initLoading()
document.body.addEventListener('click', (e) => {
if (e.target.classList.contains('load-btn')) {
loading.start()
const id = Math.floor(Math.random() * 100);
fetch(`https://picsum.photos/id/${id}/info`)
.then(res => res.json())
.then(async data => {
const { author, download_url } = data
const newCard = cardTemplate.cloneNode(true)
newCard.querySelector('.img-title').textContent = author
await loadImage(download_url)
newCard.querySelector('img').src = download_url
newCard.classList.remove('hidden')
cardContainer.firstElementChild.appendChild(newCard)
loading.finish()
})
.catch(err => {
console.error(err)
loading.finish()
})
}
})
document
.getElementById('loading-type')
.addEventListener("change", e => initLoading(e.target.value))
const drawerOpenBtn = document.getElementById('open-drawer')
const drawerCloseBtn = document.getElementById('close-drawer')
const drawer = document.getElementById('drawer')
const drawerWrapper = drawer.parentElement
const closeDrawer = () => {
drawer.style.transform = "translateX(100%)"
drawerWrapper.style.setProperty("--tw-bg-opacity", "0")
setTimeout(() => drawerWrapper.style.display = 'none', 300)
}
drawerOpenBtn.addEventListener('click', () => {
drawerWrapper.style.display = 'block'
window.requestAnimationFrame(() => {
drawerWrapper.style.setProperty("--tw-bg-opacity", "0.5")
drawer.style.transform = "translateX(0)"
})
})
drawerCloseBtn.addEventListener("click", closeDrawer)
drawerWrapper.addEventListener("click", (e) => (e.target === drawerWrapper) && closeDrawer())
const loadImage = src => {
return new Promise((resolve, reject) => {
const img = document.createElement('img')
img.src = src
img.onload = resolve
img.onerror = reject
})
}
</script>
</body>
</html>