angularionic-frameworkionic4ion-segment

Ionic 4: How to focus the view of ion-segment when its clicked


There are five ion-segment-button,


Something | Something | Something | Something | Something


If I click the 4th segment the view didnt follow or focus to the current segment.

enter image description here

Above is the current screenshot

I want the view move (to the right or focus it to the center) when the segment is clicked

HTML:

<ion-row>
    <!-- ion-segment -->
    <ion-toolbar>
      <ion-segment (ionChange)="segmentChanged()" [(ngModel)]="segment" color="tertiary" scrollable>
        <ion-segment-button value="0">
          Something
        </ion-segment-button>
        <ion-segment-button value="1">
          Something
        </ion-segment-button>
        <ion-segment-button value="2">
          Something
        </ion-segment-button>
        <ion-segment-button value="3">
          Something
        </ion-segment-button>
        <ion-segment-button value="4">
          Something
        </ion-segment-button>
      </ion-segment>
    </ion-toolbar>

    <ion-col>
      <ion-slides #slides (ionSlideDidChange)="slideChanged()" scrollbar="true" [options]="slideOpts">
        <!-- Something -->
        <ion-slide style="overflow-y: auto">
          <p>Something</p>
        </ion-slide>
        <!-- Something -->
        <ion-slide>
         <p>Something</p>
        </ion-slide>
        <!-- Something -->
        <ion-slide>
          <p>Something</p>
        </ion-slide>
        <!-- Something -->
        <ion-slide>
          <p>Something</p>
        </ion-slide>
        <!-- Something -->
        <ion-slide>
          <p>Something</p>
        </ion-slide>
      </ion-slides>   
    </ion-col>
</ion-row>

TS:

slideOpts = {
    zoom: false
};

@ViewChild('slides') slider: IonSlides;

segment = 0;

async segmentChanged() {
    await this.slider.slideTo(this.segment);
}

async slideChanged() {
    this.segment = await this.slider.getActiveIndex();
    this.slider.update();
}

The found other solution but its only suitable on Ionic 3 or another ionic version.. Thank you so much for reading


Solution

  • It could be done like this, just add an id:

    <ion-segment-button id="something-1" value="0">
        Something
    <ion-segment-button>
    <ion-segment-button id="something-2" value="1">
        Something
    <ion-segment-button>
    <ion-segment-button id="something-3" value="2">
        Something
    <ion-segment-button>
    <ion-segment-button id="something-4" value="3">
        Something
    <ion-segment-button>
    <ion-segment-button id="something-5" value="4">
        Something
    <ion-segment-button>
    

    and then:

    async segmentChanged(event) {
        this.focusSegment(event['srcElement']['children'][this.segment]['id']);
        await this.slider.slideTo(this.segment);
    }
    
    focusSegment(segmentId) {
        document.getElementById(segmentId).scrollIntoView({ 
          behavior: 'smooth',
          block: 'center',
          inline: 'center'
        });
    }
    

    NOTED: This only works in Android as in iOS the WKWebView can't support the scrollIntoView with object parameters. There are possibilities you can put Smooth Scroll behavior polyfill to make it work.

    https://github.com/iamdustan/smoothscroll