简体   繁体   中英

Zooming in a specific div in Cordova/Ionic

I added the viewport with properties allowing zooming/scaling. And I added these to the native code:

WebSettings settings = super.appView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(true);
settings.setSupportZoom(true);

I am able to zoom, but along with my view, ion-header-bar and ion-nav-bar gets zoomed. I tried giving the header css, to keep it fixed:

position: fixed;
top: 0px;
left: 0px;

but still, it'd get zoomed.

My index.html has an ion-header-bar , which contains an image. The templates go into

<ion-nav-view class="has-header"></ion-nav-view>

The template in which I require zooming in a particular div is having a 'ion-view` and it looks like:

<ion-view>
    <ion-nav-bar class="bar-stable">
        <ion-nav-buttons side="right">
            icon1
        </ion-nav-buttons>
        <ion-nav-buttons side="left">
            icon2
        </ion-nav-buttons>
    </ion-nav-bar>
    <div>
        Multiple divs in here, which are containers for html and css data we receive via AJAX requests. And this is here I need zooming in.
    </div>
</ion-view>

PS: Would it matter if I add a full HTML code(with meta viewport, no header, but body and divs) inside the ion-view ?

I wanted to get similar functionality and was able to get it to work using ion-scroll .

<ion-scroll zooming="true" direction="xy" style="width: 100%; ">
  <div></div>
</ion-scroll>

Another solution could be to use hammerjs for multi-touch gestures and map pinch out and pinch in to zoom the content using css.

Something like this:

var hammerTime = new Hammer.Manager($('div.youWantToScale')[0], {touchAction: "pan-x pan-y"});
var pinch = new Hammer.Pinch();
var lastZoom;
hammerTime.add(pinch);
hammerTime.on("pinchout pinchin", function(e) {
   var currentZoom = Number($('div.youWantToScale').css("zoom"));
   if (lastZoom) {
      var newZoom = currentZoom + (e.scale - lastZoom);

      //bounds checking for your zoom, min=1 and max=3 here
      newZoom = newZoom < 1 ? 1 : (newZoom > 3 ? 3 : newZoom); 
      $('div.youWantToScale').css("zoom", newZoom);
   } 
   lastZoom = e.scale;
});

hammerTime.on("pinchstart", function (e) {
   //Seems clunky, but reset the lastZoom on a new pinch   
   lastZoom = undefined;
});

Thanks to @laughinpine above, I came up with this solution in Ionic 4:

<ion-content scrollX scrollY>
  <div #map style="background-image: url('/assets/images/map.png');">
  </div>
</ion-content>


import * as Hammer from 'hammerjs';

@Component({
  ...
})
export class MapPage
  @ViewChild('map') public map: ElementRef;

  private readonly minZoom: number = 0.3;
  private readonly maxZoom: number = 2.0;
  private readonly zoomVelocity: number = 0.005;

  public constructor(private renderer: Renderer2) {}

  private addPinchToMap(): void {
    const hammerTime = new Hammer.Manager(this.map.nativeElement, { touchAction: 'pan-x pan-y' });
    const pinch = new Hammer.Pinch();
    let lastZoom: number = 1; // Default original zoom

    hammerTime.add(pinch);

    hammerTime.on('pinchin', (event: MSGestureEvent) => {
      let newZoom = lastZoom - this.zoomVelocity;
      newZoom = newZoom < this.minZoom ? this.minZoom : newZoom;
      this.renderer.setStyle(this.map.nativeElement, 'zoom', newZoom);
      lastZoom = newZoom;
    });

    hammerTime.on('pinchout', (event: MSGestureEvent) => {
      let newZoom = lastZoom + this.zoomVelocity;
      newZoom = newZoom > this.maxZoom ? this.maxZoom : newZoom;
      this.renderer.setStyle(this.map.nativeElement, 'zoom', newZoom);
      lastZoom = newZoom;
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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