当前位置: 代码网 > it编程>编程语言>Javascript > JS实现页面指定区域全屏阅读功能

JS实现页面指定区域全屏阅读功能

2024年06月11日 Javascript 我要评论
这里用到vueuse中的usefullscreen(vueuse中提供了许多封装好的函数可以直接使用,极大提高了开发效率)详见vueuse官方文档:home | vueuse实现流程首先需要在项目中安

这里用到vueuse中的usefullscreen(vueuse中提供了许多封装好的函数可以直接使用,极大提高了开发效率)

详见vueuse官方文档:

home | vueuse

实现流程

首先需要在项目中安装vueuse

npm i @vueuse/core

在需要用到的页面中引入usefullscreen

import { usefullscreen } from '@vueuse/core'

使用(这里用的vue3)

将需要全屏展示的元素用mainele标记,作为入参传入usefullscreen,获取全屏展示用到的数据

isfullscreen:布尔类型的值,用来判断当前是否是全屏状态

toggle:调用toggle函数实现全屏和非全屏的切换

const mainele = ref<htmlelement | null>(null);
const isfullscreentext = ref("全屏阅读");
const { isfullscreen, enter, exit, toggle } = usefullscreen(mainele);
const changefullscreen = () => {
  toggle();
  if (isfullscreen.value) {
    isfullscreentext.value = "全屏阅读";
  } else {
    isfullscreentext.value = "退出全屏";
  }
};

完整代码

<template>
  <div>
    <h1 style="text-align: center;">全屏阅读测试</h1>
    <div ref="mainele">
      <el-button type="primary" @click="changefullscreen">{{
        isfullscreentext
      }}</el-button>
      <div style="width: 100%; height: 90vh; background-color: antiquewhite">
        内容内容内容内容内容
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { usefullscreen } from "@vueuse/core";
const mainele = ref<htmlelement | null>(null);
const isfullscreentext = ref("全屏阅读");
const { isfullscreen, enter, exit, toggle } = usefullscreen(mainele);
const changefullscreen = () => {
  toggle();
  if (isfullscreen.value) {
    isfullscreentext.value = "全屏阅读";
  } else {
    isfullscreentext.value = "退出全屏";
  }
};
</script>

到此这篇关于js实现页面指定区域全屏阅读功能的文章就介绍到这了,更多相关js指定区域全屏阅读内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com