数据可视化大屏,根据窗口大小进行适配

文章介绍了如何在Vue组件中实现组件挂载时的自动缩放功能,通过计算isScale属性判断是否需要缩放,使用calcRate方法计算初始缩放比例,并利用定时器的防抖机制,仅在窗口大小变化后延迟执行calcRate,以优化性能并保持内容居中。
摘要由CSDN通过智能技术生成

  1. 组件挂载时,判断是否需要进行缩放(通过isScale计算属性)。
  2. 如果需要缩放,首先执行一次calcRate方法以计算初始缩放比例,然后监听窗口的resize事件以响应窗口大小的变化。
  3. 当窗口大小发生变化时,resize方法被触发。该方法利用定时器来实现防抖,确保在一定延迟后只执行一次calcRate方法,从而重新计算缩放比例。
  4. calcRate方法根据当前窗口的宽高比与设计稿的宽高比来决定如何缩放内容,并通过修改appRef元素的transform属性来实现内容的缩放和居中。
// 屏幕适配 mixin 函数

// * 默认缩放值
const scale = {
  width: '1',
  height: '1',
}

// * 设计稿尺寸(px)
const baseWidth = 1920
const baseHeight = 1080

// * 需保持的比例(默认1.77778)
const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))

export default {
  data() {
    return {
      // * 定时函数
      drawTiming: null,
    }
  },
  computed: {
    isScale(){
      return this.$store.state.setting.isScale
    }
  },
  mounted () {
    if(!this.isScale){
      return
    }
    this.calcRate()
    window.addEventListener('resize', this.resize)
  },
  beforeDestroy () {
       window.removeEventListener('resize', this.resize)
  },
  methods: {
    calcRate () {
      const appRef = this.$refs["appRef"]
      if (!appRef) return 
      // 当前宽高比
      const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
      if (appRef) {
        if (currentRate > baseProportion) {
          // 表示更宽
          scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
          scale.height = (window.innerHeight / baseHeight).toFixed(5)
          appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
        } else {
          // 表示更高
          scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
          scale.width = (window.innerWidth / baseWidth).toFixed(5)
          appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
        }
      }
    },
    resize () {
      if(!this.isScale){
        return
      }
      clearTimeout(this.drawTiming)
      this.drawTiming = setTimeout(() => {
        this.calcRate()
      }, 200)
    }
  },
}
<template>
  <div id="index" ref="appRef" class="index_home" :class="{ page_is_scale: isScale }">
    <div class="bg">
      <dv-loading v-if="loading">Loading...</dv-loading>
      <div v-else class="host_body">
        <!-- 头部 s -->
        <div class="d-flex jc-center title_wrap">
          <div class="zuojuxing"></div>
          <div class="youjuxing"></div>
          <div class="guang"></div>
          <div class="d-flex jc-center">
            <div class="title">
              <span class="title-text">可视化平台</span>
            </div>
          </div>
          <div class="timers ">
            {{ dateYear }} {{ dateWeek }} {{ dateDay }}
          </div>
        </div>
        <!-- 头部 e-->
        <!-- 内容  s-->
        <router-view></router-view>
        <!-- 内容 e -->
      </div>
    </div>
  </div>
</template>

<script>
import drawMixin from "../utils/drawMixin";
import { formatTime } from "../utils/index.js";
export default {
  mixins: [drawMixin],
  data() {
    return {
      timing: null,
      loading: true,
      dateDay: null,
      dateYear: null,
      dateWeek: null,
      weekday: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],



    };
  },
  filters: {
    numsFilter(msg) {
      return msg || 0;
    },
  },
  computed: {

  },
  created() {

  },
  mounted() {
    this.timeFn();
    this.cancelLoading();
  },
  beforeDestroy() {
    clearInterval(this.timing);
  },
  methods: {
    showSetting() {
      this.$refs.setting.init()
    },
    timeFn() {
      this.timing = setInterval(() => {
        this.dateDay = formatTime(new Date(), "HH: mm: ss");
        this.dateYear = formatTime(new Date(), "yyyy-MM-dd");
        this.dateWeek = this.weekday[new Date().getDay()];
      }, 1000);
    },
    cancelLoading() {
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
  },
};
</script>

<style lang="scss">
@import "./home.scss";
</style>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue数据可视化大屏适配,可以采用CSS3的缩放属性进行等比缩放实现。阿里DataV和腾讯云图等大厂的数据可视化项目都采用了这种方案。在实际项目中,可以在mounted生命周期中监听窗口的变化,使用window.addEventListener('resize')来触发缩放操作。\[1\]\[2\] 在进行大屏适配之前,需要了解屏幕的比例。常见的屏幕比例有16:9,如1920*1080(1080P)、2k、4k等。在确定了实际屏幕比例后,可以选择一个设计稿尺寸进行设计,并严格按照设计稿的像素进行前端开发。下列适应方案可以适应同比例下的所有分辨率屏幕。\[3\] 综上所述,对于Vue数据可视化大屏适配,可以使用CSS3的缩放属性进行等比缩放,并在mounted生命周期中监听窗口变化进行缩放操作。在设计阶段需要确定实际屏幕比例,并按照设计稿的像素进行前端开发。 #### 引用[.reference_title] - *1* *3* [vue大屏可视化自适应完美方案](https://blog.csdn.net/u013180541/article/details/127302298)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Vue 数据可视化大屏适配](https://blog.csdn.net/qq_38427709/article/details/116857143)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值