-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponsiveBackground.svelte
66 lines (60 loc) · 1.12 KB
/
ResponsiveBackground.svelte
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
<!--
Can be used like this
<ResponsiveBackground
{images}
{fallbackImage}>
some optional html
</ResponsiveBackground>
where images = [
{ media, type, srcset }
]
and where fallbackImage = {
src,
alt,
}
-->
<script>
export let images;
export let fallbackImage;
const {src, alt} = fallbackImage;
let curSrc;
const updateBg = ev => {
const img = ev.target;
var src = typeof img.currentSrc !== "undefined" ? img.currentSrc : img.src;
if (curSrc !== src) {
curSrc = `url(${src})`;
}
};
</script>
<div style="background-image: {curSrc};">
<picture>
{#each images as {media, srcset, type}}
<source
{media}
{srcset}
{type} />
{/each}
<img
on:load={updateBg}
{src}
{alt} />
</picture>
<slot></slot>
</div>
<style>
picture,
picture img {
visibility: hidden;
width: 0;
height: 0;
overflow: hidden;
}
div {
overflow: hidden;
position: relative;
display: block;
background-position: center;
background-size: cover;
background-color: #c87f3a;
}
</style>