【问题标题】:Detect page zoom change with jQuery in Safari在 Safari 中使用 jQuery 检测页面缩放变化
【发布时间】:2021-11-30 07:45:42
【问题描述】:

我在包含 position:fixed 元素的 Web 应用程序中遇到 Safari 问题。当页面缩小(更小 100%)时,事情会中断,需要通过调用函数来修复。所以我想检测用户的缩放。不久前我发现了这个 jQueryPlug-in:

http://mlntn.com/2008/12/11/javascript-jquery-zoom-event-plugin/

http://mlntn.com/demos/jquery-zoom/

它检测可能导致页面缩放级别更改的键盘和鼠标事件。很公平。它适用于当前的 FF 和 IE,但不适用于 Safari。有什么想法可以在当前的 WebKit 浏览器中做类似的事情吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    它不是 this question 的直接副本,因为它处理 Mobile Safari,但同样的解决方案也可以。

    当你放大时,window.innerWidth 会被调整,但 document.documentElement.clientWidth 不会,因此:

    var zoom = document.documentElement.clientWidth / window.innerWidth;
    

    此外,您应该能够使用 onresize 事件处理程序(或 jQuery 的 .resize())来检查:

    var zoom = document.documentElement.clientWidth / window.innerWidth;
    $(window).resize(function() {
        var zoomNew = document.documentElement.clientWidth / window.innerWidth;
        if (zoom != zoomNew) {
            // zoom has changed
            // adjust your fixed element
            zoom = zoomNew
        }
    });
    

    【讨论】:

    • 我刚刚在 Chrome 上测试了这个理论,无论我放大到 100%、67% 还是 175%,我似乎总是得到非常接近 1 的比率。附带说明一下,jQuery 的 $(window).width() 返回与 document.documentElement.clientWidth 相同的值,而 window.innerWidth 通常稍大一些(尽管不足以反映缩放量)。
    • window.innerWidth 不是滚动条出现的确切宽度。没有缩放时,您必须执行 window.innerWidth - 17 才能获得 1
    • 在这种情况下,不要检查它们是否完全相等 (===),而是检查它们的差是否小于 1 (Math.abs(element.scrollTop + element.height - child. innerHeight)
    【解决方案2】:

    有一个从yonran 构建的漂亮插件可以进行检测。这是他之前在 StackOverflow 上提出的answered 问题。它适用于大多数浏览器。申请就这么简单:

    window.onresize = function onresize() {
      var r = DetectZoom.ratios();
      zoomLevel.innerHTML =
        "Zoom level: " + r.zoom +
        (r.zoom !== r.devicePxPerCssPx
            ? "; device to CSS pixel ratio: " + r.devicePxPerCssPx
            : "");
    }
    

    Demo

    【讨论】:

      【解决方案3】:

      srceen.width 是固定值,但 window.innerWidth 值会根据缩放效果而变化。请尝试以下代码:

       $(window).resize(function() {
         if(screen.width == window.innerWidth){
             alert("you are on normal page with 100% zoom");
         } else if(screen.width > window.innerWidth){
             alert("you have zoomed in the page i.e more than 100%");
         } else {
             alert("you have zoomed out i.e less than 100%");
         }
      });
      

      【讨论】:

        【解决方案4】:

        区分窗口大小调整、浏览器缩放变化和系统 dpi 变化

        ;(() => {
          const last = {
            devicePixelRatio: devicePixelRatio,
            innerWidth: innerWidth,
            innerHeight: innerHeight,
            outerWidth: outerWidth,
            outerHeight: outerHeight,
          }
              
          const browser = navigator.appVersion.includes('WebKit')
          
          const almostZero = n => n <= 1 && n >= -1
          
          window.addEventListener('resize', () => {
            if (last.devicePixelRatio !== devicePixelRatio) {
              if (browser ? almostZero(last.innerWidth - innerWidth) && almostZero(last.innerHeight - innerHeight)
                  :almostZero(last.outerWidth - outerWidth) && almostZero(last.outerHeight - outerHeight)) {
                console.log('system wide dpi change')
              } else {
                console.log('browser level zoom change')
              }
            } else {
              console.log('window resize')
            }
            last.devicePixelRatio = devicePixelRatio
            last.innerWidth = innerWidth
            last.innerHeight = innerHeight
            last.outerWidth = outerWidth
            last.outerHeight = outerHeight
          })
        })()
        

        适用于 Windows 上的 Chrome 和 Firefox

        【讨论】:

        • 事后 7 年,谢谢,我会看看这个
        猜你喜欢
        • 1970-01-01
        • 2014-03-03
        • 2011-12-02
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 2018-11-01
        • 1970-01-01
        相关资源
        最近更新 更多