sticky-section.uvue 4.3 KB
Newer Older
雪洛's avatar
雪洛 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
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"
DCloud-WZF's avatar
DCloud-WZF 已提交
4 5
    @scroll="onScroll" @scrollend="onScrollEnd" bounces="false">
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
雪洛's avatar
雪洛 已提交
6
      <button @click="gotoStickyHeader('C')" size="mini">跳转到id为C的sticky-header位置上</button>
7
    </list-item>
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9 10 11 12 13 14 15
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
      <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>
    </list-item>
    <sticky-section v-for="(section) in sectionArray" :key="section.name" :padding="sectionPadding"
      :push-pinned-header="true">
16 17
      <sticky-header :id="section.name">
        <text class="sticky-header-text">{{section.name}}</text>
W
wanganxp 已提交
18
      </sticky-header>
19
      <list-item v-for="(list) in section.list" :key="list.text" :name="list.text" class="content-item" :type=10>
20
        <text class="text">{{list.text}}</text>
W
wanganxp 已提交
21 22
      </list-item>
    </sticky-section>
DCloud-WZF's avatar
DCloud-WZF 已提交
23
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=30>
W
wanganxp 已提交
24 25 26 27 28 29
      <!-- <text style="color: #aaa">到底了</text> -->
      <button @click="toTop" size="mini">回到顶部</button>
    </list-item>
  </list-view>
</template>

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

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

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

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