页面使用 scale 缩放实践

最近接到一个任务,需要把页面放大1.5倍显示。这里使用 css 里的 transform: scale(1.5) 来实现。

javascript 复制代码
document.body.setAttribute(
    'style',
    `transform: scale(1.5); transform-origin: left top; width: ${window.innerWidth / 1.5}px; height: ${window.innerHeight / 1.5}px;`
);

缩放后元素大小

width

API
clientWidth 583
offsetWidth 597
scrollWidth 700
window.getComputedStyle($0).width 596.5px
getBoundingClientRect().width 894.75

height

API
clientHeight 226
offsetHeight 240
scrollHeight 431
window.getComputedStyle($0).height 240px
getBoundingClientRect().height 360

通过以上数值比较,我们发现:

clientWidthclientHeightoffsetWidthoffsetHeightscrollWidthscrollHeightwindow.getComputedStyle 获取的值都是缩放前的;

getBoundingClientRect获取的值是缩放后的。

这里我们以 width 为例,我们先把 getBoundingClientRectwidth 换算成缩放前的值

javascript 复制代码
894.75 / 1.5 (缩放倍数) = 596.5

我们发现换算后的值跟 getComputedStyle 获取到的值一致,是缩放前宽度的值;clientWidthoffsetWidth 的值会进行取整。

计算宽高的时候我们优先选择 getBoundingClientRectgetComputedStyle

scroll

我们再来看下 scrollHeight,我们把滚动条滚动到最底部

API
clientHeight 226
offsetHeight 240
scrollHeight 431
scrollTop 205

我们发现

javascript 复制代码
clientHeight + scrollTop = scrollHeight

这里我们可以得出 scrollTopscrollLeftscrollHeightscrollWidth 也是缩放前的值。

因此在计算宽高的时候,我们优先选择 getComputedStyle,可以避免进行换算。

这里我们将其封装成方法,方便使用:

ts 复制代码
/** 获取元素大小 */
function getElementSize(el: Element) {
    const style = window.getComputedStyle(el);
    return {
        width: parseFloat(style.width),
        height: parseFloat(style.height),
    };
}

坐标

getBoundingClientRect

属性
width 894.75
height 360
left 213
top 224.90625
right 1107.75
bottom 584.90625
x 213
y 224.90625

getBoundingClientRect 获取到的宽高和坐标位置都是缩放后的(即实际显示值)。

MouseEvent

属性
clientX 213
clientY 225
pageX 213
pageY 225

MouseEvent 获取到的坐标系值也是缩放后的(即实际显示值)。

因此在和坐标位置有关的值进行计算时,推荐使用 getBoundingClientRect

这里有一点要注意的,我们使用坐标及getBoundingClientRect宽高计算出来的值,由于这些值是缩放后的,最终设置到元素上的时候需要除以放大倍数(这里是1.5倍),因为设置在元素上的值也会被缩放。例如:

javascript 复制代码
element.style.left = `${e.clientX / 1.5}px`;
element.style.width = `${element.getBoundingClientRect().width / 1.5}px`

获取 scale 的值

当我们使用坐标有关的值时避免不了进行换算,因此我们需要获取当前 scale 缩放倍数

ts 复制代码
function toNum(num: number) {
    return Number.isNaN(num) ? 1 : num;
}

function getScale(el: Element) {
    const eleRect: DOMRect = el.getBoundingClientRect();
    const computedStyle = window.getComputedStyle(el);
    const eleOutWidth = parseFloat(computedStyle.width);
    const eleOutHeight = parseFloat(computedStyle.height);

    const scaleX = toNum(Math.round((eleRect.width / eleOutWidth) * 1000) / 1000);
    const scaleY = toNum(Math.round((eleRect.height / eleOutHeight) * 1000) / 1000);

    return {
        scaleX,
        scaleY,
    };
}

这里我们封装了一个方法 getScale,传入当前元素的父元素,即可获取当前缩放倍数。例如:

javascript 复制代码
getScale(document.body)

得到的值为:

json 复制代码
{
    scaleX: 1.5
    scaleY: 1.5
}

我们得到当前水平缩放 1.5 倍,垂直缩放 1.5 倍。

总结

计算宽高的时候,优先选择 getComputedStyle

跟坐标位置有关的,使用 getBoundingClientRect,但是计算出的值需要除以 scale 放大倍数。

相关推荐
小彭努力中2 分钟前
205.Vue3 + OpenLayers:加载动画,采用 CSS 的 @keyframes 方式
前端·css·vue.js·openlayers·cesium·webgis
木斯佳6 分钟前
前端八股文面经大全:上海威派格前端实习(2026-05-07)·面经深度解析
前端
_Twink1e7 分钟前
基于Vue的纯前端的库存销售系统
前端·vue.js·vue·web
幽络源小助理15 分钟前
音频在线剪切助手网页版源码 – 纯前端HTML单文件免费分享
前端·音视频
陈振wx:zchen200815 分钟前
前端-面试题-Vue
前端·vue.js
计算机安禾16 分钟前
【c++面向对象编程】第5篇:类与对象(四):赋值运算符重载
java·前端·c++
Moment17 分钟前
从 beginWork 到 completeWork,Fiber 树是怎么“盖”出来的❓❓❓
前端·javascript·面试
Java面试题总结19 分钟前
.NET 8 Web开发入门(三):解构引擎——依赖注入(DI)与中间件管道
前端·中间件·.net
不会写DN23 分钟前
为什么需要 @types/react? 解决“无法找到模块 react 的声明文件”报错
前端·react.js·前端框架
前端初见24 分钟前
React 开发实战全攻略:从基础到项目实战(面向 Vue 开发者)
javascript·vue.js·react.js