Programmatically scroll ion-segment

…衆ロ難τιáo~ 提交于 2019-12-06 03:55:19

Interesting question.

It's not supported out of the box. I'm not sure if it's possible to do because, as I understand it, there is no way to get at the insides of the ion-segment web component unless there is an API exposed.

This encapsulation is how the components can be dropped into the page and not conflict with other stuff.

If it's super important to your project you could consider creating your own segment from copying the code of the segment and segment-button components into your own separate version?

Check this out, it should help Ionic 4 Segments and Slides

The .html can be like this:

<ion-app>
  <ion-header>
   <ion-toolbar color="primary">
    <ion-title>Ionic 4 Segment</ion-title>
   </ion-toolbar>
  <ion-toolbar color="primary">
  <ion-segment scrollable>
    <ion-segment-button value="0" checked>
      <ion-icon name="call"></ion-icon>
      <ion-label>Call</ion-label>
    </ion-segment-button>
    <ion-segment-button value="1">
      <ion-icon name="cloud-upload"></ion-icon>
      <ion-label>Publish</ion-label>
    </ion-segment-button>
    <ion-segment-button value="2">
      <ion-icon name="paper"></ion-icon>
      <ion-label>Topic</ion-label>
    </ion-segment-button>
    <ion-segment-button value="3">
      <ion-icon name="code-working"></ion-icon>
      <ion-label>Query</ion-label>
    </ion-segment-button>
    <ion-segment-button value="4">
      <ion-icon name="open"></ion-icon>
      <ion-label>Open</ion-label>
    </ion-segment-button>
    <ion-segment-button value="5">
      <ion-icon name="search"></ion-icon>
      <ion-label>Search</ion-label>
    </ion-segment-button>
    <ion-segment-button value="6">
      <ion-icon name="create"></ion-icon>
      <ion-label>Write</ion-label>
    </ion-segment-button>
    <ion-segment-button value="7">
      <ion-icon name="book"></ion-icon>
      <ion-label>Read</ion-label>
    </ion-segment-button>
    <ion-segment-button value="8">
      <ion-icon name="trash"></ion-icon>
      <ion-label>Trash</ion-label>
    </ion-segment-button>
   </ion-segment>
  </ion-toolbar>
 </ion-header>

<ion-content>
 <ion-slides>
  <ion-slide class="slide-1">
    Call
  </ion-slide>
  <ion-slide class="slide-2">
    Publish
  </ion-slide>
  <ion-slide class="slide-3">
    Topic
  </ion-slide>
  <ion-slide class="slide-1">
    Query
  </ion-slide>
  <ion-slide class="slide-2">
    Open
  </ion-slide>
  <ion-slide class="slide-3">
    Search
  </ion-slide>
  <ion-slide class="slide-1">
    Write
  </ion-slide>
  <ion-slide class="slide-2">
    Read
  </ion-slide>
  <ion-slide class="slide-3">
    Trash
  </ion-slide>
   </ion-slides>
  </ion-content>
 </ion-app>

Then the js like this

var segment = document.querySelector('ion-segment');
var slides = document.querySelector('ion-slides');

segment.addEventListener('ionChange', (ev) => onSegmentChange(ev));
slides.addEventListener('ionSlideDidChange', (ev) => 
 onSlideDidChange(ev));

// On Segment change slide to the matching slide
function onSegmentChange(ev) {
 slideTo(ev.detail.value);
}

function slideTo(index) {
 slides.slideTo(index);
}

// On Slide change update segment to the matching value
async function onSlideDidChange(ev) {
 var index = await slides.getActiveIndex();
 clickSegment(index);
}

function clickSegment(index) {
  segment.value = index;
}

I hope this helps anyone

I am also facing the same issue and finally solved the issue with below code.

you must define the unique "id" for each "ion-segment-button"

<ion-segment-button SwipedTabs *ngFor="let category of tabs ; let i = index"
        value={{category}} (click)="selectTab(i)" **id="segment-{{i}}**">
        <ion-label>{{category}}</ion-label>
      </ion-segment-button>

.ts code 
async slideChanged() {
    this.activeIndex= await this.slider.getActiveIndex();
      document.getElementById("segment-" + activeIndex).scrollIntoView({
      behavior: 'smooth',
      block: 'center',
      inline: 'center'
    });
}

hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!