Vue 大屏可视化-屏幕自适应(保持设计尺寸比例)

最近在用VUE写大屏页面,遇到屏幕自适应问题,根据设计的尺寸,在各种屏幕尺寸下自适应,保持显示比例不变,经过几天摸索,终于达到了效果,希望为还在这问题困扰的提供点帮助

铺满全屏效果见Vue 大屏可视化 铺满全屏

1. 在配置文件设置大屏设计的尺寸1920*1080


//config/base.js
export default{
    screen:{
        width:1920,
        height:1080,
        scale:20
    }//大屏设计宽高
}

2. 定义resetScreenSize.js


import appConfig from '../config/base'

export function pageResize(callback) {
    let init = () => {
        console.log(window.innerHeight + "," + window.innerWidth);    
        let _el = document.getElementById('app');
       
        let hScale = window.innerHeight / appConfig.screen.height;
        let wScale = window.innerWidth / appConfig.screen.width;
        let pageH = window.innerHeight;
        let pageW = window.innerWidth;
       
        let isWider = (window.innerWidth / window.innerHeight) >= (appConfig.screen.width / appConfig.screen.height);
        console.log(isWider);
        if (isWider) {
                _el.style.height = window.innerHeight+'px';// '100%';
                _el.style.width = pageH * appConfig.screen.width / appConfig.screen.height + 'px';
                _el.style.top='0px';
                _el.style.left=(window.innerWidth -pageH * appConfig.screen.width / appConfig.screen.height)*0.5+'px';
                console.log(_el.style.width + "," + _el.style.height)
        }
        else {
                _el.style.width = window.innerWidth+'px';// '100%';
                _el.style.height = pageW * appConfig.screen.height / appConfig.screen.width + 'px';
                _el.style.top= 0.5*(window.innerHeight-pageW * appConfig.screen.height / appConfig.screen.width)+'px';
                _el.style.left='0px';
                console.log(_el.style.height);
                console.log(_el.style.top);
        }
        document.documentElement.style.fontSize =  (_el.clientWidth / appConfig.screen.scale) + 'px';

      
    }    
    var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
    window.addEventListener(resizeEvt, init, false);
    document.documentElement.addEventListener('DOMContentLoaded', init, false);
    init()
}

3 使用方式

main.js 引入

import appConfig from './config/base.js';
Vue.prototype.appConfig=appConfig;

app.Vue  在mounted函数引入

import  {pageResize} from './utils/resetScreenSize'

export default {
  name: 'App',
  data(){
    return{
       
    }
  },
  mounted(){
    pageResize();
    console.log('pageResize');
  }
}

组件中样式 lang="stylus"

 .mc{
        display :flex;
        flex-direction :column;
        align-content :center;
        justify-content :center;  
        display: flex;
        flex: 1 1 auto;
        flex-direction: column;
        padding:(15/96)rem;
    }

    .leftC{
       width :(410/96)rem;
    }

    .centerC{
       width :(1060/96)rem;
    }

    .rightC{
       width :(450/96)rem;
    }

其中 96为 配置文件中1920/20得来,这样不用在进行各种换算了

  • 9
    点赞
  • 119
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Vue项目做可视化大屏自适应缩放可以尝试以下几个方案: 1. 使用CSS3的transform属性进行缩放 在Vue组件中设置一个容器元素,通过CSS3的transform属性对容器元素进行缩放,可以实现自适应缩放的效果。在容器元素的父元素中监听resize事件,根据父元素的宽度和高度来计算缩放比例,然后通过CSS3的transform属性对容器元素进行缩放。 示例代码: ```html <template> <div class="container" ref="container"> <!-- 可视化大屏内容 --> </div> </template> <script> export default { mounted() { window.addEventListener('resize', this.handleResize) }, beforeDestroy() { window.removeEventListener('resize', this.handleResize) }, methods: { handleResize() { const container = this.$refs.container const parentWidth = container.parentNode.clientWidth const parentHeight = container.parentNode.clientHeight const contentWidth = container.offsetWidth const contentHeight = container.offsetHeight const scaleX = parentWidth / contentWidth const scaleY = parentHeight / contentHeight container.style.transform = `scale(${Math.min(scaleX, scaleY)})` } } } </script> <style> .container { transform-origin: top left; } </style> ``` 2. 使用Vue的自定义指令进行缩放 在Vue项目中可以创建一个自定义指令,通过该指令来实现自适应缩放的效果。在指令中监听resize事件,根据父元素的宽度和高度来计算缩放比例,然后通过CSS3的transform属性对元素进行缩放。 示例代码: ```html <template> <div class="container" v-resize-scale> <!-- 可视化大屏内容 --> </div> </template> <script> export default { directives: { resizeScale: { inserted(el) { function handleResize() { const parentWidth = el.parentNode.clientWidth const parentHeight = el.parentNode.clientHeight const contentWidth = el.offsetWidth const contentHeight = el.offsetHeight const scaleX = parentWidth / contentWidth const scaleY = parentHeight / contentHeight el.style.transform = `scale(${Math.min(scaleX, scaleY)})` } window.addEventListener('resize', handleResize) handleResize() }, unbind() { window.removeEventListener('resize', this.handleResize) } } } } </script> <style> .container { transform-origin: top left; } </style> ``` 以上两种方案可以根据实际需求进行选择,第一种方案比较简单,但是需要在组件中手动监听resize事件,第二种方案可以通过Vue的自定义指令来实现缩放效果,使得组件代码更加简洁。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值