Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use position sticky #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions Scroller.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
let left;
let sections;
let wh = 0;
let fixed;
let offset_top = 0;
let width = 1;
let height;
Expand All @@ -96,14 +95,10 @@
$: (top, bottom, threshold, parallax, update());

$: style = `
position: ${fixed ? 'fixed' : 'absolute'};
top: 0;
transform: translate(0, ${offset_top}px);
top: ${offset_top || top_px}px;
z-index: ${inverted ? 3 : 1};
`;

$: widthStyle = fixed ? `width:${width}px;` : '';

onMount(() => {
sections = foreground.querySelectorAll(query);
count = sections.length;
Expand Down Expand Up @@ -136,19 +131,8 @@
const available_space = bottom_px - top_px;
progress = (top_px - fg.top) / (foreground_height - available_space);

if (progress <= 0) {
offset_top = 0;
fixed = false;
} else if (progress >= 1) {
offset_top = parallax
? (foreground_height - background_height)
: (foreground_height - available_space);
fixed = false;
} else {
offset_top = parallax ?
Math.round(top_px - progress * (background_height - available_space)) :
top_px;
fixed = true;
if (parallax) {
offset_top = Math.round(top_px - progress * (background_height - available_space));
}

for (let i = 0; i < sections.length; i++) {
Expand All @@ -170,8 +154,8 @@
<svelte:window bind:innerHeight={wh}/>

<svelte-scroller-outer bind:this={outer}>
<svelte-scroller-background-container class='background-container' style="{style}{widthStyle}">
<svelte-scroller-background bind:this={background}>
<svelte-scroller-background-container class='background-container'>
<svelte-scroller-background bind:this={background} style="{style}">
<slot name="background"></slot>
</svelte-scroller-background>
</svelte-scroller-background-container>
Expand All @@ -189,7 +173,8 @@

svelte-scroller-background {
display: block;
position: relative;
position: sticky;
top: 0;
width: 100%;
}

Expand All @@ -208,7 +193,9 @@
svelte-scroller-background-container {
display: block;
position: absolute;
top: 0;
width: 100%;
height: 100%;
max-width: 100%;
pointer-events: none;
/* height: 100%; */
Expand Down