Skip to content

Commit

Permalink
Scroll behave fix (#1)
Browse files Browse the repository at this point in the history
* Scroll vertically

* Scroll container

* ScrollContainer works

* Scroll smooth
  • Loading branch information
prokawsar authored Sep 22, 2024
1 parent 7537015 commit bf4d910
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
80 changes: 80 additions & 0 deletions src/lib/components/elements/ScrollContainer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<script lang="ts">
import { onMount } from 'svelte'
export let maxHeight = '380px'
let containerRef: HTMLDivElement
let contentRef: HTMLDivElement
let isScrolling = false
let isVisible = false
let observer: IntersectionObserver
function handleScroll(event: WheelEvent) {
if (!isVisible || isScrolling) return
isScrolling = true
const container = containerRef
const { scrollTop, scrollHeight, clientHeight } = container
const isAtTop = scrollTop === 0
const isAtBottom = contentRef.clientHeight === Math.floor(scrollTop + clientHeight)
const scrollDiv = () => {
event.preventDefault()
event.stopPropagation()
container.scrollTop += event.deltaY * 3.8
}
if (event.deltaY > 0) {
// Scrolling down
if (!isAtBottom) {
scrollDiv()
}
} else if (event.deltaY < 0) {
// Scrolling up
if (!isAtTop) {
scrollDiv()
}
}
isScrolling = false
}
function handleIntersection(entries: any) {
isVisible = entries[0].isIntersecting
}
onMount(() => {
window.addEventListener('wheel', handleScroll, { passive: false })
observer = new IntersectionObserver(handleIntersection, {
root: null,
rootMargin: '-100px',
threshold: 1.0
})
if (containerRef) {
observer.observe(containerRef)
}
return () => {
window.removeEventListener('wheel', handleScroll)
if (observer) {
observer.disconnect()
}
}
})
</script>

<div
bind:this={containerRef}
class="flex w-full flex-col overflow-y-auto scroll-smooth rounded-xl"
style="max-height: {maxHeight};"
>
<div bind:this={contentRef} class="flex flex-col gap-2">
<slot />
</div>
</div>

<style>
div::-webkit-scrollbar {
display: none;
}
</style>
12 changes: 8 additions & 4 deletions src/lib/components/sections/Hero.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import { base } from '$app/paths'
import Cta from '$lib/components/elements/CTA.svelte'
import Carousel from 'svelte-carousel'
import { browser } from '$app/environment'
import Icon from '@iconify/svelte'
import ScrollContainer from '$lib/components/elements/ScrollContainer.svelte'
const slides = [
{
Expand Down Expand Up @@ -105,9 +105,9 @@
</div>
</div>

<div class="mx-auto flex w-full max-w-7xl flex-row rounded-xl sm:px-8 lg:px-0">
<div class="mx-auto flex w-full max-w-7xl flex-row sm:px-8 lg:px-0">
{#if browser}
<Carousel dots={false} arrows={false} autoplay autoplayDuration={5000}>
<ScrollContainer>
{#each slides as { color, text, animation }, i}
<div
class="flex w-full flex-col justify-between rounded-xl lg:flex-row"
Expand All @@ -123,7 +123,7 @@
</div>
</div>
{/each}
</Carousel>
</ScrollContainer>
{/if}
</div>
</div>
Expand All @@ -133,4 +133,8 @@
filter: brightness(105%);
-webkit-filter: brightness(105%);
}
::-webkit-scrollbar {
display: none;
}
</style>

0 comments on commit bf4d910

Please sign in to comment.