-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
36 lines (32 loc) · 845 Bytes
/
script.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
function setBackground(url) {
const backgroundEl = document.querySelector('.background');
backgroundEl.style.backgroundImage = `url('${url}')`;
}
function onFileSelect(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.addEventListener('load', () => {
try {
localStorage.backgroundData = reader.result;
setBackground(localStorage.backgroundData);
} catch (err) {
alert('Image size must be smaller than 5MB');
}
});
reader.readAsDataURL(file);
}
function listenPick() {
const picker = document.querySelector('.picker');
picker.addEventListener('change', (e)=> {
onFileSelect(e);
});
}
function init() {
listenPick();
if(localStorage.backgroundData) {
setBackground(localStorage.backgroundData);
} else {
setBackground('default.png');
}
}
init();