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

<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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue 可以结合 CSS3 的 `vh` 单位和 JavaScript 获取屏幕高度来实现根据屏幕高度适配。 具体操作如下: 1. 在 CSS 中使用 `vh` 单位来设置元素高度。`vh` 单位是指相对于视口高度的百分比单位,例如 `height: 50vh` 表示元素高度为视口高度的一半。 2. 在 Vue 组件中使用 `mounted` 生命周期钩子获取屏幕高度,并将其存储到组件的数据中。 ```javascript mounted() { this.screenHeight = window.innerHeight; }, data() { return { screenHeight: 0 } } ``` 3. 在模板中使用数据绑定将获取到的屏幕高度应用到需要适配的元素上。 ```html <div :style="{ height: screenHeight + 'px' }">需要适配的元素</div> ``` 这样就可以根据屏幕高度实现适配了。需要注意的是,当屏幕大小变化时,应该使用 `resize` 事件重新获取屏幕高度并更新数据。 ### 回答2: Vue可以根据屏幕高度自适应,主要通过CSS和响应式设计来实现。 首先,在Vue组件中,可以通过使用计算属性来获取屏幕高度。可以使用window对象的innerHeight属性来获取浏览器窗口的高度,并将其存储在data属性中。 ```js export default { data() { return { screenHeight: 0 } }, computed: { styleObject() { return { height: `${this.screenHeight}px` } } }, mounted() { this.screenHeight = window.innerHeight window.addEventListener('resize', () => { this.screenHeight = window.innerHeight }) } } ``` 然后,在模板中可以使用计算属性的值来动态设置样式,从而实现根据屏幕高度进行适配。 ```html <template> <div :style="styleObject"> <!-- 内容 --> </div> </template> ``` 上述代码中,我们在mounted钩子函数中监听窗口的resize事件,并在事件回调函数中更新屏幕高度的值。这样,无论窗口大小如何改变,屏幕高度都能被动态更新。 最后,将计算属性styleObject应用在需要适配的元素上,样式对象中的高度会根据屏幕高度进行动态调整。 这样,Vue就可以根据屏幕高度来适应不同的设备,保证页面在不同屏幕上的显示效果。 ### 回答3: 在Vue中根据屏幕高度适配可以通过几种不同的方法来实现。一种常见的方法是使用CSS样式来动态调整元素的高度。可以在Vue组件中使用计算属性或绑定样式来实现这个功能。 首先,可以使用计算属性来获取屏幕的高度。可以通过在Vue实例中的`created`或者`mounted`生命周期钩子中获取屏幕高度,并将其保存为一个计算属性。例如: ``` computed: { screenHeight() { return window.innerHeight; } } ``` 然后,可以在模板中使用绑定样式来根据屏幕高度调整元素的样式。可以使用`:style`绑定来设置元素的高度。例如: ``` <div :style="{ height: screenHeight + 'px' }"> 这个元素的高度将根据屏幕高度而定 </div> ``` 这样,当窗口的大小改变时,元素的高度将会自动调整以适应屏幕的高度。 另外,还可以使用第三方库,例如`vue-screen-size`,来更方便地获取屏幕的高度。这个库可以在Vue组件中直接使用`screenHeight`属性来获取屏幕高度。例如: ``` import {screenHeight} from 'vue-screen-size'; export default { data() { return { screenHeight: screenHeight, }; }, }; ``` 然后在模板中同样可以根据屏幕高度来设置元素的样式。例如: ``` <div :style="{ height: screenHeight + 'px' }"> 这个元素的高度将根据屏幕高度而定 </div> ``` 以上是两种常用的在Vue中根据屏幕高度适配的方法,根据具体的项目需求选择适合的方式来实现。总的来说,通过获取屏幕高度并将其与元素样式绑定,可以实现在Vue中根据屏幕高度适配的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值