index.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { ref, computed } from 'vue'
  2. export function useModal(isShow: boolean, delay: number) {
  3. // 显示隐藏
  4. const visible = ref(isShow);
  5. // 动画类名
  6. const transitionClass = ref('');
  7. // 是否阻止鼠标事件
  8. const pointerEvents = ref(false);
  9. // 动画样式
  10. const transitionStyles = computed(() => ({
  11. transitionDuration: delay + 'ms',
  12. pointerEvents: pointerEvents.value ? 'none' : 'auto',
  13. }))
  14. // 动画切换
  15. const transition = (isShow: boolean, callback?: () => void) => {
  16. if (isShow) visible.value = true
  17. window.setTimeout(() => {
  18. transitionClass.value = isShow ? 'is-show' : 'is-hide';
  19. // 阻止鼠标事件,防止动画过程中触发事件
  20. pointerEvents.value = true;
  21. window.setTimeout(() => {
  22. pointerEvents.value = false;
  23. visible.value = isShow;
  24. callback && callback();
  25. }, delay);
  26. }, 20);
  27. }
  28. // 初始化
  29. if (isShow) {
  30. transition(true);
  31. }
  32. return {
  33. visible,
  34. transitionClass,
  35. transitionStyles,
  36. transition,
  37. }
  38. }