vue3信息大屏适配解决

使用css的transform来解决适配问题

2.定义内容区域

 <div ref="screen" class="screen">
<div/>

1.将展示区域设置为水平垂直居中,宽高比设置1920*1080

 .screen {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 1920px;
    height: 1080px;
}

3.编写缩放比例方法

// 定义大屏缩放比例
function getScale(w = 1920, h = 1080) {
  // 得到视口宽高与定义的宽高的比例ww, hh
  const ww = window.innerWidth / w
  const hh = window.innerHeight / h
  // 宽高比例哪个小就使用哪个
  return ww < hh ? ww : hh
}

4.在页面挂载后修改内容盒子的缩放比例

onMounted(() => {
  screen.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`
})

5.添加调整窗口大小事件 

window.onresize = () => {
  console.log(111)

  screen.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`
}

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中使用`resize`进行大屏适配,可以使用Vue3提供的`onMounted`和`onUnmounted`钩子函数来监听窗口大小变化。 首先,在组件中引入`onMounted`和`onUnmounted`: ```javascript import { onMounted, onUnmounted } from 'vue'; ``` 然后,在`onMounted`中添加窗口大小变化的监听事件: ```javascript onMounted(() => { window.addEventListener('resize', handleResize); }); ``` 其中,`handleResize`是处理窗口大小变化的函数,可以根据实际需要进行相应的处理。 最后,在`onUnmounted`中移除监听事件: ```javascript onUnmounted(() => { window.removeEventListener('resize', handleResize); }); ``` 完整的代码示例如下: ```javascript <template> <div class="container" :style="{fontSize: fontSize}"> <slot></slot> </div> </template> <script> import { onMounted, onUnmounted, reactive } from 'vue'; export default { name: 'AdaptiveContainer', setup() { const state = reactive({ fontSize: '14px', }); const handleResize = () => { const width = document.documentElement.clientWidth; if (width >= 1200) { state.fontSize = '16px'; } else if (width >= 768) { state.fontSize = '14px'; } else { state.fontSize = '12px'; } }; onMounted(() => { window.addEventListener('resize', handleResize); }); onUnmounted(() => { window.removeEventListener('resize', handleResize); }); return { state, }; }, }; </script> <style scoped> .container { width: 100%; max-width: 1200px; margin: 0 auto; padding: 0 20px; } </style> ``` 其中,`AdaptiveContainer`组件会根据窗口大小自适应字体大小,当窗口宽度大于等于1200px时,字体大小为16px,当窗口宽度在768px到1200px之间时,字体大小为14px,当窗口宽度小于768px时,字体大小为12px。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值