-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Scroll vertically * Scroll container * ScrollContainer works * Scroll smooth
- Loading branch information
Showing
2 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters