Skip to content

useFullscreen

一个用于处理 dom 全屏的 Hook。

使用Demo

基础用法

vue
<template>
  <div class="hello">
    <div ref="fullScreen" style="background: white">
        <p>是否全屏: {{isFullscreen}}</p>
        <button @click="setFull" id="a">全屏</button>
        <button @click="exitFull">退出全屏</button>
        <button @click="toggle">toggle切换</button>
    </div>
  </div>
</template>

<script lang="ts">
import { useFullscreen } from "@dfsj/hooks";

export default {
  setup() {
    const [ isFullscreen, { setFull, exitFull, toggle } ] = useFullscreen();
    
    // useVirtualList测试
    return {
      isFullscreen,
      setFull,
      exitFull,
      toggle
    };
  }
};
</script>

没有传值默认为document.body

局部全屏

vue
<template>
  <div class="hello">
    <div ref="fullScreen" style="background: white">
        <p>是否全屏: {{isFullscreen}}</p>
        <button @click="setFull" id="a">全屏</button>
        <button @click="exitFull">退出全屏</button>
        <button @click="toggle">toggle切换</button>
    </div>
  </div>
</template>

<script lang="ts">
import { ref } from 'vue';
import { useFullscreen } from "../../../dist/index.js";

export default {
  setup() {
    const fullScreen = ref();
    const [ isFullscreen, { setFull, exitFull, toggle } ] = useFullscreen(fullScreen, {
        onFull: ()=>{
            console.log('全屏')
        },
        onExitFull: ()=>{
            console.log('非全屏')
        }
    });
    
    return {
      isFullscreen,
      setFull,
      exitFull,
      toggle,
      fullScreen
    };
  }
};
</script>

Ref传值为fullScreen的div标签,则只会此区域全屏

介绍

useFullscreen接受一个HTMLElement, 导出操作方法.

Api

interface Actions {
    setFull: () => void;
    exitFull: () => void;
    toggle: () => void;
}
const useFullscreen: (
  target?: Target$1 | undefined, 
  options?: Options | undefined
) => [
  isFullscreen: Ref<boolean>,
  actions: Actions
];

Params

参数说明类型默认值
target原生Dom或者被Ref嵌套的DomHTMLElement or Ref<HTMLElement> or (() => HTMLElement)document.body
options设置(可选)Options-

Options

参数说明类型
onExitFull监听退出全屏()=>void
onFull监听全屏()=>void

Result

参数说明类型
isFullscreen是否全屏boolean
setFull设置全屏()=>void
exitFull退出全屏()=>void
toggleFull切换全屏()=>void