angular - 单击时如何将元素滚动到 View 中

标签 angular typescript ionic-framework ionic2 ionic3

我正在寻找类似于 scrollIntoView() 的东西例如,当我点击一个按钮时,在 Ionic2 中使用。

ion-content 中没有详述的方法帮助。

最佳答案

请看 this working plunker

您可以使用 scrollTo(x,y,duration)方法 (docs) . 代码非常简单,首先我们获取目标元素的位置(在本例中为 <p></p>)然后我们在 scrollTo(...) 中使用它方法。首先是 View :

<ion-header>
  <ion-navbar primary>
    <ion-title>
      <span>My App</span>
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>

  <button ion-button text-only (click)="scrollElement()">Click me</button>

  <div style="height: 600px;"></div>

  <!-- Notice the #target in the p element -->
  <p #target>Secret message!</p>

  <div style="height: 600px;"></div>

</ion-content>

以及组件代码:

import { ViewChild, Component } from '@angular/core';
import { NavController, Content } from 'ionic-angular';

@Component({
  templateUrl:"home.html"
})
export class HomePage {
  @ViewChild(Content) content: Content;
  @ViewChild('target') target: any;

  constructor() {   }

  public scrollElement() {
    // Avoid reading the DOM directly, by using ViewChild and the target reference
    this.content.scrollTo(0, this.target.nativeElement.offsetTop, 500);
  }
}

关于angular - 单击时如何将元素滚动到 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39396908/

相关文章:

angular - 如何强制在 angular2 中执行自定义过滤管道

javascript - 从切换列表中获取值

javascript - 我该如何处理 Angular 形式的单选按钮值

angular - 如何捕获 angular(ionic) 中的 http 错误?

angular - Angular 客户端错误跟踪的可用选项?

javascript - 如何知道复选框是否被选中?

javascript - 如何在 Typescript 中为枚举分配多个值?

node.js - Firebase Cloud Functions 与 TypeScript : Realtime Database update ends with success but not updates anything, JS 工作正常

typescript - 如何清除 NextJs GetStaticPaths 缓存/"unpublish"动态路由?

javascript - 在同一个项目中同时使用 coffeescript 和 typescript