【前端大屏自适应缩放】

🚀 作者 :“码上有前”
🚀 文章简介 :前端开发
🚀 欢迎小伙伴们 点赞👍、收藏⭐、留言💬
在这里插入图片描述

往期精彩内容

【前端高频面试题–HTML篇】
【前端高频面试题–CSS上篇】
【前端高频面试题–CSS下篇】
【前端高频面试题–JS上篇】
【前端高频面试题–JS下篇】
【前端高频面试题–ES6篇】
【前端高频面试题–ES7-ES11】
【前端–异步编程】
【前端高频面试题–TypeScript篇】

【前端高频面试题–git篇】
【前端高频面试题–微信小程序篇】

【前端高频面试题–Vue基础篇】
【前端高频面试题–虚拟DOM篇】
【前端高频面试题–Vue3.0篇】
【前端高频面试题–Vuex上篇】
【前端高频面试题–Vuex下篇】
【前端高频面试题–Vue生命周期篇】
【前端高频面试题–Vue组件通信篇】
【前端高频面试题–Vue路由篇】

【前端-Vue3创建一个新项目】
【前端大屏自适应缩放】
【前端Vue3 + TS项目开发一般流程】

简介

前端中大屏往往用于展示各种炫酷的界面和特效,因此特别受用好欢迎。
但是在开发过程中,常常也会出现各种问题,与一般的页面相比,
最让人头疼的是大屏的自适应问题。

使用CSS中transform属性和js获取缩放比例方法

	先简单写一下网页,先画一个大盒子container,再画自适应大屏盒子box,
再box中就是我们测试的两个小盒子。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <!-- 数据展示的区域 -->
        <div class="box">
            <div class="top">我是top部分,2035年实现基本社会主义现代化</div>
            <div class="bottom">我是bottom部分,
2050年实现第二个一百年奋斗目标,全面建成富强民主文明和谐美丽的社会主义现代化强国</div>
        </div>
    </div>
</body>
</html>
接着我们开始写css部分,主要用到vw和vh这两个属性单位和transform属性
* {
            margin: 0;
            padding: 0;
        }
        /* 大屏盒子box 使用fixed定位,将屏幕的尺寸设置为宽高 并通过 transform-origin: left top将变换的基点设置为屏幕左上角*/
        .container {
            width: 100vw;
            height: 100vh;
            background: url(./bg.png) no-repeat;
            background-size: cover;
        }
        .box {
            position: fixed;
            width: 1920px;
            height: 1080px;
            background: red;
            transform-origin: left top;
            left: 50%;
            top: 50%;
        }
        .top {
            width: 100px;
            height: 100px;
            background: hotpink;
            margin-left: 50px;
        }
        .bottom {
            width: 100px;
            height: 100px;
            background: skyblue;
            margin-left: 50px;
            margin-top: 100px;
        }

接着写js,通过resize控制屏幕尺寸大小

 //控制数据大屏放大与缩小
    let box = document.querySelector('.box');
    box.style.transform = `scale(${getScale()}) translate(-50%,-50%)`
    //计算缩放的比例啦
    function getScale(w = 1920, h = 1080) {
        const ww = window.innerWidth / w;
        const wh = window.innerHeight / h;
        return ww < wh ? ww : wh;
        //ww<wh情况: 1920/1920(ww)   1080/1080(wh)
        //ww>wh情况:1920/1920(ww)   1080/1080(wh)
    }
 	// 防抖
    window.onresize = () => {
        box.style.transform = `scale(${getScale()}) translate(-50%,-50%)`
    }

注意

同时还可以写防抖和多媒体查询使得屏幕缩放更加自如、
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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的自定义指令来实现缩放效果,使得组件代码更加简洁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上有前

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值