0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00
svelte/documentation/examples/17-special-elements/04-svelte-window-bindings/App.svelte

87 lines
1.5 KiB
Svelte
Raw Normal View History

2019-03-10 20:53:01 +01:00
<script>
const layers = [0, 1, 2, 3, 4, 5, 6, 7, 8];
let y;
</script>
2023-05-14 07:50:43 +02:00
<svelte:window bind:scrollY={y} />
2019-03-10 20:53:01 +01:00
<a class="parallax-container" href="https://www.firewatchgame.com">
{#each layers as layer}
2019-03-10 20:53:01 +01:00
<img
2023-05-14 07:50:43 +02:00
style="transform: translate(0,{(-y * layer) / (layers.length - 1)}px)"
2019-03-10 20:53:01 +01:00
src="https://www.firewatchgame.com/images/parallax/parallax{layer}.png"
alt="parallax layer {layer}"
2023-05-14 07:50:43 +02:00
/>
2019-03-10 20:53:01 +01:00
{/each}
</a>
<div class="text">
2023-05-14 07:50:43 +02:00
<span style="opacity: {1 - Math.max(0, y / 40)}"> scroll down </span>
2019-03-10 20:53:01 +01:00
<div class="foreground">
You have scrolled {y} pixels
</div>
</div>
<style>
.parallax-container {
position: fixed;
width: 2400px;
height: 712px;
left: 50%;
2023-05-14 07:50:43 +02:00
transform: translate(-50%, 0);
2019-03-10 20:53:01 +01:00
}
.parallax-container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
will-change: transform;
}
.parallax-container img:last-child::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
2023-05-14 07:50:43 +02:00
background: rgb(45, 10, 13);
2019-03-10 20:53:01 +01:00
}
.text {
position: relative;
width: 100%;
height: 300vh;
2023-05-14 07:50:43 +02:00
color: rgb(220, 113, 43);
2019-03-10 20:53:01 +01:00
text-align: center;
padding: 4em 0.5em 0.5em 0.5em;
box-sizing: border-box;
pointer-events: none;
}
span {
display: block;
font-size: 1em;
text-transform: uppercase;
will-change: transform, opacity;
}
.foreground {
position: absolute;
top: 711px;
left: 0;
width: 100%;
height: calc(100% - 712px);
2023-05-14 07:50:43 +02:00
background-color: rgb(32, 0, 1);
2019-03-10 20:53:01 +01:00
color: white;
padding: 50vh 0 0 0;
}
2019-03-11 00:39:14 +01:00
:global(body) {
margin: 0;
padding: 0;
background-color: rgb(253, 174, 51);
}
</style>