use-collapse.js 803 B

12345678910111213141516171819202122232425262728293031
  1. import { ref } from 'vue';
  2. // 自定义 Hook 用于过渡效果
  3. export function useCollapse(initialMaxHeight = 500) {
  4. const collapseMaxHeight = ref(initialMaxHeight); // 固定的最大高度
  5. // 过渡钩子 - 在元素开始进入时
  6. const beforeEnter = (el) => {
  7. el.style.maxHeight = 0;
  8. };
  9. // 过渡钩子 - 进入时
  10. const enter = (el, done) => {
  11. el.offsetHeight; // 强制重排
  12. el.style.transition = 'max-height 0.5s ease';
  13. el.style.maxHeight = `${collapseMaxHeight.value}px`; // 使用固定的最大高度
  14. done();
  15. };
  16. // 过渡钩子 - 离开时
  17. const leave = (el, done) => {
  18. el.style.transition = 'max-height 0.5s ease';
  19. el.style.maxHeight = 0; // 收起时的最小高度
  20. done();
  21. };
  22. return {
  23. beforeEnter,
  24. enter,
  25. leave
  26. };
  27. }