screenfull全屏、退出全屏、指定元素全屏的使用步骤

文章目录

1.下载插件
建议下载指定版本5.1.0,不然可能有一个报错

复制代码
npm install --save screenfull@5.1.0

2.页面引入

复制代码
import screenfull from "screenfull"

页面全屏

3.在标签上绑定点击事件

复制代码
<div @click="handleFull">全屏/退出全屏</div>

4.在methods中写方法代码

复制代码
handleFull() {
  if (screenfull.isEnabled) {
    // 加个判断浏览器是否支持全屏
    screenfull.toggle(); // 切换全屏状态
  } else {
    console.log("您的浏览器不支持全屏");
  }
}

页面全屏

5.在标签上绑定点击事件

复制代码
<div id="demo" @click="container">指定元素全屏</div>

6.在methods中写方法代码

复制代码
container() {
  const element = document.getElementById("demo"); // 指定全屏元素
  if (screenfull.isEnabled) {
    screenfull.request(element);
    this.elementFull = !this.elementFull; // 判断状态 决定是全屏还是退出全屏
    if (this.elementFull) {
      screenfull.toggle(); //全屏/退出全屏切换
    }
  }
}

完整代码

复制代码
<template>
  <div class="parentBox">
    <div @click="handleFull" class="btn">全屏/退出全屏</div>
    <div id="demo" @click="container">指定元素全屏</div>
  </div>
</template>
<script>
import screenfull from "screenfull";
export default {
  data() {
    return {
      elementFull: false,
    };
  },
  methods: {
    handleFull() {
      if (screenfull.isEnabled) {
        // 加个判断浏览器是否支持全屏
        screenfull.toggle(); // 切换全屏状态
      } else {
        console.log("您的浏览器不支持全屏");
      }
    },
    container() {
      const element = document.getElementById("demo"); // 指定全屏元素
      if (screenfull.isEnabled) {
        screenfull.request(element);
        this.elementFull = !this.elementFull; // 判断状态 决定是全屏还是退出全屏
        if (this.elementFull) {
          screenfull.toggle(); //全屏/退出全屏切换
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.btn {
  background-color: gray;
  width: 200px;
}
#demo {
  background-color: aqua;
  width: 200px;
  height: 200px;
}
</style>
相关推荐
胖咕噜的稞达鸭1 天前
二叉树进阶面试题:最小栈 栈的压入·弹出序列 二叉树层序遍历
开发语言·c++
我的写法有点潮1 天前
彻底理解 JavaScript 的深浅拷贝
前端·javascript·vue.js
wzg20161 天前
pyqt5 简易入门教程
开发语言·数据库·qt
心静财富之门1 天前
【无标题】标签单击事件
开发语言·php
草莓熊Lotso1 天前
揭开 C++ vector 底层面纱:从三指针模型到手写完整实现
开发语言·c++
小秋学嵌入式-不读研版1 天前
C56-字符串拷贝函数strcpy与strnpy
c语言·开发语言·笔记
hui函数1 天前
python全栈(基础篇)——day04:后端内容(字符编码+list与tuple+条件判断+实战演示+每日一题)
开发语言·数据结构·python·全栈
Never_Satisfied1 天前
在JavaScript / HTML中,转移字符导致js生成的html出错
开发语言·javascript·html
羚羊角uou1 天前
【Linux】POSIX信号量、环形队列、基于环形队列实现生产者消费者模型
java·开发语言
知识分享小能手1 天前
微信小程序入门学习教程,从入门到精通,WXS语法详解(10)
前端·javascript·学习·微信小程序·小程序·vue·团队开发