Vue适配数据大屏

使用scale来实现适配多种屏幕,设计稿尺寸通常为1920*1080(默认尺寸16:9)

注:开发时可在浏览器中添加以下尺寸进行调试

常用尺寸: 1080P:1920*1080

2K:2560*1440

4K:3840*2160

8K:7680*4320

Html部分:

<template>
    <div class="appContent" ref="contentRef">
        <div>内容</div>
    </div>
</template>

Css部分:

.appContent{
    width: 1920px;
    height: 1080px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transform-origin: left top;
}

Js部分:

export default {
    data() {
        return {
           scale: {
            width:'1',
            height:'1'
           },
           scaleTime: null, // 定时任务
           baseWidth: 1920, // 设计稿尺寸
           baseHeight: 1080,  // 设计稿尺寸
           baseProportion: 0, // 比例
        }
    },
    created() { 
        this.baseProportion = parseFloat((this.baseWidth/this.baseHeight).toFixed(2));
    },
    mounted() {
        this.calcRate()
        window.addEventListener('resize', this.resize);
    },
    beforeDestroy () {
        window.removeEventListener('resize', this.resize);
    },
    methods: {
        // 计算放大倍数
       calcRate(){
            const contentRef = this.$refs.contentRef;
            if (!contentRef) return;
            // 当前宽高比
            const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(2));
            if (currentRate > this.baseProportion) {
                // 表示更宽
                this.scale.width = ((window.innerHeight * this.baseProportion) / this.baseWidth).toFixed(2);
                this.scale.height = (window.innerHeight / this.baseHeight).toFixed(2);
                contentRef.style.transform = `scale(${this.scale.width}, ${this.scale.height}) translate(-50%, -50%)`;
            } else {
                // 表示更高
                this.scale.height = ((window.innerWidth / this.baseProportion) / this.baseHeight).toFixed(2);
                this.scale.width = (window.innerWidth / this.baseWidth).toFixed(2);
                contentRef.style.transform = `scale(${this.scale.width}, ${this.scale.height}) translate(-50%, -50%)`;
            }
       },
       // 屏幕大小改变更新倍数
       resize () {
            clearTimeout(this.scaleTime)
            this.scaleTime = setTimeout(() => {
                this.calcRate()
            }, 0)
       }
    }
}
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值