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

<script>
  export default {
    data() {
      return {
        data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
雪洛's avatar
雪洛 已提交
30 31
        sectionPadding: [0, 10, 0, 10] as Array<number>,
        scrollIntoView: "",
32
        scrolling: false
W
wanganxp 已提交
33 34
      }
    },
雪洛's avatar
雪洛 已提交
35 36 37 38
    methods: {
      toTop(){
        this.scrollIntoView = ""
        uni.getElementById("list-view")!.scrollTop = 0
W
wanganxp 已提交
39 40 41
      },
      //用于自动化测试
      listViewScrollByY(y : number) {
DCloud-yyl's avatar
DCloud-yyl 已提交
42
        const listview = this.$refs["list-view"] as UniElement
shutao-dc's avatar
shutao-dc 已提交
43 44
        // listview.scrollBy(0, y)
        listview.scrollTop = y
雪洛's avatar
雪洛 已提交
45 46 47 48 49 50 51 52 53 54 55 56
      },
      gotoStickyHeader(id : string) {
        this.scrollIntoView = id
      },
      onScroll() {
        this.scrolling = true
      },
      onScrollEnd() {
        this.scrolling = false
        //滚动后重置scrollIntoView = ""
        if(this.scrollIntoView != "") {
          this.scrollIntoView = ""
shutao-dc's avatar
shutao-dc 已提交
57
        }
W
wanganxp 已提交
58 59 60 61 62 63
      }
    }
  }
</script>

<style>
shutao-dc's avatar
shutao-dc 已提交
64
  .page {
W
wanganxp 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
    flex: 1;
    background-color: #f5f5f5;
  }

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

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