使用 typeScript 滚动到我的 webView 上的 x,y 坐标

Scroll to an x,y coordinate on my webView using typeScript

我正在我的应用程序中制作自定义地图。这本质上是一个大地图图像,我根据 gps 位置在大地图图像上移动一个小头像图像。

我允许用户在地图上滚动以查看屏幕外的地方。

我现在想添加一个按钮,让用户回到他们所在的位置。但它不起作用,我尝试使用:

  window.moveTo()
  window.scrollTo()

但什么也没发生。有人可以帮忙吗?

html

 <ion-content padding id=ionScroll scrollX="true" scrollY="true">

  <div id = "main" >
    <img class = "map"
    id = "map"
    src = "../../assets/img/limerickmap.JPG"
    alt = "map"/> 
    <img class = "avatar"
    id = "avatar"
    src = "../../assets/img/satan.png"
    alt = "avatar" />
  </div>
  <ion-fab vertical="bottom" horizontal="end" slot="fixed">
    <ion-fab-button (click) = "centreMap()">
      <ion-icon name="compass"></ion-icon>
    </ion-fab-button>
  </ion-fab>

   </ion-content>

打字稿

 ngOnInit() {
      ionScroll = window.document.getElementById("ionScroll");
 }

 centreMap() {
     ionScroll.scrollTo(avatar.style.left , avatar.style.top);
     console.log("TEST scroll" +avatar.style.left+"  "+avatar.style.top)
 }

尝试设置 id 不是我认为的正确方法。

Freaky Jolly tutorial explaining how to scroll to an X/Y coord.

首先,您需要 scrollEventsion-content:

<ion-header>
  <ion-toolbar>
    <ion-title>
      Ion Content Scroll
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content [scrollEvents]="true">
  <div style="height: 50px;width:600px;" text-right>
    <ion-button (click)="ScrollToPoint(-300, -120)">
      Scroll To Point Right
    </ion-button>
  </div>
</ion-content>

在代码中您需要使用 @ViewChild 来获取对 ion-content 的代码引用,然后您可以使用它的 ScrollToPoint() api:

import { Component, ViewChild } from '@angular/core';
import { IonContent } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  @ViewChild(IonContent) content: IonContent;

  constructor(
  ){
  }

  ScrollToPoint(X,Y){
    this.content.scrollToPoint(X,Y,1500);
  }
}

我已经简化了这篇文章,所以如果您想让它真正起作用,您需要在其中添加一些内容来向页面添加滚动条。