如何在所有现代浏览器中检测页面缩放级别?虽然这篇文章讲述了如何在IE7和IE8中做到这一点,但我找不到一个好的跨浏览器解决方案。 Firefox存储页面缩放级别以供将来访问。在第一页加载,我能得到缩放级别吗?在某个地方,我读到当页面加载后发生缩放变化时,它是有效的。 有办法捕捉'缩放'事件吗?

我需要这个,因为我的一些计算是基于像素的,他们可能会在放大时波动。


@tfl给出的修改样本

放大时,此页面会提示不同的高度值。(jsFiddle)

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"/></script>
    </head>
    <body>
        <div id="xy" style="border:1px solid #f00; width:100px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sollicitudin tortor in lacus tincidunt volutpat. Integer dignissim imperdiet mollis. Suspendisse quis tortor velit, placerat tempor neque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent bibendum auctor lorem vitae tempor. Nullam condimentum aliquam elementum. Nullam egestas gravida elementum. Maecenas mattis molestie nisl sit amet vehicula. Donec semper tristique blandit. Vestibulum adipiscing placerat mollis.</div>
        <button onclick="alert($('#xy').height());">Show</button>
    </body>
</html>

当前回答

没有在IE中测试这个,但是如果你创建一个元素elem with

min-width: 100%

然后

window.document.width / elem.clientWidth

将为您提供浏览器缩放级别(包括document.body.style.zoom因子)。

其他回答

您的计算仍然是基于一些CSS像素。现在它们在屏幕上的大小不同了。这就是整页缩放的意义所在。

您希望在192dpi设备上的浏览器上发生什么,因此通常在图像中的每个像素中显示四个设备像素?在50%缩放时,这个设备现在在一个设备像素中显示一个图像像素。

我使用了不同的方法,我创建了一个dom元素,高度和宽度固定为100px,位置固定,不透明度为0,基本上是一个隐藏元素。

然后我使用dom-to-image来捕获这个元素作为图像,这听起来可能有点过分,但我想要一个防弹的解决方案,我们已经在使用这个包了,然后验证输出图像宽度,如果它返回110,缩放是110%,这是非常准确的。

const ORIGINAL_ZOOM_SIZE = 100;
async function isValidateZoomLevel() {
    const base64Render = await domToImage({
      ref: hiddenElementReference,
    });
    const pixleRatio = Math.round(window.devicePixelRatio);
    return new Promise((resolve, reject) => {
      const img = new Image();
      img.onload = () => resolve(ORIGINAL_ZOOM_SIZE === img.width / pixleRatio && ORIGINAL_ZOOM_SIZE === (img.height / pixleRatio));
      img.onerror = reject;
      img.src = base64Render; 
    });
  }

这是一个很久以前发布的问题,但今天当我在寻找相同的答案“如何检测放大和缩小事件”时,我找不到一个适合所有浏览器的答案。

就像现在一样:对于Firefox/Chrome/IE8和IE9,放大和缩小会触发一个窗口。调整大小事件。 可以使用以下方法捕获:

$(window).resize(function() {
//YOUR CODE.
});

截至2016年1月,我有一个解决方案。在Chrome, Firefox和MS Edge浏览器中测试工作。

The principle is as follows. Collect 2 MouseEvent points that are far apart. Each mouse event comes with screen and document coordinates. Measure the distance between the 2 points in both coordinate systems. Although there are variable fixed offsets between the coordinate systems due to the browser furniture, the distance between the points should be identical if the page is not zoomed. The reason for specifying "far apart" (I put this as 12 pixels) is so that small zoom changes (e.g. 90% or 110%) are detectable.

参考: https://developer.mozilla.org/en/docs/Web/Events/mousemove

步骤:

添加一个鼠标移动监听器 窗口。addEventListener("鼠标移动",函数(事件){ //处理事件 }); 从鼠标事件中捕获4个测量值: 事件。clientX、事件。clientY、事件。screenX, event.screenY 测量客户端系统中两点之间的距离d_c 测量屏幕系统中两点之间的距离d_s 如果d_c != d_s,则应用缩放。两者之间的差别告诉你缩放的大小。

注意:很少做距离计算,例如,当你可以采样一个新的鼠标事件,它离前一个事件很远的时候。

限制:假设用户将移动鼠标至少一点,缩放是不可知的,直到这个时候。

你可以使用Visual Viewport API:

window.visualViewport.scale;

它是标准的,在桌面和移动设备上都可以使用:浏览器支持。