useFullscreen.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * 全屏功能组合式函数
  3. * 提供全屏状态管理和相关工具函数
  4. * 适用于所有应用组件
  5. */
  6. import { ref, computed, inject } from 'vue';
  7. /**
  8. * 使用全屏功能
  9. * @param {Object} options - 配置选项
  10. * @param {number} options.headerHeight - Header高度,默认64px
  11. * @returns {Object} 全屏相关的响应式数据和方法
  12. *
  13. * @example
  14. * // 在组件中使用
  15. * const { isFullscreen, containerHeight, fullscreenClass } = useFullscreen();
  16. *
  17. * // 在模板中使用
  18. * <div :class="fullscreenClass" :style="{ height: containerHeight }">
  19. * ...
  20. * </div>
  21. */
  22. export function useFullscreen(options = {}) {
  23. const { headerHeight = 64 } = options;
  24. // 从父组件获取全屏状态
  25. const isFullscreen = inject('isFullscreen', ref(false));
  26. // 计算容器高度
  27. const containerHeight = computed(() => {
  28. return isFullscreen.value ? '100vh' : `calc(100vh - ${headerHeight}px)`;
  29. });
  30. // 计算全屏样式类
  31. const fullscreenClass = computed(() => {
  32. return isFullscreen.value ? 'app-fullscreen' : '';
  33. });
  34. // 计算容器高度值(用于JavaScript计算)
  35. const containerHeightValue = computed(() => {
  36. return isFullscreen.value ? window.innerHeight : window.innerHeight - headerHeight;
  37. });
  38. /**
  39. * 获取容器高度(用于Canvas等需要数值的场景)
  40. * @returns {number} 容器高度值
  41. */
  42. const getContainerHeight = () => {
  43. return isFullscreen.value ? window.innerHeight : window.innerHeight - headerHeight;
  44. };
  45. /**
  46. * 获取容器宽度
  47. * @returns {number} 容器宽度值
  48. */
  49. const getContainerWidth = () => {
  50. return window.innerWidth;
  51. };
  52. return {
  53. // 响应式状态
  54. isFullscreen,
  55. containerHeight,
  56. containerHeightValue,
  57. fullscreenClass,
  58. // 方法
  59. getContainerHeight,
  60. getContainerWidth
  61. };
  62. }