1: ScreenFull principle and scheme analysis
Screenfull (full screen) function implementation
Principle:
For ScreenFull, the browser already provides an API for screenFull, which you can view here. The API provides two main methods:
- Document.exitfullscreen () : This method is used to request a switch from full-screen mode to window mode
Element.requestFullscreen()
: This method is used to request the browser (User Agent) to put a particular element (and even its descendants) in full-screen mode- For example, we can pass
document.getElementById('app').requestFullscreen()
In obtainingid=app
的DOM
After that, set the area to full screen
- For example, we can pass
But usually we don’t use the API directly for full-screen effects, instead we use its wrapper, ScreenFull
Solution:
So clear good principle, the next implementation of the scheme is relatively easy.
The overall scheme is divided into two steps:
- encapsulation
screenfull
component- Display switch button
- Implement switching function based on ScreenFull
- Importing the component
2: Implementation: screenfull
Once we have a clear plan, we will implement the plan
encapsulationscreenfull
Components:
-
Download the dependency package ScreenFull
NPM I [email protected]Copy the code
-
Create components/Screenfull/index
<template> <div> <el-button @click="onToggle" >Click {{isFullscreen? "exit-fullscreen" : "fullscreen" }}</el-button ></div> </template> <script setup> import { ref, onMounted, onUnmounted } from "vue"; import screenfull from "screenfull"; // Full screen const isFullscreen = ref(false); // Listen for changes const change = () = > { isFullscreen.value = screenfull.isFullscreen; }; // Switch events const onToggle = () = > { screenfull.toggle(); }; // Set the listener onMounted(() = > { screenfull.on("change", change); }); // Delete the listener onUnmounted(() = > { screenfull.off("change", change); }); </script> <style lang="scss" scoped></style> Copy the code
Importing the component
<screenfull class="right-menu-item hover-effect" /> import Screenfull from "@/components/Screenfull"; Copy the code