首页 > 其他分享 >vue开发大屏项目屏幕适配问题解决方案

vue开发大屏项目屏幕适配问题解决方案

时间:2023-02-21 17:46:56浏览次数:37  
标签:el style vue const callback 适配 height width 大屏

1.新建自定义指令文件如下:

 

2.文件中插入一下代码:

import { App, Directive, DirectiveBinding, nextTick } from 'vue'
import { throttle } from 'lodash-es'
import ResizeObserver from 'resize-observer-polyfill'
import { floor, isArray, isFunction } from 'lodash-es'
interface TYPE1 extends DirectiveBinding {
    value: {
        rect: Array<Number>
        callback: Function
    }
}
interface TYPE2 extends DirectiveBinding {
    value: Array<Number>
}
function handler(el: HTMLElement, binding) {
    const { width, height, callback } = binding
    const { clientWidth, clientHeight } = el.parentElement
    const widthScale = floor(clientWidth / width, 2)
    const heightScale = floor(clientHeight / height, 2)
    const scale = widthScale < heightScale ? widthScale : heightScale
    el.style.setProperty('transform', 'scale(' + scale + ')')
    el.style.setProperty('top', (clientHeight - height) / 2 + 'px')
    el.style.setProperty('left', (clientWidth - width) / 2 + 'px')
    isFunction(callback) && callback(scale)
}
const containDirective: Directive = {
    mounted(el: HTMLElement, binding: TYPE1 | TYPE2) {
        const parent: HTMLElement | null = el.parentElement
        if (!parent) {
            throw new Error('v-contain指令所在的dom需要一个容器')
        }
        let width, height, callback
        if (isArray(binding.value)) {
            const [w, h] = binding.value
            width = w
            height = h
        } else {
            const [w, h] = binding.value.rect
            width = w
            height = h
            callback = binding.value.callback
        }
        parent.style.setProperty('position', 'relative')
        el.style.setProperty('position', 'absolute')
        el.style.setProperty('width', width + 'px')
        el.style.setProperty('height', height + 'px')
        const handleResize = throttle(() => {
            handler(el, { width, height, callback })
        }, 200)
        const resizeObserver = new ResizeObserver(handleResize)
        resizeObserver.observe(parent)
        el['data-resizeObserver'] = resizeObserver
        nextTick(() => {
            handler(el, { width, height, callback })
        })
    },
    unmounted(el) {
        el['data-resizeObserver']?.disconnect()
        el['data-resizeObserver'] = null
    }
}
export function setupContain(app: App) {
    app.directive('contain', containDirective)
}

 3.在main.js中挂在这个指令

 

 

4.在需要控制比例的div上使用该指令

 

标签:el,style,vue,const,callback,适配,height,width,大屏
From: https://www.cnblogs.com/cscredis/p/17141807.html

相关文章

  • 1 props其他、 2 混入mixin 、3 插件、 4 elementui使用(重点) 、5 vuex 、6 vue Rout
    目录1props其他2混入mixin3插件4elementui使用(重点)5vuex6vueRouter7localStorage系列1props其他#安装依赖 cnpminstall#做成纯净的vue项目 -在router......
  • vue2 day7
    昨日回顾#1nodejs后端语言---》js语法---》node,npm命令 -npm命令下载模块慢-淘宝的cnpm,以后使用npm的地方都可以使用cnpm#2安装vue-cli创建项目 -vue项目的......
  • Vue全套教程
     1、初识Vuejs1.1、为什么学习Vuejs?可能你的公司正要用Vue将原项目重构可能你的公司新项目决定使用Vue技术栈可能你正在找工作,会发现十个前端八个对Vue有或多或少的要求......
  • Vue 学习笔记-入门(1)
    Vue入门简述​Vue(读音/vjuː/,类似于view)是一套用于构建用户界面的渐进式JavaScript框架。[5]与其它大型框架不同的是,Vue被设计为可以自底向上逐层应用。Vue的核......
  • 论今日,Vue VSCode Snippets 不进行代码提示的问题 或 vetur Request textDocument/doc
    这他喵的是因为vetur这个鬼东西升级了,然后和项目中某些包不匹配了,降级就好了,法克尤啊法克尤,我整了一天,大概是坏了吧灵感来源:https://cxymm.net/article/a843334549/1......
  • vue-cli创建项目、项目目录介绍、es6导入导出语法、小练习-登录功能、scoped
    目录1vue-cli创建项目2vue项目目录介绍3es6导入导出语法3.1App.vue,main.js,About.vue写了什么3.2导入导出语法3.2vue项目编写步骤4小练习-登录功能4.1App.vue动......
  • VUEX 使用学习六 : modules
    转载请注明出处:当Store中存放了非常多非常大的共享数据对象时,应用会变的非常的复杂,Store对象也会非常臃肿,所以Vuex提供了一个Module模块来分隔Store。通过对Vuex中的Sto......
  • VUEX 使用学习五 : getter
    转载请注明出处:Getter对Store中的数据进行加工处理形成新的数据。他不会修改state中的原始数据,起到的是包装数据的作用;有时我们需要从store中的state中派生出一......
  • 直播软件搭建,vue3 页面回到顶部(平缓滚动效果)
    直播软件搭建,vue3页面回到顶部(平缓滚动效果) common.js //页面回到顶部(滚动效果)exportconsthandleScroll=()=>{  letscrollTop=window.pageYOffset||d......
  • vue3 setup echarts5 绘制图表
    vue3<divref="chartRef1"style="width:100%;height:100%"/><scriptsetuplang="ts">importtype{ECharts,EChartsOption}from"echarts";import{init......