-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy path17-stored-bookmarker.html
201 lines (176 loc) Β· 5.06 KB
/
17-stored-bookmarker.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Getting Started with JavaScript</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
body, .jumbotron { padding: 30px; }
body {
background-image: url('img/meadow.jpeg');
background-size: cover;
}
.content {
font-size: 19px;
line-height: 1.5;
max-width: 500px;
margin: 0 auto;
position: relative;
border-radius: 5px;
}
.floater {
background: #FFF;
transition: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.4) all;
margin-bottom: 30px;
z-index: 11;
}
.floater input[type=text] {
padding: 30px 20px;
border: none;
box-shadow: none;
font-size: 14px;
transition: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.4) all;
}
.floater-bottom {
padding: 6px 10px 8px;
text-align: right;
border-top: 1px solid #DDD;
}
.floater-bottom button {
padding: 4px 8px;
font-size: 10px;
}
.overlay {
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.80);
width: 100%;
height: 100%;
opacity: 0;
transition: 0.3s ease opacity;
z-index: 10;
pointer-events: none;
}
body.show-floater .floater {
transform: scale(1.1);
}
body.show-floater .floater input[type=text] {
padding: 50px 20px;
font-size: 18px;
}
body.show-floater .overlay {
opacity: 1;
pointer-events: all;
}
/* ==================== */
.bookmarks-list {
position: relative;
z-index: 9;
}
.bookmark {
display: block;
background: #FFF;
color: #999;
padding: 20px;
transition: 0.3s ease all;
border-bottom: 1px solid #DDD;
font-size: 16px;
}
.bookmark:hover {
color: #1CE;
text-decoration: none;
}
.bookmark:first-child {
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.bookmark:last-child {
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
border-bottom: 0;
}
</style>
</head>
<body>
<div class="overlay"></div>
<div class="floater content">
<form class="bookmark-form">
<div class="floater-top">
<input type="text" class="form-control" placeholder="What's on your mind?">
</div>
<div class="floater-bottom">
<button type="submit" class="btn btn-primary btn-sm">Add</button>
</div>
</form>
</div>
<div class="bookmarks-list content"></div>
<!-- π₯π₯π₯π₯ start javascript π₯π₯π₯π₯ -->
<script>
const body = document.body;
const input = document.querySelector('input[type=text]');
const overlay = document.querySelector('.overlay');
function showFloater() {
body.classList.add('show-floater');
}
function closeFloater() {
if (body.classList.contains('show-floater')) {
body.classList.remove('show-floater');
}
}
input.addEventListener('focusin', showFloater);
input.addEventListener('focusout', closeFloater);
overlay.addEventListener('click', closeFloater);
// =========================
const bookmarksList = document.querySelector('.bookmarks-list');
const bookmarkForm = document.querySelector('.bookmark-form');
const bookmarkInput = bookmarkForm.querySelector('input[type=text]');
const bookmarks = JSON.parse(localStorage.getItem('bookmarks')) || [];
fillBookmarksList(bookmarks);
function createBookmark(e) {
e.preventDefault();
// add a new bookmark to the bookmarks
const title = bookmarkInput.value;
const bookmark = {
title: title
};
bookmarks.push(bookmark);
fillBookmarksList(bookmarks);
storeBookmarks(bookmarks);
bookmarkForm.reset();
console.table(bookmarks);
// save that bookmarks list to localStorage
// const title = bookmarkInput.value;
// const bookmark = document.createElement('a');
// bookmark.className = 'bookmark';
// bookmark.innerText = title;
// bookmark.href = '#';
// bookmark.target = '_blank';
// bookmarksList.appendChild(bookmark);
}
function fillBookmarksList(bookmarks = []) {
const bookmarksHtml = bookmarks.map((bookmark) => {
return `
<a href="#" class="bookmark">
${bookmark.title}
</a>
`;
}).join('');
bookmarksList.innerHTML = bookmarksHtml;
// let bookmarksHtml = '';
// for (let i = 0; i < bookmarks.length; i++) {
// bookmarksHtml += `
// <a href="#" class="bookmark">
// ${bookmarks[i].title}
// </a>
// `;
// }
// console.log(bookmarksHtml);
}
function storeBookmarks(bookmarks = []) {
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
bookmarkForm.addEventListener('submit', createBookmark);
</script>
</body>
</html>