Skip to content

Commit

Permalink
Snap container
Browse files Browse the repository at this point in the history
  • Loading branch information
prokawsar committed Oct 3, 2024
1 parent ee7ff03 commit 03f97e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
33 changes: 15 additions & 18 deletions src/lib/components/elements/ScrollContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
export let maxHeight = '380px'
let containerRef: HTMLDivElement
let contentRef: HTMLDivElement
let isScrolling = false
let isVisible = false
let observer: IntersectionObserver
Expand All @@ -13,25 +12,23 @@
if (!isVisible || isScrolling) return
isScrolling = true
const container = containerRef
const { scrollTop, scrollHeight, clientHeight } = container
const { scrollTop, scrollHeight, clientHeight } = containerRef
const isAtTop = scrollTop === 0
const isAtBottom = contentRef.clientHeight === Math.floor(scrollTop + clientHeight)
const scrollDiv = () => {
const isAtBottom = scrollHeight === Math.floor(scrollTop + clientHeight)
const scrollDiv = (offset: number) => {
event.preventDefault()
event.stopPropagation()
container.scrollTop += event.deltaY * 3.8
containerRef.scrollTop += offset
}
const offset = 20 * Math.max(event.deltaY, 10)
if (event.deltaY > 0) {
// Scrolling down
if (!isAtBottom) {
scrollDiv()
}
if (!isAtBottom) scrollDiv(offset)
} else if (event.deltaY < 0) {
// Scrolling up
if (!isAtTop) {
scrollDiv()
}
if (!isAtTop) scrollDiv(-offset)
}
isScrolling = false
Expand Down Expand Up @@ -65,12 +62,12 @@

<div
bind:this={containerRef}
class="flex w-full flex-col overflow-y-auto scroll-smooth rounded-xl lg:h-[317px] xl:h-full"
class="flex w-full snap-y snap-mandatory flex-col gap-2 overflow-y-auto scroll-smooth rounded-xl lg:h-[317px] xl:h-full"
style="max-height: {maxHeight};"
>
<div bind:this={contentRef} class="flex flex-col gap-2">
<slot />
</div>
<!-- <div bind:this={contentRef} class=" flex snap-y snap-mandatory flex-col gap-2 overflow-y-auto"> -->
<slot />
<!-- </div> -->
</div>

<style>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/sections/Hero.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<ScrollContainer>
{#each slides as { color, text, animation }, i}
<div
class="flex w-full flex-col justify-between rounded-xl lg:flex-row"
class="flex w-full snap-center flex-col justify-between rounded-xl lg:flex-row"
style="background-color: {color}"
>
<p class="mt-10 pl-5 text-center text-3xl font-medium lg:text-start">{text}</p>
Expand Down

0 comments on commit 03f97e2

Please sign in to comment.