Skip to content

Commit a258358

Browse files
committed
add smooth scroll to demo
1 parent cc3273a commit a258358

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@vueuse/motion": "^3.0.3",
2424
"@wdns/vue-code-block": "^2.3.5",
2525
"gsap": "^3.13.0",
26+
"lenis": "^1.3.8",
2627
"matter-js": "^0.20.0",
2728
"motion-v": "^1.5.0",
2829
"ogl": "^1.0.11",

src/demo/Components/GlassSurfaceDemo.vue

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<TabbedLayout>
33
<template #preview>
4-
<div class="relative overflow-y-auto no-scrollbar demo-container">
4+
<div class="relative overflow-y-auto no-scrollbar demo-container" ref="scrollContainerRef">
55
<GlassSurface
66
:key="key"
77
:width="360"
@@ -22,26 +22,21 @@
2222
/>
2323

2424
<div class="absolute flex flex-col items-center gap-6 top-0 left-0 right-0">
25-
<div
26-
class="absolute translate-y-1/2 top-12 text-4xl font-bold text-[#27FF64] z-0 whitespace-nowrap text-center"
27-
>
25+
<div class="absolute translate-y-1/2 top-12 text-4xl font-bold text-[#333] z-0 whitespace-nowrap text-center">
2826
Try scrolling.
2927
</div>
3028

31-
<!-- Top Spacer -->
3229
<div class="h-60 w-full" />
3330

34-
<!-- Image Blocks -->
35-
<div v-for="(item, index) in imageBlocks" :key="index" class="relative">
31+
<div v-for="(item, index) in imageBlocks" :key="index" class="relative py-4">
3632
<img :src="item.src" class="w-128 rounded-2xl object-cover grayscale-100" />
3733
<div
38-
class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 font-extrabold text-center leading-[100%] text-[3rem] min-w-72"
34+
class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 font-extrabold text-center leading-[100%] text-[3rem] min-w-72 mix-blend-overlay text-white"
3935
>
4036
{{ item.text }}
4137
</div>
4238
</div>
4339

44-
<!-- Bottom Spacer -->
4540
<div class="h-60 w-full" />
4641
</div>
4742
</div>
@@ -75,7 +70,8 @@
7570
</template>
7671

7772
<script setup lang="ts">
78-
import { ref, watch } from 'vue';
73+
import { ref, watch, onMounted, onUnmounted, useTemplateRef } from 'vue';
74+
import Lenis from 'lenis';
7975
import TabbedLayout from '../../components/common/TabbedLayout.vue';
8076
import PropTable from '../../components/common/PropTable.vue';
8177
import CliInstallation from '../../components/code/CliInstallation.vue';
@@ -88,6 +84,10 @@ import { useForceRerender } from '@/composables/useForceRerender';
8884
8985
const { rerenderKey: key, forceRerender } = useForceRerender();
9086
87+
const scrollContainerRef = useTemplateRef<HTMLElement>('scrollContainerRef');
88+
let lenis: Lenis | null = null;
89+
let rafId: number | null = null;
90+
9191
const borderRadius = ref(50);
9292
const backgroundOpacity = ref(0.1);
9393
const saturation = ref(1);
@@ -118,6 +118,49 @@ watch(
118118
}
119119
);
120120
121+
const initLenis = () => {
122+
if (!scrollContainerRef.value) return;
123+
124+
lenis = new Lenis({
125+
wrapper: scrollContainerRef.value,
126+
content: scrollContainerRef.value.firstElementChild as HTMLElement,
127+
duration: 1.2,
128+
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
129+
orientation: 'vertical',
130+
gestureOrientation: 'vertical',
131+
smoothWheel: true,
132+
wheelMultiplier: 1,
133+
touchMultiplier: 2,
134+
infinite: false
135+
});
136+
137+
const raf = (time: number) => {
138+
lenis?.raf(time);
139+
rafId = requestAnimationFrame(raf);
140+
};
141+
142+
rafId = requestAnimationFrame(raf);
143+
};
144+
145+
const destroyLenis = () => {
146+
if (rafId) {
147+
cancelAnimationFrame(rafId);
148+
rafId = null;
149+
}
150+
if (lenis) {
151+
lenis.destroy();
152+
lenis = null;
153+
}
154+
};
155+
156+
onMounted(() => {
157+
initLenis();
158+
});
159+
160+
onUnmounted(() => {
161+
destroyLenis();
162+
});
163+
121164
const imageBlocks = [
122165
{
123166
src: 'https://images.unsplash.com/photo-1500673587002-1d2548cfba1b?q=80&w=1740&auto=format&fit=crop',

0 commit comments

Comments
 (0)