@@ -45,9 +45,10 @@ const touch = useTouch()
4545const props = defineProps (floatingPanelProps )
4646const emit = defineEmits ([' update:height' , ' height-change' ])
4747
48+ const heightValue = ref <number >(props .height )
49+
4850const DAMP = 0.2 // 阻尼系数
4951let startY: number // 起始位置
50- const height = ref <number >(props .height ) // 当前高度
5152const windowHeight = ref <number >(0 )
5253const dragging = ref <boolean >(false ) // 是否正在拖拽
5354
@@ -61,17 +62,22 @@ const anchors = computed(() => (props.anchors.length >= 2 ? props.anchors : [bou
6162const rootStyle = computed (() => {
6263 const style: CSSProperties = {
6364 height: addUnit (boundary .value .max ),
64- transform: ` translateY(calc(100% + ${addUnit (- height .value )})) ` ,
65+ transform: ` translateY(calc(100% + ${addUnit (- heightValue .value )})) ` ,
6566 transition: ! dragging .value ? ` transform ${props .duration }ms cubic-bezier(0.18, 0.89, 0.32, 1.28) ` : ' none'
6667 }
6768
6869 return ` ${objToStyle (style )};${props .customStyle } `
6970})
7071
72+ const updateHeight = (value : number ) => {
73+ heightValue .value = value
74+ emit (' update:height' , value )
75+ }
76+
7177const handleTouchStart = (event : TouchEvent ) => {
7278 touch .touchStart (event )
7379 dragging .value = true
74- startY = - height .value
80+ startY = - heightValue .value
7581}
7682
7783const handleTouchMove = (event : TouchEvent ) => {
@@ -81,15 +87,15 @@ const handleTouchMove = (event: TouchEvent) => {
8187 }
8288 touch .touchMove (event )
8389 const moveY = touch .deltaY .value + startY
84- height . value = - ease (moveY )
90+ updateHeight ( - ease (moveY ) )
8591}
8692
8793const handleTouchEnd = () => {
8894 dragging .value = false
89- height . value = closest (anchors .value , height .value )
95+ updateHeight ( closest (anchors .value , heightValue .value ) )
9096
91- if (height .value !== - startY ) {
92- emit (' height-change' , { height: height .value })
97+ if (heightValue .value !== - startY ) {
98+ emit (' height-change' , { height: heightValue .value })
9399 }
94100}
95101
@@ -109,16 +115,16 @@ const ease = (y: number) => {
109115}
110116
111117watch (
112- () => height . value ,
118+ () => props . height ,
113119 (value ) => {
114- emit ( ' update:height ' , value )
120+ heightValue . value = value
115121 }
116122)
117123
118124watch (
119125 boundary ,
120126 () => {
121- height . value = closest (anchors .value , height .value )
127+ updateHeight ( closest (anchors .value , heightValue .value ) )
122128 },
123129 { immediate: true }
124130)
0 commit comments