当前位置:   article > 正文

vue3关于大屏的放大缩小功能_vue3.0大屏

vue3.0大屏

效果:

放大前放大后:

1.首先创建一个文件夹,在此文件夹下新建autoScreen.js,plugins.js两个文件; 如:

autoScreen.js文件里的内容:

  1. export default (_this) => {
  2. ((_this) => {
  3. // vue 对象
  4. // 动态获取vue组件实例
  5. const appRef = _this
  6. // * 默认缩放值
  7. const scale = {
  8. width: '1',
  9. height: '1'
  10. }
  11. // * 设计稿尺寸(px)
  12. const baseWidth = 1920
  13. const baseHeight = 1080
  14. // * 需保持的比例(默认1.77778)
  15. const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
  16. // 重新设置缩放比例值
  17. const calcRate = () => {
  18. // 当前视口的宽高比例
  19. const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
  20. if (appRef) {
  21. // 获取到的当前实例
  22. if (currentRate > baseProportion) {
  23. // 表示更宽
  24. scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
  25. scale.height = (window.innerHeight / baseHeight).toFixed(5)
  26. appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%) translate3d(0,0,0)`
  27. } else {
  28. // 表示更高
  29. scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
  30. scale.width = (window.innerWidth / baseWidth).toFixed(5)
  31. appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%) translate3d(0,0,0)`
  32. }
  33. }
  34. }
  35. const resize = () => {
  36. setTimeout(() => {
  37. calcRate()
  38. }, 200)
  39. }
  40. // 改变窗口大小重新绘制
  41. const windowDraw = () => {
  42. window.addEventListener('resize', resize)
  43. }
  44. windowDraw()
  45. calcRate()
  46. })(_this)
  47. /**
  48. * @参数说明
  49. * @_this 当前vue实例对象,用于获取当前的显示页面
  50. * @scrrenRef vue2 中获取操作页面的根页面。页面的ref节点
  51. * **/
  52. }

plugins.js文件里的内容:

  1. //调用各个浏览器提供的全屏方法
  2. export function enterFullScreen() { //进入全屏
  3. let de = document.documentElement;
  4. if (de.requestFullscreen) {
  5. de.requestFullscreen();
  6. } else if (de.mozRequestFullScreen) {
  7. de.mozRequestFullScreen();
  8. } else if (de.webkitRequestFullScreen) {
  9. de.webkitRequestFullScreen();
  10. } else if (de.msRequestFullscreen) {
  11. de.msRequestFullscreen();
  12. }
  13. console.log('已全屏')
  14. }
  15. //调用各个浏览器提供的退出全屏方法
  16. export function exitFullscreen() {
  17. if(document.exitFullscreen) {
  18. document.exitFullscreen();
  19. }else if(document.mozCancelFullScreen) {
  20. document.mozCancelFullScreen();
  21. }else if(document.webkitExitFullscreen) {
  22. document.webkitExitFullscreen();
  23. }else if (document.msExitFullscreen) {
  24. document.msExitFullscreen();
  25. }
  26. console.log('已还原')
  27. }

2.在App.vue里引入autoScreen.js文件,并且设置一下样式

  1. <template>
  2. <div id="screenRef" class="screen-container">
  3. <!--动态看板内容-->
  4. <router-view></router-view>
  5. </div>
  6. </template>
  7. <script setup>
  8. import {onMounted} from "vue";
  9. import autoScreen from "./utils/autoScreen.js";
  10. // 挂载载
  11. onMounted(() => {
  12. document.body.style.height = '100vh';
  13. document.body.style.margin = '0';
  14. const _this = document.getElementById('screenRef')
  15. autoScreen(_this)
  16. });
  17. </script>
  18. <style>
  19. .screen-container {
  20. position: absolute;
  21. top: 50%;
  22. left: 50%;
  23. transform: translate(-50%, -50%);
  24. transform-origin: left top;
  25. overflow: hidden;
  26. }
  27. </style>

3.在需要有放大缩小功能的页面导入plugins.js的放大缩小两个方法,需要设置一下该页面的css样式

  1. <template>
  2. <div class="wrapper">
  3. <div class="full_screen">
  4. //有两个按钮,放大按钮,缩小按钮;此处我用两张图片代替按钮
  5. <img v-if="fullScreen" src="../../../assets/shrink_screen.png" alt="退出全屏" class="full_screen" @click="handleFullScreen()">
  6. <img v-else src="../../../assets/full_screen.png" alt="进入全屏" class="full_screen" @click="handleFullScreen()">
  7. </div>
  8. </div>
  9. </template>
  10. <script setup>
  11. import { exitFullscreen, enterFullScreen} from "../../../utils/plugins"; //此处要改为自己的文件夹路径
  12. const fullScreen = ref(false);//是否全屏
  13. //全屏&退出全屏
  14. const handleFullScreen = () => {
  15. fullScreen.value ? exitFullscreen() : enterFullScreen();
  16. fullScreen.value = !fullScreen.value
  17. };
  18. </script>
  19. <style scoped>
  20. .wrapper {
  21. width: 1920px;
  22. height: 1080px;
  23. margin: 0 auto;
  24. background: url('../../../assets/bg.png') center;
  25. background-size: contain;
  26. box-sizing: border-box;
  27. padding-top: 14px;
  28. position: relative;
  29. }
  30. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/128881
推荐阅读
相关标签
  

闽ICP备14008679号