ScrollTo particular item of ListiVew in Ionic 2

2020-06-03 07:57发布

问题:

I want to scrollTo a particular item in listview in ionic 2. I have a listview which is bound to array.

export class TestPage {
    @ViewChild(Content) content: Content;
    public items: Array<Object> = [];

    ionViewWillEnter() {
        console.log(this.navParams.data);
        this.customService.getArray(this.params.index).then((items) => {
            this.items = items;
                //scroll to item
                // this.content.scrollTo()
        });
     }
}

Here is the view:

<ion-list [virtualScroll]="items" approxItemHeight="120px" approxItemWidth="100%" 
    class="items-listview list list-md">
  <button class="items-listview-item item item-md" *virtualItem="let item">
    <div class="items-listview-text">
        {{ item.text }}<span>{{item.index}}</span>
    </div>
  </button>
</ion-list>

I see that scrollTo only support position i.e top and left but not the element itself. How can i scrollTo listview item (e. item no 150) itself ? How can i get the position of item no 150 and pass it to scrollTo?

回答1:

You can assign an id to each item (by doing something like [id]="'item' + item.index" and then in your code just use that id to get the offsetTop:

scrollTo(elementId:string) {
    let yOffset = document.getElementById(elementId).offsetTop;
    this.content.scrollTo(0, yOffset, 4000)
}


回答2:

The current accepted answer only scrolled relative to the parent element, so here is what I came up with to scroll to the selected element (without traversing the DOM).

  scrollTo(element:string) {
    let elem = document.getElementById(element);
    var box = elem.getBoundingClientRect();

    var body = document.body;
    var docEl = document.documentElement;

    var scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop;
    var clientTop = docEl.clientTop || body.clientTop || 0;
    var top  = box.top +  scrollTop - clientTop;
    var cDim = this.content.getContentDimensions();

    var scrollOffset = Math.round(top) + cDim.scrollTop - cDim.contentTop;

    this.content.scrollTo(0, scrollOffset, 500);
  }


回答3:

I am posting the solution i come up with. First you need to give unique id to each listview item and then select the ListView

@ViewChild(VirtualScroll) listView: VirtualScroll;

After that i created a function (following) ScrollTo which has a timeout of resizing the listview after scrolling too (as i was changing buffer ratio dynamically).

private scrollTo(index: number) {
    let key = '#customIds_' + index;

    let hElement: HTMLElement = this.content._elementRef.nativeElement;
    let element = hElement.querySelector(key);
    element.scrollIntoView();

    //wait till scroll animation completes
    setTimeout(() => {
        //resize it! otherwise will not update the bufferRatio
        this.listView.resize();
    },500);
}

Last i just called this function after a second delay as i waited till listview loads:

//must be delay otherwise content scroll doesn't go to element properly..magic!
setTimeout(() => {
    this.scrollTo('someId');
}, 1000);