如何在 ionic2 中使用 content.scrollTo() 进行离子滚动?

How can I use content.scrollTo() for ion-scroll in ionic2?

我尝试滚动到固定位置,例如 scrollTo(500, 20)。假设您使用的设备宽度为 300 像素。滚动目标现在超出屏幕范围,您必须向右滚动。

我通过执行以下操作解决了这个问题:

<ion-content>
    <ion-scroll scrollX="true" style="width:100%; height:100%; white-space: nowrap; ">
        <div style="width:1000px; ">
            [box1] [box2] [box3] [...]
        </div>
    </ion-scroll>
</ion-content>

到目前为止一切都很好。例如,如果我想通过按下按钮向右跳转 500 像素,问题就开始了。滑动到正确的作品。我知道有一个函数可以为 <ion-content>:

@ViewChild(Content) content: Content;
[...]
this.content.scrollTo(500, 500, 500); 

不幸的是,这对我来说不起作用。我认为问题在于内容与设备大小有关,因此 scrollTo() 方法对 <ion-scoll> 没有影响。如何将 scrollTo() 方法用于 <ion-scroll> 而不是 <ion-content>

感谢您的帮助!

为什么不首先获取要滚动到的元素的尺寸?在 html 文件中为你的 ion-scroll 分配一个 id,然后你可以 :

scrollToElement(id) { 
    var el = document.getElementById(id);
    var rect = el.getBoundingClientRect();
    // scrollLeft as 0px, scrollTop as "topBound"px, move in 800 milliseconds
    this.content.scrollTo(0, rect.top, 800);
}

来自this documentation: getBoundingClientRect 的返回值 left、top、right、bottom、x、y、width、height 属性以像素为单位描述边框。宽度和高度以外的属性相对于视口的左上角。

看看你的代码,我认为你不需要 ion-scroll,你应该能够只为你的 div 分配一个 id 并得到你想要的结果。

How can I use the scrollTo() method for <ion-scroll> instead of <ion-content>?

我仍在研究如何为滚动设置动画,但至少这可以被视为适合您的场景的解决方案。请看this plunker.

由于我们不能使用 ion-content 进行滚动,我考虑获取 Scroll 的实例,然后访问内部 html 滚动元素,然后使用 element.scrollLeft 属性 在该元素上滚动:

The element.scrollLeft property gets or sets the number of pixels that an element's content is scrolled to the left.

所以在组件代码中:

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

@Component({...})
export class HomePage {
  @ViewChild('scroll') scroll: any;

    constructor() {}

    public backToStart(): void {
      this.scroll.scrollElement.scrollLeft = 0;
    }

    public scrollToRight(): void {
      this.scroll.scrollElement.scrollLeft = 500;
    }

}

并且在视图中:

<ion-content padding>
  <ion-scroll #scroll scrollX="true" style="width:100%; height:150px; white-space: nowrap;">
        <div  style="width:1000px;">
            <div style="display:inline-block;height:100px;width:100px;border:1px solid black;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid red;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid blue;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid green;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid grey;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid brown;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid yellow;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid orange;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid pink;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid violet;"></div>
        </div>
    </ion-scroll>

    <button (click)="backToStart()" ion-button text-only>Go to start</button>
    <button (click)="scrollToRight()" ion-button text-only>Scroll to right!</button>
</ion-content>

通过this.scroll.scrollElement.scrollLeft = 500;我们可以将ion-scroll的内容向右滚动500px。然后我们可以通过 this.scroll.scrollElement.scrollLeft = 0;.

再次回到开始

通过内容滚动

@ViewChild(Content) content: Content;
  this.content.scrollTo 

通过 id #scroll

@ViewChild('scroll') scroll: any;  
this.scroll.scrollElement.scrollLeft = 500;

上面的方法对于上下滚动效果很好,但是在 horizontal/scrollX 的情况下不起作用,所以通过下面的代码我解决了我的解决方案,希望它对您有所帮助。

TypeScript

scrollmain(elementId, index) {
   console.info('elementId', elementId)
   var el = document.getElementById(elementId);
   el.scrollIntoView({ behavior: "smooth" });
}

HTML

<ion-scroll #scroll scrollX="true" style="width:100%;white-space: 
          nowrap; height: 60px">
    <ion-segment *ngIf="mcqdata" color="appmaincolor">
        <ion-segment-button *ngFor="let mcq of mcqdata; let i=index;" [id]="mcq.ques_id" value="{{mcq.ques_id}}" (ionSelect)="scrollmain(mcq.ques_id,i)"
            [ngClass]="{'active': selectedIdx == i}">
            <strong>Queastion{{i+1}}</strong>
        </ion-segment-button>
    </ion-segment>
</ion-scroll>

添加到上面的答案中,这将处理平滑滚动。

在您的 .html

中命名滚动元素
<ion-scroll #scroll>

导入卷轴。

import { Scroll } from 'ionic-angular';

在 .ts 中引用滚动条

@ViewChild('scroll') scroll: any;

在需要的地方添加此代码:

this.scroll._scrollContent.nativeElement.scrollTo({ left: 0, top: 0, behavior: 'smooth' });

希望对某人有所帮助。

您可以将 this.content.scrollTo(...) 替换为 this.content._scrollContent.nativeElement.scrollTo(...)