1
1
<script setup lang="ts">
2
2
import { useScroll } from ' @vueuse/core'
3
- import { computed , shallowRef , toRefs , useTemplateRef } from ' vue'
3
+ import { computed , nextTick , shallowRef , toRefs , useTemplateRef } from ' vue'
4
4
5
5
const el = useTemplateRef <HTMLElement >(' el' )
6
6
const smooth = shallowRef (false )
7
7
const behavior = computed (() => smooth .value ? ' smooth' : ' auto' )
8
- const { x, y, isScrolling, arrivedState, directions } = useScroll (el , { behavior })
8
+ const { x, y, isScrolling, arrivedState, directions, measure } = useScroll (el , { behavior })
9
9
const { left, right, top, bottom } = toRefs (arrivedState )
10
10
const { left : toLeft, right : toRight, top : toTop, bottom : toBottom } = toRefs (directions )
11
11
@@ -27,12 +27,20 @@ const displayY = computed({
27
27
y .value = Number .parseFloat (val )
28
28
},
29
29
})
30
+
31
+ const height = shallowRef <' h-[500px]' | ' h-[200px]' >(' h-[500px]' )
32
+ function updateScrollPosition() {
33
+ height .value = height .value === ' h-[500px]' ? ' h-[200px]' : ' h-[500px]'
34
+ nextTick (() => {
35
+ measure ()
36
+ })
37
+ }
30
38
</script >
31
39
32
40
<template >
33
41
<div class =" flex" >
34
42
<div ref =" el" class =" w-300px h-300px m-auto overflow-scroll bg-gray-500/5 rounded" >
35
- <div class =" w-500px h-400px relative" >
43
+ <div class =" w-500px relative" :class = " height " >
36
44
<div position =" absolute left-0 top-0" bg =" gray-500/5" p =" x-2 y-1" >
37
45
TopLeft
38
46
</div >
@@ -64,7 +72,13 @@ const displayY = computed({
64
72
<input v-model =" displayY" type =" number" min =" 0" max =" 100" step =" 10" class =" w-full !min-w-0" >
65
73
</div >
66
74
</div >
67
- <label for =" smooth-scrolling-option" text =" right" opacity =" 75" >Smooth scrolling</label >
75
+ <div class =" col-span-full flex items-center justify-between" >
76
+ Measure
77
+ <button @click =" updateScrollPosition" >
78
+ Toggle height
79
+ </button >
80
+ </div >
81
+ <label for =" smooth-scrolling-option" class =" whitespace-nowrap" text =" right" opacity =" 75" >Smooth scrolling</label >
68
82
<span ><input id =" smooth-scrolling-option" v-model =" smooth" type =" checkbox" ></span >
69
83
<span text =" right" opacity =" 75" >isScrolling</span >
70
84
<BooleanDisplay :value =" isScrolling" />
0 commit comments