【问题标题】:how ignore browser zoom (ctrl+scroll) for iframe on the page如何忽略页面上 iframe 的浏览器缩放(ctrl+scroll)
【发布时间】:2017-10-11 11:38:40
【问题描述】:

如何忽略页面上 iframe 的浏览器缩放(ctrl+scroll)

我尝试:

尝试使用window.devicePixelRatio 计算比例并更改 iframe 缩放 document.body.style.transform = 'scale(' + 1 / window.devicePixelRatio + ')';

【问题讨论】:

  • 您是否尝试在主页和iframe 中显示的页面上同时使用元标记?另外,您是从主页还是从 IFrame 中的页面调用 javascript 方法?
  • 是的,它是同一个页面,但有另一个表格和标题

标签: javascript html css reactjs printing


【解决方案1】:

好的,经过一番尝试,我找到了解决您问题的方法。它使用 tombigel (link) 的 DetectZoom 库。一切都在主文档中完成(例如 index.html)

首先,您必须在文档中添加以下几行 CSS

<style>
        #frame {
            -moz-transform: scale(1.0);
            -moz-transform-origin: 0 0;
            -o-transform-origin: 0 0;
            -webkit-transform-origin: 0 0;
        }
</style>

然后,将 id(在我的情况下为 frame)添加到您的 iframe,如下所示:

<iframe id="frame" src="...">
</iframe>

并且,在文档的最后,添加一些 javascript:

首先,我们需要 jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

然后,实际的 DetectZoom 库(我已经下载了它并将其放在此处的一行代码中以使事情变得更容易,但您当然可以将它放在单独的 js 文件中)。

<script>
    (function(root,ns,factory){"use strict";"undefined"!=typeof module&&module.exports?module.exports=factory(ns,root):"function"==typeof define&&define.amd?define(function(){return factory(ns,root)}):root[ns]=factory(ns,root)})(window,"detectZoom",function(){var devicePixelRatio=function(){return window.devicePixelRatio||1},fallback=function(){return{zoom:1,devicePxPerCssPx:1}},ie8=function(){var zoom=Math.round(100*(screen.deviceXDPI/screen.logicalXDPI))/100;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},ie10=function(){var zoom=Math.round(100*(document.documentElement.offsetHeight/window.innerHeight))/100;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},webkitMobile=function(){var deviceWidth=90==Math.abs(window.orientation)?screen.height:screen.width,zoom=deviceWidth/window.innerWidth;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},webkit=function(){var important=function(str){return str.replace(/;/g," !important;")},div=document.createElement("div");div.innerHTML="1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>0",div.setAttribute("style",important("font: 100px/1em sans-serif; -webkit-text-size-adjust: none; text-size-adjust: none; height: auto; width: 1em; padding: 0; overflow: visible;"));var container=document.createElement("div");container.setAttribute("style",important("width:0; height:0; overflow:hidden; visibility:hidden; position: absolute;")),container.appendChild(div),document.body.appendChild(container);var zoom=1e3/div.clientHeight;return zoom=Math.round(100*zoom)/100,document.body.removeChild(container),{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},firefox4=function(){var zoom=mediaQueryBinarySearch("min--moz-device-pixel-ratio","",0,10,20,1e-4);return zoom=Math.round(100*zoom)/100,{zoom:zoom,devicePxPerCssPx:zoom}},firefox18=function(){return{zoom:firefox4().zoom,devicePxPerCssPx:devicePixelRatio()}},opera11=function(){var zoom=window.top.outerWidth/window.top.innerWidth;return zoom=Math.round(100*zoom)/100,{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},mediaQueryBinarySearch=function(property,unit,a,b,maxIter,epsilon){function binarySearch(a,b,maxIter){var mid=(a+b)/2;if(0>=maxIter||epsilon>b-a)return mid;var query="("+property+":"+mid+unit+")";return matchMedia(query).matches?binarySearch(mid,b,maxIter-1):binarySearch(a,mid,maxIter-1)}var matchMedia,head,style,div;window.matchMedia?matchMedia=window.matchMedia:(head=document.getElementsByTagName("head")[0],style=document.createElement("style"),head.appendChild(style),div=document.createElement("div"),div.className="mediaQueryBinarySearch",div.style.display="none",document.body.appendChild(div),matchMedia=function(query){style.sheet.insertRule("@media "+query+"{.mediaQueryBinarySearch "+"{text-decoration: underline} }",0);var matched="underline"==getComputedStyle(div,null).textDecoration;return style.sheet.deleteRule(0),{matches:matched}});var ratio=binarySearch(a,b,maxIter);return div&&(head.removeChild(style),document.body.removeChild(div)),ratio},detectFunction=function(){var func=fallback;return isNaN(screen.logicalXDPI)||isNaN(screen.systemXDPI)?window.navigator.msMaxTouchPoints?func=ie10:"orientation"in window&&"string"==typeof document.body.style.webkitMarquee?func=webkitMobile:"string"==typeof document.body.style.webkitMarquee?func=webkit:navigator.userAgent.indexOf("Opera")>=0?func=opera11:window.devicePixelRatio?func=firefox18:firefox4().zoom>.001&&(func=firefox4):func=ie8,func}();return{zoom:function(){return detectFunction().zoom},device:function(){return detectFunction().devicePxPerCssPx}}});
</script>

最后,让我们为iframe 添加功能以保持大小。

<script>
    function refresh() {
        var zoomLevel = 1 / (window.detectZoom.device().toFixed(2));
        $('#frame').css({ transform: 'scale(' + zoomLevel +')' });
    }
    $(document).ready(function () {
        refresh();
        $(window).on('resize', refresh);
    });
</script>

您可以在这里看到一个工作示例:click(这将重定向到与上述内容相似的 HTML 文档。此 HTML 文档托管在我自己的服务器上,因为我无法在 JSFiddle 上使用 IFrame,但我那里没有病毒。承诺;)!)

请注意,我没有编写 DetectZoom 库,而只编写了缩放 iFrame 的最后一个 javscript 代码 sn-p。

一切都用 Google Chrome 61 测试过,我不知道它与其他或更老的兄弟的效果如何。

【讨论】:

  • 我不能使用一些库试试 let scale = 1 / window.devicePixelRatio; document.getElementById('frame').style.transform = 'scale(' + scale + ')'; window.addEventListener('resize', smFn);在页面 iframe 上确实没有改变,但在打印此框架时 - 不正确
  • 使用我在回答中提出的方法,iframe 可以正确缩放,但我不知道这是否取决于浏览器。但是为什么你不能使用图书馆呢?
【解决方案2】:

我使用下一个代码:

const updateSizePrintPage = () => {
        let scale = 1 / window.devicePixelRatio;
        document.getElementById('print-page').style.transform = 'scale(' + scale + ')';
        document.getElementById('print-page').style.transformOrigin = 'top left';
      };
 window.addEventListener('resize', updateSizePrintPage )

它对我有用。 请看这个问题:how did not zoom content on printing when browser zoom?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    相关资源
    最近更新 更多