From f8b07136745a60c74c7397ed702dee5dd9a31109 Mon Sep 17 00:00:00 2001 From: Tyh2001 <1469442737@qq.com> Date: Fri, 16 Jun 2023 15:55:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20`f-collapse-animati?= =?UTF-8?q?on`=20=E7=BB=84=E4=BB=B6=E5=88=9D=E5=A7=8B=E5=B1=95=E5=BC=80?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=97=B6=E5=8A=A8=E7=94=BB=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + packages/fighting-design/_hooks/index.ts | 1 + .../_hooks/use-collapse-animation/index.ts | 126 +++++++++++++ .../src/collapse-animation.vue | 173 ++---------------- start/src/App.vue | 24 +-- 5 files changed, 144 insertions(+), 181 deletions(-) create mode 100644 packages/fighting-design/_hooks/use-collapse-animation/index.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 4686076bd0..eacac1178a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - 新增 `f-dialog` 组件 `mask-background` `mask-opacity` `show-header` 配置项 - 新增 `f-drawer` 组件 `mask-background` `mask-opacity` `show-header` 配置项 - 修复 `f-up-load` 继续多选重置之前文件的问题 +- 修复 `f-collapse-animation` 组件初始展开关闭时动画失效的问题 ## 0.41.0 (2023-06-04) diff --git a/packages/fighting-design/_hooks/index.ts b/packages/fighting-design/_hooks/index.ts index 2ec3632a5c..d070f21bd8 100644 --- a/packages/fighting-design/_hooks/index.ts +++ b/packages/fighting-design/_hooks/index.ts @@ -28,3 +28,4 @@ export * from './use-count-down' export * from './use-trigger' export * from './use-form-check' export * from './use-transition' +export * from './use-collapse-animation' diff --git a/packages/fighting-design/_hooks/use-collapse-animation/index.ts b/packages/fighting-design/_hooks/use-collapse-animation/index.ts new file mode 100644 index 0000000000..4104f8b471 --- /dev/null +++ b/packages/fighting-design/_hooks/use-collapse-animation/index.ts @@ -0,0 +1,126 @@ +import { computed } from 'vue' +import { isNumber } from '../../_utils' +import { useRun } from '..' +import type { CollapseAnimationProps } from '../../collapse-animation' + +export interface UseCollapseAnimationReturn { + after: (el: Element) => void + before: (el: Element) => void + ing: (el: Element) => void +} + +const { run } = useRun() + +export const useCollapseAnimation = (prop: CollapseAnimationProps): UseCollapseAnimationReturn => { + /** 动画样式 */ + const transitionStyle = computed((): string => { + if (isNumber(prop.animationTime)) { + return `${prop.animationTime}s all ease-in-out` + } + return '0.747s all ease-in-out' + }) + + /** + * 在动画开始之前,加点样式 + * + * @param { Object } el 元素节点 + */ + const before = (el: Element): void => { + const node = el as HTMLElement + + node.style.transition = transitionStyle.value + node.style.width = 'auto' + + if (prop.opened) { + node.style.height = '0' + + run(prop.onOpen, el) + + } else { + node.style.height = node.scrollHeight + 'px' + + if (prop.widthAnimation) { + /** 获取父节点 */ + const parent = node.parentElement as HTMLElement + /** + * 获取父节点的宽度 + * + * @see HTMLElement.offsetWidth https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/offsetWidth + * @see Element.clientWidth https://developer.mozilla.org/zh-CN/docs/Web/API/Element/clientWidth + */ + const parentWidth: number = parent.offsetWidth || parent.clientWidth + + node.style.width = `${parentWidth}px` + } + + run(prop.onClose, el) + } + } + + /** + * 在打开和关闭完成之后,移除样式 + * + * @param { Object } el 元素节点 + */ + const after = (el: Element): void => { + const node = el as HTMLElement + + node.style.transition = '' + node.style.height = '' + node.style.width = '' + + if (prop.opened) { + run(prop.onOpenEnd, el) + + } else { + run(prop.onCloseEnd, el) + } + } + + /** + * 运动过程中干点事儿 + * + * @param { Object } el 元素节点 + */ + const ing = (el: Element): void => { + const node = el as HTMLElement + + node.style.overflow = 'hidden' + + if (prop.opened) { + /** 开启执行 */ + node.style.height = node.scrollHeight + 'px' + + /** 如果需要宽度过度 */ + if (prop.widthAnimation) { + node.style.width = '0' + + /** 获取父节点 */ + const parent = node.parentElement as HTMLElement + /** + * 获取父节点的宽度 + * + * @see HTMLElement.offsetWidth https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/offsetWidth + * @see Element.clientWidth https://developer.mozilla.org/zh-CN/docs/Web/API/Element/clientWidth + */ + const parentWidth: number = parent.offsetWidth || parent.clientWidth + + /** 如果两个方法都没有获取到宽度,使用 auto */ + node.style.width = isNumber(parentWidth) ? `${parentWidth}px` : 'auto' + } + } else { + /** 关闭执行 */ + node.style.height = '0' + + if (prop.widthAnimation) { + node.style.width = '0' + } + } + } + + return { + after, + before, + ing + } +} diff --git a/packages/fighting-design/collapse-animation/src/collapse-animation.vue b/packages/fighting-design/collapse-animation/src/collapse-animation.vue index 26cc32223a..ad5efa8d24 100644 --- a/packages/fighting-design/collapse-animation/src/collapse-animation.vue +++ b/packages/fighting-design/collapse-animation/src/collapse-animation.vue @@ -1,27 +1,20 @@