【组件篇】ionic3分组索引及锚点滚动列表

好久没有写文章了,趁着吃完饭消化的时候打算写一篇。先前一篇文章提到并关注的capacitor终于出到1.0.0-alpha.5了,本想写它,但是内容比较多,所以先放一下,写别的。

先前写过另一篇文章《ionic3开源组件》,里面有一个分类我其实没怎么用过:

锚点滚动列表

其中有不少人私信我,说ionic3-index-list有问题,我没理,今天又有人和我说,于是我简单看了下源码,没发现什么问题(后来发现个原有组件不能动态刷新锚点栏的Bug),只是觉得它写得有点复杂了,和现有ionic的组件集成度还没那么好(如不能很好兼容使用单选和多选列表),所以花了几分钟,在大部分沿用原来代码的基础下,简单改动了下:

  1. 移除多余的ion-index-cell;
  2. ion-index-section修改为index-group,并替换为官方list的相关组件;
  3. 修改锚点滚动逻辑;
  4. 修改原有组件不能动态刷新锚点栏的bug;

代码为index-list(原来代码基本没动,只改动锚点滚动逻辑)和index-group(重新实现)共两个组件,所以会发现两种不同的代码风格。

index-list组件

index-list.html:

<!-- Generated template for the IndexListComponent component -->
<div class="index-list">
  <div class="index-list-wrapper" #scrollContent tappable (scroll)="onScroll($event)">
    <ng-content></ng-content>
  </div>
  <div class="index-list-nav" (touchstart)="touchstart($event)" (touchmove)="touchmove($event)" (touchend)="touchend($event)">
    <div class="index-bar" *ngFor="let index of _indexes;" [class.index-list-nav-activate]="index === _currentIndicator">
      {{index}}
    </div>
  </div>
  <div class="modal" [class.show]="_showModal">
    {{_currentIndicator}}
  </div>
</div>

index-list.scss:

index-list {
    ::-webkit-scrollbar {
        width: 0
      }
      .index-list{
        width: 100%;
        display: flex;
        justify-content: space-between;
        height: 100%;
        overflow: hidden;
      }
      .index-list-wrapper{
        width: 100%;
        overflow-y: scroll;
        -webkit-overflow-scrolling: touch;
      }
      .index-list-nav{
        width:6%;
        position: absolute;
        right: 0;
        display: flex;
        justify-content:center;
        flex-direction: column;
        text-align: center;
        background-color: rgba(245, 245, 245, 0.3);
        height: 100%;
        z-index: 1000;
        -webkit-touch-callout: none;
      }
      .index-bar{
        padding: 2px 6px;
        font-size: 12px;
      }
      .index-list-nav-activate{
        color: red;
      }
      .modal {
        top: 50%;
        left: 50%;
        z-index: 100;
        position: fixed;
        pointer-events: none;
        width: 20vw;
        height: 20vw;
        line-height: 20vw;
        margin-left: -10vw;
        margin-top: -10vw;
        color: #fff;
        font-size: 3em;
        text-align: center;
        border-radius: 8px;
        background-color: rgba(0, 0, 0, 0.52);
        -webkit-box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.16);
        box-shadow: 0 0 4px 1px rgba(0, 0, 0, 0.16);
        -webkit-transition: opacity .5s;
        -o-transition: opacity .5s;
        transition: opacity .5s;
        opacity: 0;
      }
      .modal.show {
        opacity: 1;
      }
}

index-list.ts:

import { Component, ElementRef, ViewChild, ContentChildren, QueryList } from '@angular/core';
import { IndexGroupComponent } from './index-group';

