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

vue3【实战】响应式主题(实时获取页面比例,指定尺寸内按比例缩放,超过指定尺寸保持高度不变的图片)

实时获取页面比例

  • 移动端设计稿通常为 750px
  • 当前窗口的宽为 window.innerWidth
  • 通过 useResizeObserver 可实时监听窗口大小的变化

src/stores/theme.ts

export const useThemeStroe = defineStore('theme', () => {const rate = ref(0)function setRate(newRate: number) {rate.value = newRate}return { rate, setRate }
})

src/App.vue

import { useThemeStroe } from '@/stores/theme'const themeStroe = useThemeStroe()useResizeObserver(document.body, () => {const w_width = window.innerWidth// 设计稿的宽度const contentWidth = 750themeStroe.setRate((w_width / contentWidth).toFixed(2))
})

src/pages/index.vue

页面中获取比例

import { useThemeStroe } from '@/stores/theme'const themeStroe = useThemeStroe()
:style="{ height: themeStroe.rate * 24 + 'rem' }"

指定尺寸内按比例缩放,超过指定尺寸保持高度不变的图片

在这里插入图片描述

src/pages/index.vue

页面中的图片高度根据随窗口变化动态更新 rate 计算得到

  <imgclass="w-full"style="object-fit: cover":style="{ height: themeStroe.rate * 24 + 'rem' }"src="https://img0.baidu.com/it/u=998088135,496941855&fm=253&fmt=auto&app=138&f=PNG?w=600&h=300"/>
import { useThemeStroe } from '@/stores/theme'const themeStroe = useThemeStroe()

src/App.vue

import { useThemeStroe } from '@/stores/theme'const themeStroe = useThemeStroe()useResizeObserver(document.body, () => {const w_width = window.innerWidth// 设计稿的宽度const contentWidth = 750themeStroe.setRate(w_width < contentWidth ? +(w_width / contentWidth).toFixed(2) : 1)
})

src/stores/theme.ts

export const useThemeStroe = defineStore('theme', () => {const rate = ref(0)function setRate(newRate: number) {rate.value = newRate}return { rate, setRate }
})

相关文章:

  • 云原生|浅谈云原生中的对象存储之MinIO 的使用
  • 回归预测 | Matlab基于SO-SVR蛇群算法优化支持向量机的数据多输入单输出回归预测
  • 【Linux】常用指令【更详细,带实操】
  • 数据结构编程实践20讲(Python版)—01数组
  • idea2021git从dev分支合并到主分支master
  • 什么是机器学习?
  • 谷神后端$vs.proc.invoke.stock.loadMap
  • ngxin
  • FileLink跨网文件传输 | 一站式跨网文件交换平台,重塑企业信息流通生态!
  • mysql数据库的基本管理
  • ansible实用模块
  • LLM基础概念:Prompt
  • Desmos图形计算器分段函数
  • SpringBoot日志详解
  • 传输大咖47 | 软件企业文件传输难题?这款FTP替代工具了解一下
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • Angular2开发踩坑系列-生产环境编译
  • express.js的介绍及使用
  • HTTP那些事
  • Laravel Telescope:优雅的应用调试工具
  • LintCode 31. partitionArray 数组划分
  • nginx 负载服务器优化
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 笨办法学C 练习34:动态数组
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 前端面试之闭包
  • 人脸识别最新开发经验demo
  • 如何用vue打造一个移动端音乐播放器
  • 译有关态射的一切
  • 你对linux中grep命令知道多少?
  • #Datawhale X 李宏毅苹果书 AI夏令营#3.13.2局部极小值与鞍点批量和动量
  • #QT(智能家居界面-界面切换)
  • (160)时序收敛--->(10)时序收敛十
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (vue)el-tabs选中最后一项后更新数据后无法展开
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (二十九)STL map容器(映射)与STL pair容器(值对)
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (四)图像的%2线性拉伸
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • (转载)hibernate缓存
  • (转载)从 Java 代码到 Java 堆
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .NET Standard、.NET Framework 、.NET Core三者的关系与区别?
  • .net的socket示例
  • .net对接阿里云CSB服务
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • .net网站发布-允许更新此预编译站点