[wip] Add reducing side margin effect on Photos when scrolling

This commit is contained in:
2021-10-12 22:12:24 +02:00
parent 56c523679b
commit db8f984739
3 changed files with 77 additions and 8 deletions

View File

@@ -7,6 +7,7 @@
import { quartOut } from 'svelte/easing'
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import { clamp, lerp } from '$utils/functions'
// Components
import Metas from '$components/Metas.svelte'
import IconEarth from '$components/atoms/IconEarth.svelte'
@@ -28,6 +29,7 @@
let buttonReset: HTMLElement
let buttonShuffle: HTMLElement
let scrollY: number
// Filters
const urlFiltersParams = new URLSearchParams()
@@ -38,7 +40,6 @@
$: filtered = filterCountry !== defaultCountry || filterSort !== defaultSort
$: latestPhoto = photos[0]
// Pages related informations
let currentPage: number = 1
let ended: boolean
@@ -46,6 +47,14 @@
$: currentPhotosAmount = photos.length
$: ended = currentPhotosAmount === totalPhotos
// Container margins
let scrollProgress: number
let sideMargins: number = 6
$: if (browser && window.innerWidth >= 768) {
scrollProgress = clamp(scrollY / (window.innerHeight * 1.5), 0, 1) // TODO: Define this value to be 1 past 1.5 viewport
sideMargins = lerp(8, 30, scrollProgress)
}
/**
* Define URL params
@@ -152,6 +161,9 @@
image=""
/>
<svelte:window bind:scrollY />
<main class="photos">
<section class="photos__intro">
<h1 class="title-huge">Houses</h1>
@@ -225,16 +237,16 @@
Reset
</button>
{/if}
<button class="shuffle" bind:this={buttonShuffle}>
<!-- <button class="shuffle" bind:this={buttonShuffle}>
<svg width="14" height="14" viewBox="0 0 14 14" fill="#3C0576" xmlns="http://www.w3.org/2000/svg" aria-label="Shuffle icon">
<path fill-rule="evenodd" clip-rule="evenodd" d="M10.8168 13.7928C10.565 13.5166 10.565 13.0688 10.8168 12.7925L11.6371 11.8927C10.0729 11.7978 8.9174 11.2494 8.00077 10.4379C7.01752 9.56747 6.3426 8.41893 5.73606 7.382L5.72202 7.358C5.10114 6.29653 4.5499 5.35411 3.79106 4.65375C3.05738 3.97661 2.10408 3.50731 0.644748 3.50731C0.288663 3.50731 0 3.19063 0 2.8C0 2.40936 0.288666 2.09269 0.644751 2.09269C2.39889 2.0927 3.64837 2.6734 4.62121 3.57126C5.53188 4.41176 6.17567 5.5133 6.76119 6.51514L6.82133 6.61801C7.44316 7.68108 8.01476 8.63254 8.81058 9.33707C9.55258 9.99396 10.5204 10.4595 11.9721 10.491L10.8168 9.22362C10.565 8.9474 10.565 8.49956 10.8168 8.22333C11.0686 7.94711 11.4768 7.94711 11.7286 8.22333L13.8112 10.5079C14.0629 10.7842 14.0629 11.232 13.8112 11.5082L11.7286 13.7928C11.4768 14.0691 11.0686 14.0691 10.8168 13.7928ZM10.8168 5.77667C10.565 5.50045 10.565 5.0526 10.8168 4.77638L11.972 3.50905C10.5204 3.54053 9.55258 4.00608 8.81058 4.66297C8.50789 4.93094 8.23764 5.23463 7.98472 5.5666C7.73665 5.18234 7.48128 4.77156 7.2176 4.37967C7.45664 4.09011 7.71575 3.81442 8.00077 3.5621C8.9174 2.75061 10.0729 2.2022 11.6371 2.10738L10.8168 1.20745C10.565 0.931231 10.565 0.483387 10.8168 0.207166C11.0686 -0.0690553 11.4768 -0.0690553 11.7286 0.207166L13.8112 2.49177C14.0629 2.768 14.0629 3.21584 13.8112 3.49206L11.7286 5.77667C11.4768 6.05289 11.0686 6.05289 10.8168 5.77667ZM0 11.2C0 11.5907 0.288666 11.9073 0.644751 11.9073C2.39889 11.9073 3.64837 11.3266 4.62121 10.4288C4.82778 10.2381 5.02061 10.034 5.20215 9.82074C5.03927 9.5548 4.88613 9.293 4.73943 9.0422L4.73621 9.0367C4.647 8.88418 4.56006 8.73561 4.47456 8.59108C4.26231 8.86556 4.03764 9.11871 3.79106 9.34629C3.05738 10.0234 2.10408 10.4927 0.644748 10.4927C0.288663 10.4927 0 10.8094 0 11.2Z" />
</svg>
</button>
</button> -->
</div>
</div>
</section>
<section class="photos__content">
<section class="photos__content" style="--margin-sides: {sideMargins}px;">
<div class="grid container">
{#if photos.length}
<div class="photos__grid">

View File

@@ -195,14 +195,36 @@
// Content Block
&__content {
background-color: $color-tertiary;
// margin: 0 12px;
--corners: 8px;
position: relative;
padding: 20px 0;
border-radius: 6px;
background-color: $color-tertiary;
@include bp (sm) {
margin: 0 30px;
padding: 64px 0;
border-radius: 8px;
}
&:before, &:after {
content: "";
display: block;
position: absolute;
z-index: -1;
top: 0;
width: 25%;
height: 100%;
background: $color-tertiary;
transition: transform 0.8s var(--ease-quart);
}
&:before {
border-radius: var(--corners) 0 0 var(--corners);
left: 0;
transform: translate3d(calc(-1 * var(--margin-sides)), 0, 0);
}
&:after {
border-radius: 0 var(--corners) var(--corners) 0;
right: 0;
transform: translate3d(var(--margin-sides), 0, 0);
}
}

View File

@@ -1,3 +1,29 @@
/**
* Throttle function
*/
export const throttle = (func: Function, timeout: number) => {
let ready: boolean = true
return (...args: any) => {
if (!ready) return
ready = false
func(...args)
setTimeout(() => ready = true, timeout)
}
}
/**
* Debounce function
*/
export const debounce = (func: Function, timeout: number) => {
let timer: NodeJS.Timeout
return (...args: any) => {
clearTimeout(timer)
timer = setTimeout(() => func(...args), timeout)
}
}
/**
* Linear Interpolation
*/
@@ -6,6 +32,15 @@ export const lerp = (start: number, end: number, amt: number): number => {
}
/**
* Clamp a number
*/
export const clamp = (num: number, a: number, b: number) => {
return Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b))
}
/**
* Return a random element from an array
*/