简体   繁体   English

启用捏缩放内部iframe - Ionic 2 AngularJS 2

[英]Enable pinch to zoom inside iframe - Ionic 2 AngularJS 2

I added zooming="true" inside the tag but when the page is loaded I cannot zoom to increase or decrease the view. 我在标签内添加了zooming="true"但是当页面加载时我无法缩放以增加或减少视图。 I've also set webkitallowfullscreen mozallowfullscreen allowfullscreen to scale the page in order to fit the device screen but nothing changed and the page is still cut. 我还设置了webkitallowfullscreen mozallowfullscreen allowfullscreen来缩放页面以适应设备屏幕,但没有任何改变,页面仍然被剪切。

To explain this concept a little better I take for example Android native apps. 为了更好地解释这个概念,我采用Android原生应用程序。 Now, if you want to load a page from the web you use a WebView and the result is exactly like using an iframe on Ionic . 现在,如果您想从Web加载页面,则使用WebView ,结果就像在Ionic上使用iframe一样。 But on android things become simpler regarding customization: 但在Android上,事情变得更简单:

webview.getSettings().setBuiltInZoomControls(true);

to enable pinch-to-zoom, and 启用捏合缩放,和

webview.getSettings().setUseWideViewPort(true);

to fit and scale the web page depending on the size of the (mobile) screen. 根据(移动)屏幕的大小来适应和缩放网页。 Now, using Windows 10 it's not possible for me to build native iOS apps so I have to rely on cross-platform development . 现在,使用Windows 10 ,我无法构建native iOS apps因此我不得不依赖于cross-platform development

Here's my detail-page : html : 这是我的detail-pagehtml

<ion-content>
  <iframe sandbox class="link" frameborder="0" [src]="webPage()" zooming="true" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</ion-content>

scss : scss

detail-page {

    .scroll-content{
        padding: 0px ;
    }

    ::-webkit-scrollbar,
    *::-webkit-scrollbar {
        display: none;
    }

    iframe.link {
        width: 100%;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
    }

}

ts : ts

webPage() {
        return this.sanitizer.bypassSecurityTrustResourceUrl(this.entry.getElementsByTagName('link')[0].textContent);
    }

Hope you can help me. 希望您能够帮助我。

Edit 编辑

I added document.getElementsByTagName('iframe').contentWindow.document.body.style = 'zoom:50%'; 我添加了document.getElementsByTagName('iframe').contentWindow.document.body.style = 'zoom:50%'; but I'm getting a Typescript error : 但是我收到了一个Typescript错误

Typescript Error
Property 'contentWindow' does not exist on type 'NodeListOf<HTMLIFrameElement>'.

Here's my whole .ts file: 这是我的整个.ts文件:

export class DetailPage {

    entry:any = [];

    constructor(private sanitizer: DomSanitizer, public nav: NavController, navParams:NavParams) {
        console.log('run');
        this.nav = nav;
        this.entry = navParams.get('selectedEntry');
        console.log('My entry is: "'+ this.entry.getElementsByTagName('title')[0].textContent + '"');

        document.getElementsByTagName('iframe').contentWindow.document.body.style = 'zoom:50%';
    }

    webPage() {
        return this.sanitizer.bypassSecurityTrustResourceUrl(this.entry.getElementsByTagName('link')[0].textContent);
    }
}

Edit 2 编辑2

After adding id="myframe" inside <iframe> I've also tried with the function ngAfterViewInit() but still no changes there. <iframe>添加id="myframe"我也尝试使用函数ngAfterViewInit()但仍然没有更改。

ngAfterViewInit() {
  var x = document.getElementById("myframe");
  var y = (<HTMLIFrameElement> x).contentWindow.document;
  y.body.style.zoom = "50%";
}

And in this form too: 并以这种形式:

ngAfterViewInit() {
    var iframe:HTMLIFrameElement = <HTMLIFrameElement>document.getElementById('myframe');
    var iWindow = (<HTMLIFrameElement>iframe).contentWindow.document;
    iWindow.body.style.zoom = "50%";
}

You will need to track the gesture and apply the change of zoom to the iframe like this document.getElementByTagName('iframe').contentWindow.document.body.style = 'zoom:50%'; 您将需要跟踪手势并将缩放更改应用于iframe,如此document.getElementByTagName('iframe').contentWindow.document.body.style = 'zoom:50%';

Here the zoom is set to 50%, but this can be added dynamically using the gesture event values. 此处缩放设置为50%,但可以使用手势事件值动态添加。

I think it is not possible to do in a IFrame as that will be a security flaw. 我认为在IFrame中不可能做到这一点,因为这将是一个安全漏洞。 what you are basically doing is trying to access a webpage from your mobile hybrid app (Ionic App in your case). 您基本上正在尝试从您的移动混合应用程序(在您的情况下为Ionic App)访问网页。 it must not allow you to run javascript on that webpage, workaround for it will be by disabling web security on that browser or in your case webview (not sure how to do that in mobile but that is manual browser customization so will not work in your scenario). 它不能允许你在该网页上运行javascript,解决方法是通过在该浏览器或你的案例webview中禁用web安全性(不确定如何在移动设备中执行此操作,但这是手动浏览器自定义,因此无法在您的网页中使用场景)。

more explanation on this post SecurityError: Blocked a frame with origin from accessing a cross-origin frame 有关此帖子的更多解释SecurityError:阻止具有原点的帧访问跨源帧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM