当前位置: 首页 > ai >正文

大屏放大缩小自适应

目录

components

responsive.ts

页面代码


 

components

/*** 大屏响应式布局组件* 实现自动缩放以适应不同尺寸的屏幕*/
<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch } from 'vue';
import { useWindowSize } from '@vueuse/core';
import { getScale, setScale } from '../../../utils/responsive';// 组件属性定义
const props = defineProps({// 设计稿宽度width: {type: Number,default: 1920},// 设计稿高度height: {type: Number,default: 1080},// 背景色background: {type: String,default: 'transparent'},// 背景图片backgroundImage: {type: String,default: ''}
});// 创建内容容器引用
const screenRef = ref<HTMLElement | null>(null);// 使用 vueuse 的窗口大小监听
const { width: windowWidth, height: windowHeight } = useWindowSize();/*** 应用缩放*/
const applyScale = () => {if (!screenRef.value) return;// 使用新的 APIsetScale(screenRef.value, {designWidth: props.width,designHeight: props.height});
};// 组件挂载时设置初始缩放
onMounted(() => {applyScale();
});// 监听窗口大小变化
watch([windowWidth, windowHeight], () => {applyScale();
});// 组件卸载时不需要手动移除监听,vueuse 会自动处理
onUnmounted(() => {});
</script><template><div class="screen-container" :style="{background: props.background,backgroundImage: props.backgroundImage ? `url(${props.backgroundImage})` : 'none'}"><div ref="screenRef" class="screen-content" :style="{width: `${props.width}px`,height: `${props.height}px`}"><!-- 内容插槽 --><slot></slot></div></div>
</template><style lang="scss" scoped>
.screen-container {position: relative;width: 100vw;height: 100vh;overflow: hidden;background-size: cover;background-position: center center;
}.screen-content {position: fixed;left: 50%;top: 50%;transform-origin: left top;
}
</style>

responsive.ts

/*** 大屏自适应工具* 根据当前屏幕大小计算缩放比例并应用transform*/// 设计稿尺寸,默认为1920x1080
export const DESIGN_WIDTH = 1920;
export const DESIGN_HEIGHT = 1080; /*** 缩放策略枚举*/
export enum ScalingStrategy {// 取最小缩放比例,确保内容完全显示(可能有空白)FIT = 'fit',// 取最大缩放比例,确保充满屏幕(可能有裁剪)FILL = 'fill',// 只按宽度缩放WIDTH = 'width',// 只按高度缩放HEIGHT = 'height'
}/*** 计算缩放比例* @param designWidth 设计稿宽度* @param designHeight 设计稿高度* @param strategy 缩放策略* @returns 当前屏幕的最佳缩放比例*/
export function getScale(designWidth = DESIGN_WIDTH, designHeight = DESIGN_HEIGHT,strategy = ScalingStrategy.FIT
): number {// 获取当前窗口宽高const width = window.innerWidth;const height = window.innerHeight;// 计算宽高比例const widthScale = width / designWidth;const heightScale = height / designHeight;// 根据策略返回不同的缩放比例switch (strategy) {case ScalingStrategy.FILL:return Math.max(widthScale, heightScale);case ScalingStrategy.WIDTH:return widthScale;case ScalingStrategy.HEIGHT:return heightScale;case ScalingStrategy.FIT:default:return Math.min(widthScale, heightScale);}
}/*** 缩放选项接口*/
export interface ScalingOptions {designWidth?: number;designHeight?: number;strategy?: ScalingStrategy;offsetX?: number;   // X轴偏移量调整(百分比)offsetY?: number;   // Y轴偏移量调整(百分比)
}/*** 设置元素的缩放* @param element 需要缩放的DOM元素* @param options 缩放选项*/
export function setScale(element: HTMLElement | null, options: ScalingOptions = {}
): void {if (!element) return;const {designWidth = DESIGN_WIDTH,designHeight = DESIGN_HEIGHT,strategy = ScalingStrategy.FIT,offsetX = -50,offsetY = -50} = options;// 计算并应用缩放const scale = getScale(designWidth, designHeight, strategy);element.style.transform = `scale(${scale}) translate(${offsetX}%, ${offsetY}%)`;// 添加当前缩放比例作为data属性,方便调试element.dataset.scale = scale.toFixed(3);
}/*** 初始化响应式大屏* @param elementRef 需要缩放的元素引用* @param options 缩放选项*/
export function initResponsive(elementRef: HTMLElement | null,options: ScalingOptions = {}
): { cleanup: () => void } {if (!elementRef) return { cleanup: () => {} };// 初始化缩放setScale(elementRef, options);// 创建调整函数const handleResize = () => {setScale(elementRef, options);};// 监听窗口大小变化,自动调整缩放window.addEventListener('resize', handleResize);// 返回清理函数return {cleanup: () => {window.removeEventListener('resize', handleResize);}};
}/*** 获取视口信息* 返回当前视口尺寸和缩放信息*/
export function getViewportInfo(designWidth = DESIGN_WIDTH, designHeight = DESIGN_HEIGHT) {const width = window.innerWidth;const height = window.innerHeight;const scale = getScale(designWidth, designHeight);const aspectRatio = width / height;const designAspectRatio = designWidth / designHeight;return {viewport: { width, height, aspectRatio },design: { width: designWidth, height: designHeight, aspectRatio: designAspectRatio },scale,isWider: aspectRatio > designAspectRatio,isNarrower: aspectRatio < designAspectRatio};
}

页面代码

  <ResponsiveScreen :width="1920" :height="1080"><div>这里面放你的页面代码 宽高100%</div></ResponsiveScreen>
<script setup lang="ts">
import ResponsiveScreen from '@/components/ResponsiveScreen/index.vue';
</script>

http://www.xdnf.cn/news/7465.html

相关文章:

  • 微软的 Windows Linux 子系统现已开源
  • 采集需要登录网站的教程
  • HTTP 协议的发展历程及技术演进
  • 使用亮数据代理IP+Python爬虫批量爬取招聘信息训练面试类AI智能体(附完整源码)
  • jmeter转义unicode变成中文
  • docker- Harbor 配置 HTTPS 协议的私有镜像仓库
  • Rofin PowerLine E Air维护和集成手侧激光Maintenance and Integration Manual
  • 能管理MySQL、Oracle、达梦数据库的桌面管理软件开源了
  • 使用 Java 开发 Android 应用:Kotlin 与 Java 的混合编程
  • 科技赋能·长效治理|无忧树建筑修缮渗漏水长效治理交流会圆满举行!
  • 企业级 Go 多版本环境部署指南-Ubuntu CentOS Rocky全兼容实践20250520
  • C# Task 与 SynchronizationContext
  • 文件包含靶场实现
  • 【移动应用安全】Android系统安全与保护机制
  • WPF技巧-常用的Converter集合(更新ing)
  • Spring Boot-Swagger离线文档(插件方式)
  • 【Redis】跳表结构
  • LSTM语言模型验证代码
  • springboot框架 集成海康ISUP-SDK 并实现 协议透传给设备下发指令!
  • 【鸿蒙开发】安全
  • centos 9 Kickstart + Ansible自动化部署 —— 筑梦之路
  • 软考软件评测师——数据库系统应用
  • spark-shuffle 类型及其对比
  • 新兴技术与安全挑战
  • Android7 Input(八)App Input事件接收器InputEventReceiver
  • 接口自动化可视化展示
  • CQF预备知识:Python相关库 —— 什么是 NumPy?
  • Linux网络基础全面解析:从协议分层到局域网通信原理
  • 【原创】ubuntu22.04下载编译AOSP 15
  • 杰里7006d日志分析