-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackstretch.js
68 lines (68 loc) · 1.92 KB
/
backstretch.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
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
async function backstretch(asset) {
const checkBackstretchFile = astr => {
const xhr = new XMLHttpRequest()
return new Promise((resolve, reject) => {
xhr.onreadystatechange = (e) => {
if (xhr.readyState !== 4) {
return
}
if (xhr.status === 200) {
resolve(true)
} else {
resolve(false)
}
}
xhr.open('GET', astr)
xhr.send()
})
}
const createStretch = image => {
const style = styleString()
return `
<div style="${style.wrapper}">
<img style="${style.image}" src="${image}" />
</div>
`
}
const styleString = () => {
const styles = {
wrapper: {
"left": "0",
"top": "0",
"overflow": "hidden",
"margin": "0",
"padding": "0",
"height": "100vh",
"width": "100%",
"z-index": "-999999",
"position": "fixed"
},
image: {
"position": "absolute",
"margin": "0",
"padding": "0",
"border": "none",
"width": "100%",
"height": "100vh",
"max-width": "none",
"z-index": "-999999",
"left": "0",
"top": "0",
"object-fit": "cover"
}
}
let strStyle = {
wrapper: '',
image: ''
}
for (let key in styles.wrapper) { strStyle.wrapper += key + ":" + styles.wrapper[key] + ";" }
for (let key in styles.image) { strStyle.image += key + ": " + styles.image[key] + "; " }
return strStyle
}
const body = document.querySelector('body')
const checkImage = await checkBackstretchFile(asset)
if (!body) return console.error("I didn't find any element body, please check your element 😘")
if (!asset) return console.error("I didn't find the asset you applied, ensure asset applied from root directory 😉")
if (!checkImage) return console.error("asset not found 🥺")
return body.innerHTML += createStretch(asset)
}