vue 根据屏幕大小等比例压缩大屏

本文介绍了如何在网页中实现大屏布局,并通过JavaScript控制全屏显示与退出。开发者可以学习如何使用`fullscreen` API和事件监听来适配不同设备的全屏模式,以及如何根据屏幕尺寸动态调整内容的缩放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
    <page-header-wrapper title=" ">
    <!-- 设置大屏外包裹div 这里大屏初始化不是全屏的,是嵌入在页面里,有菜单,如场景和我这里不同,可以去掉此div或者自行修改高度 -->
    <div class="container" ref="container" :style="{ height: clientHeight - 168 + 'px',overflowY:'auto',overflowX:'hidden' }">
      <!-- 大屏,方法对此div操作 -->
      <div class="screen" ref="bigscreen">
        <p class="title">自适应大屏</p>
        <a-icon
          type="fullscreen"
          @click="fullScreen"
          v-show="isFullScreen"
          style="position: absolute; right: 10px; top: 10px; color: #fff; font-size: 20px"
        />
        <!-- 大屏内容
          ......
          .....
          .....
         -->
      </div>

        
    </div>
     </page-header-wrapper
  >
</template>

<script>

export default {
  data() {
    return {
      clientHeight: document.documentElement.clientHeight || document.body.clientHeight,
      isFullScreen: true,
      timer: null,
      scaleCoeff: 0,
      baseWidth: 1920,
      baseHeight: 1080,
    }
  },
  mounted() {
    let isFull =
      document.fullscreenElement ||
      document.mozFullScreenElement ||
      document.webkitFullscreenElement ||
      document.fullScreen ||
      document.mozFullScreen ||
      document.webkitIsFullScreen;
    isFull= !!isFull;
    let that = this;
    document.addEventListener("fullscreenchange", () => {
      that.isFullScreen = !that.isFullScreen;
    });
    document.addEventListener("mozfullscreenchange", () => {
      that.isFullScreen = !that.isFullScreen;
    });
    document.addEventListener("webkitfullscreenchange", () => {
      that.isFullScreen = !that.isFullScreen;
    });
    document.addEventListener("msfullscreenchange", () => {
      that.isFullScreen = !that.isFullScreen;
    });

      this.calcRate()
    // 改变窗口大小重新绘制
    window.addEventListener('resize', this.resize)
  },     
  created() {
    window.onresize = () => {
      const h = document.documentElement.clientHeight || document.body.clientHeight
      this.clientHeight = h
    }
  },
  methods: {
    // 全屏操作
    fullScreen() {
      var el = this.$refs.bigscreen

      var rfs = el.requestFullScreen || el.webkitRequestFullScreen

      if (typeof rfs != 'undefined' && rfs) {
        rfs.call(el)
      } else if (typeof window.ActiveXObject != 'undefined') {
        var wscript = new ActiveXObject('WScript.Shell')

        if (wscript != null) {
          wscript.SendKeys('{F11}')
        }
      } else if (el.msRequestFullscreen) {
        el.msRequestFullscreen()
      } else if (el.oRequestFullscreen) {
        el.oRequestFullscreen()
      } else {
        this.$message.destroy()
        this.$message.warning('请更换浏览器或按F11键切换全屏')
      }
    },
    calcRate() {
      let db = this.$refs.bigscreen
      if (db && this.$refs.container.offsetWidth) {    
          //这里是以宽为准来等比例压缩,若需求不同 可以根据代码进行修改
          this.scaleCoeff = (Number(this.$refs.container.offsetWidth)/ this.baseWidth).toFixed(5)
          if(this.scaleCoeff>0.60){
            this.$refs.container.style.overflow="auto"
          }else{
            this.$refs.container.style.overflow="hidden"
          }
          db.style.transform = 'scale(' + this.scaleCoeff + ') translate(0%,0%)'
        
      }
    },
    resize() {
        this.calcRate()
    },
  },
}
</script>
<style lang="less" scoped>
.container {
  width: 100%;
}
.screen {
  // 此处很重要 需要有个固定宽高来进行压缩
  width: 1920px;
  height: 1080px;
  background-color: rgb(18, 18, 75);
  position: relative;
  transform-origin: left top;
}
</style>

vue⼤数据渲染_VueDataV⼤数据可视化模板VueBigScreen vue-big-screen 基于 vue+datav+echart 框架构建的可视化数据⼤项⽬,star⾼达1.1K+。提供数据动态渲染、屏幕⾃适应、内部图表 ⾃由替换等功能。 ⽬录结构 安装使⽤ 更换边框 边框是使⽤了 DataV ⾃带的组件,只需要去 views ⽬录下去寻找对应的位置去查找并替换就可以,具体的种类请去 DavaV 官⽹查看。 更换图表 直接进⼊ components/echart 下的⽂件修改成你要的 echarts 模样,可以去echarts 官⽅社区⾥⾯查看案例。 屏幕适配配置 本项⽬借助了 flexible 插件,通过改变 rem 的值来进⾏适配,原设计为 1920px。 ,适配区间为:1366px ~ 2560px,本项⽬有根据 实际情况进⾏源⽂件的更改,⼩屏幕(如:宽为 1366px)。// flexible⽂件位置: `common/flexible.js`,修改部分如下function refreshRem() { var width = docEl.getBoundingClientRect().width; // 最⼩1366px,最⼤适配2560px if (width / dpr < 1366) { width = 1366 * dpr; } else if (width / dpr > 2560) { width = 2560 * dpr; } // 原项⽬是1920px我设置成24等份,这样1rem就是 80px var rem = width / 24; docEl.style.fontSize = rem + 'px'; flexible.rem = win.rem = rem;} 以上就是对这个Vue项⽬的简单介绍,如果你想看到更详细的⽂档,那就点击下⾯的链接去看看吧! ok,就分享到这⾥。希望对⼤家有所帮助,喜欢的话可以去看下哈~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值