aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • 流年往事

  • 经验片段

    • 2022年2月21日
    • 2022年3月16日
    • 2022年3月21日
    • 2022年3月23日
    • 2022年4月9日
    • 2022年4月10日
    • 2022年4月15日
    • 2022年4月27日
    • 2022年4月28日
    • 2022年5月6日
    • 2022年5月16日
    • 2022年5月20日
    • 2022年6月16日
    • 2022年6月22日
    • 2022年6月23日
    • 2022年8月17日
    • 2022年10月12日
    • 2022年11月6日
    • 2022年11月8日
    • 2022年11月21日
    • 2022年12月06日
    • 2022年12月09日
    • 2022年12月27日
    • 2023年01月21日
    • 2023年02月03日
    • 2023年03月19日
    • 2023年03月26日
    • 2023年04月12日
    • 2023年05月03日
    • 2023年06月04日
  • 读书杂感

  • 历程
  • 经验片段
aiyoudiao
2022-03-21

2022年3月21日

看到以前的同事做了一个自适应的大屏,思考许久,尝试把自适应的代码写出来了。

debugger;
const width = 1920;
const height = 1080;
const targetRate = parseFloat((width/height).toFixed(5))

const currentWidth = window.innerWidth
const currentHeight = window.innerHeight
const currentScreenRate = parseFloat((currentWidth/currentHeight).toFixed(5));

let xRate,yRate
if(currentScreenRate > targetRate) {
	xRate = (currentHeight * targetRate / width).toFixed(5);
	yRate = (currentHeight / height).toFixed(5);
}else {
	xRate = (currentWidth / width).toFiexd(5);
	yRate = (currentWidth / taregtRate / height).toFixed(5);
}
const transformStyle = `scale(${xRate},${yRate}) translate(-50%, -50%)`;


// 一行代码的简写,声明变量可以通过reduce来传递,有几层变量就嵌套基层reduce。也得到了上面transformStyle的结果
Array(1).fill(0).reduce((_) => Array(1).fill(0).reduce(__ => (__.currentScreenRate > __.targetRate ? (_.xRate = (_.currentHeight * __.targetRate / _.width).toFixed(5),_.yRate = (_.currentHeight / _.height).toFixed(5)):(_.xRate = (_.currentWidth / _.width).toFiexd(5),_.yRate = (_.currentWidth / __.taregtRate / _.height).toFixed(5)),`scale(${_.xRate},${_.yRate}) translate(-50%, -50%)`), Object({targetRate: parseFloat((_.width/_.height).toFixed(5)), currentScreenRate: parseFloat((_.currentWidth/_.currentHeight).toFixed(5))})), Object({width: 1920, height: 1080, currentWidth: window.innerWidth, currentHeight: window.innerHeight, xRate: void 0, yRate: void 0}))

#代码片段
上次更新时间: 10年18月2023日 01时57分53秒
2022年3月16日
2022年3月23日

← 2022年3月16日 2022年3月23日 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1