/**
 * Generated class for the IndexListComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'index-list',
  templateUrl: 'index-list.html'
})
export class IndexListComponent {
  _indexes: any[] = []; //右侧导航
  _currentIndicator= '';
  _flag= true;
  _offsetTops: Array<number> = [];
  _navOffsetX: 0;
  _indicatorTime: any = null;
  _showModal = false;

  @ContentChildren(IndexGroupComponent) _listOfIndexSection: QueryList<IndexGroupComponent>;
  @ViewChild('scrollContent') scrollContent: ElementRef;
  constructor() {
    console.log('Hello IndexListComponent Component');
  }

  ngAfterContentChecked(){
    this.resetIndicator();
  }

  resetIndicator(){
    if (this._flag && this._listOfIndexSection && this._listOfIndexSection.length > 0){
      this._indexes = [];
      this._offsetTops = [];
      let tempIndexs: any[] = [];
      this._listOfIndexSection.forEach((section: any) => {
        tempIndexs.push(section.index);
        const offsetTop = section.getOffsetTop();
        this._offsetTops.push(offsetTop);
        console.log(section);
      });
      this._indexes = tempIndexs;
      if(this._indexes.length>0){
        this._currentIndicator = this._indexes[0];
      }
      this._flag = false;
    }
  }

  onScroll(e:any) {
    e.preventDefault();
    const scrollTopOffsetTop = this.scrollContent.nativeElement.scrollTop;
    this._offsetTops.forEach((v, i) => {
      if (scrollTopOffsetTop >= v){
        this._currentIndicator = this._indexes[i];
      }
    });
  }

  touchstart(e:any){
    this._navOffsetX = e.changedTouches[0].clientX;
    this.scrollList(e.changedTouches[0].clientY);
  }

  touchmove(e:any){
    e.preventDefault();
    this.scrollList(e.changedTouches[0].clientY);
  }

  touchend(e:any){
    this._indicatorTime = setTimeout(() => {
      this._showModal = false;
    }, 500);
  }

  scrollList(y:any){
    const currentItem:any = document.elementFromPoint(this._navOffsetX, y);
    if (!currentItem || !currentItem.classList.contains('index-bar')) {
      return;
    }
    this._currentIndicator = currentItem['innerText'];
    this.scrollIntoView(this._currentIndicator);
  }

  scrollIntoView(id: string){
    let element = document.getElementById(id);
    if(element){
      element.scrollIntoView();
      this._showModal = true;
      if (this._indicatorTime) {
        clearTimeout(this._indicatorTime);
      }
    }
  } 
}

特别注意一下,这里使用ngAfterContentChecked生命周期函数,在内部content变更后再去计算内容生成侧边栏。

index-group组件

index-group.html:

<ion-item-group id="{{index}}">
    <ion-item-divider #header color="{{headColor}}">{{index}}</ion-item-divider>
    <ng-content></ng-content>
</ion-item-group>

index-group.scss不需要
index-group.ts:

import { Component, Input, ViewChild, ElementRef } from '@angular/core';
import { ItemDivider } from 'ionic-angular/components/item/item-divider';
@Component({
  selector: 'index-group',
  templateUrl: 'index-group.html'
})
export class IndexGroupComponent {

  @Input() index: string = 'A';
  @Input() headColor: string = 'light';
  @ViewChild('header') header: ItemDivider;
  constructor() {
  }
  getOffsetTop(){
    return this.header.getNativeElement().offsetTop;
  }
}

示范使用

然后就可以用了,示范用法如下:

<index-list radio-group [(ngModel)]="vm.selectedValue">
    <index-group *ngFor="let group of groups" [index]="group.key">
      <ion-item *ngFor="let item of group.items">
        <ion-label>{{item.name || item.title}}</ion-label>
        <ion-radio value="{{item.value}}"></ion-radio>
      </ion-item>
    </index-group>
  </index-list>

总结

因为只花了一点时间来改,可能有bug,若有,给我留言。另外,index-list其实应该可以再精简的,只是我目前懒得花时间去改了,留给你们谁有兴趣再改吧。源码放在了ionic-components中。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容

  • 平时会留意一些开源组件,收藏到收藏夹里,然后一天mac电脑因为卸载window出问题导致不能不重装,然后里面的东西...
    IT晴天阅读 21,459评论 22 143
  • 本系列文集:来扯点ionic3 表单几乎是每一个应用程序中必不可少的一部分,在各类 HTML 教材中,表单也经常被...
    忠叔阅读 7,732评论 5 20
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,409评论 0 17
  • 大雾弥漫在眼前,浓重,我抬眼看不到那些训练已久,穿着酷似高中生们的方队成员,主席台的女声从遥远的我看不到的地...
    七千年笛声阅读 225评论 0 0
  • 职业习惯是指一个人长期从事某种职业而养成的那种极富职业特点的言谈举止。良好职业习惯的养成是建设职业化队伍必不可少的...
    甲坤阅读 414评论 0 1