sticky-section.uvue 4.2 KB
Newer Older
雪洛's avatar
雪洛 已提交
1
<template>
shutao-dc's avatar
shutao-dc 已提交
2
  <page-head title="sticky-section"></page-head>
雪洛's avatar
雪洛 已提交
3
  <list-view id="list-view" ref="list-view" show-scrollbar=false class="page" :scroll-into-view="scrollIntoView"
4
  @scroll="onScroll" @scrollend="onScrollEnd" bounces="false">
雪洛's avatar
雪洛 已提交
5 6
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type = 20>
      <button @click="gotoStickyHeader('C')" size="mini">跳转到id为C的sticky-header位置上</button>
7 8
    </list-item>
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type = 20>
9 10 11 12
      <button @click="appendSectionItem(0)" size="mini">第一组 section 新增5条内容</button>
    </list-item>
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type = 20>
      <button @click="deleteSection()" size="mini">删除第一组 section</button>
13
    </list-item>
14
    <sticky-section v-for="(section) in sectionArray" :key="section.name" :padding="sectionPadding" :push-pinned-header="true">
15 16
      <sticky-header :id="section.name">
        <text class="sticky-header-text">{{section.name}}</text>
W
wanganxp 已提交
17
      </sticky-header>
18
      <list-item v-for="(list) in section.list" :key="list.text" :name="list.text" class="content-item" :type=10>
19
        <text class="text">{{list.text}}</text>
W
wanganxp 已提交
20 21
      </list-item>
    </sticky-section>
雪洛's avatar
雪洛 已提交
22
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type= 30>
W
wanganxp 已提交
23 24 25 26 27 28
      <!-- <text style="color: #aaa">到底了</text> -->
      <button @click="toTop" size="mini">回到顶部</button>
    </list-item>
  </list-view>
</template>

29 30 31 32 33 34 35 36
<script>
  export type sectionData={
  	name:string,
  	list:sectionListItem[]
  }
  export type sectionListItem ={
    text:string
  }
W
wanganxp 已提交
37 38 39
  export default {
    data() {
      return {
40
        data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
雪洛's avatar
雪洛 已提交
41 42
        sectionPadding: [0, 10, 0, 10] as Array<number>,
        scrollIntoView: "",
43 44 45
        scrolling: false,
        sectionArray:[] as sectionData[],
        appendId: 0
W
wanganxp 已提交
46
      }
47 48 49 50 51 52 53 54 55 56 57 58
    },
    onLoad() {
      this.data.forEach(key=> {
        const list =[] as sectionListItem[]
        for(let i = 1; i<11; i++) {
          const item = {text: key+"--item--content----"+i} as sectionListItem
          list.push(item)
        }
        const data = {name: key, list: list} as sectionData
        this.sectionArray.push(data)
      }
      )
W
wanganxp 已提交
59
    },
雪洛's avatar
雪洛 已提交
60 61 62 63
    methods: {
      toTop(){
        this.scrollIntoView = ""
        uni.getElementById("list-view")!.scrollTop = 0
W
wanganxp 已提交
64 65 66
      },
      //用于自动化测试
      listViewScrollByY(y : number) {
DCloud-yyl's avatar
DCloud-yyl 已提交
67
        const listview = this.$refs["list-view"] as UniElement
shutao-dc's avatar
shutao-dc 已提交
68 69
        // listview.scrollBy(0, y)
        listview.scrollTop = y
雪洛's avatar
雪洛 已提交
70
      },
71 72 73 74 75 76 77
      gotoStickyHeader(id : string) {
        // #ifdef APP
        this.scrollIntoView = id
        // #endif
        // #ifdef WEB
        console.log("web端不支持该功能")
        // #endif
雪洛's avatar
雪洛 已提交
78 79 80 81 82 83 84 85 86
      },
      onScroll() {
        this.scrolling = true
      },
      onScrollEnd() {
        this.scrolling = false
        //滚动后重置scrollIntoView = ""
        if(this.scrollIntoView != "") {
          this.scrollIntoView = ""
shutao-dc's avatar
shutao-dc 已提交
87
        }
88 89 90 91 92 93 94 95 96 97 98 99
      },
      appendSectionItem(index: number) {
        const data = this.sectionArray[index]
        this.appendId++
        const list = [
          {text: data.name+"--item--content----new1--"+this.appendId} as sectionListItem,
          {text: data.name+"--item--content----new2--"+this.appendId} as sectionListItem,
          {text: data.name+"--item--content----new3--"+this.appendId} as sectionListItem,
          {text: data.name+"--item--content----new4--"+this.appendId} as sectionListItem,
          {text: data.name+"--item--content----new5--"+this.appendId} as sectionListItem
        ] as sectionListItem[]
        data.list.unshift(...list)
100 101 102
      },
      deleteSection() {
        this.sectionArray.shift()
W
wanganxp 已提交
103 104 105 106 107 108
      }
    }
  }
</script>

<style>
shutao-dc's avatar
shutao-dc 已提交
109
  .page {
W
wanganxp 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
    flex: 1;
    background-color: #f5f5f5;
  }

  .sticky-header-text {
    font-size: 16px;
    padding: 8px;
    color: #959595;
    background-color: #f5f5f5;
  }

  .content-item {
    padding: 15px;
123
    margin-bottom: 10px;
W
wanganxp 已提交
124 125
    background-color: #fff;
  }
雪洛's avatar
雪洛 已提交
126
</style>