/
background-loaded-animation.html
50 lines (47 loc) · 1.25 KB
/
background-loaded-animation.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
html, body {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
main#background {
width: 100%;
height: 100%;
opacity: 0;
background-size: cover;
animation-delay: 0s;
animation-duration: 1s;
animation-fill-mode: forwards;
animation-timing-function: ease;
}
@keyframes fadein {
0% { opacity: 0; }
100% { opacity: 1; }
}
</style>
</head>
<body>
<main id="background"></main>
<script>
addFadeInBackground('https://static.pexels.com/photos/300781/pexels-photo-300781.jpeg', 'background');
function addFadeInBackground(url, domId) {
var background = new Image();
background.src = url;
background.onload = function () {
console.log('Background load complete!');
var loadbackground = document.getElementById(domId);
loadbackground.style.backgroundImage = 'url(' + background.src + ')';
loadbackground.style.animationName = 'fadein';
}
}
</script>
</body>
</html>