sticky-section.uvue 2.3 KB
Newer Older
雪洛's avatar
雪洛 已提交
1
<template>
2
  <page-head title="sticky-section"></page-head>
雪洛's avatar
雪洛 已提交
3 4 5 6
  <list-view id="list-view" ref="list-view" show-scrollbar=false class="page" :scroll-into-view="scrollIntoView"
  @scroll="onScroll" @scrollend="onScrollEnd" rebound="false">
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type = 20>
      <button @click="gotoStickyHeader('C')" size="mini">跳转到id为C的sticky-header位置上</button>
shutao-dc's avatar
shutao-dc 已提交
7
    </list-item>
W
wanganxp 已提交
8
    <sticky-section v-for="(sectionText) in data" :padding="sectionPadding" :push-pinned-header="true">
shutao-dc's avatar
shutao-dc 已提交
9
      <sticky-header :header-id="sectionText" :id="sectionText">
W
wanganxp 已提交
10 11 12 13 14 15
        <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
雪洛 已提交
16
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type= 30>
W
wanganxp 已提交
17 18 19 20 21 22 23 24 25 26 27
      <!-- <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
雪洛 已提交
28 29
        sectionPadding: [0, 10, 0, 10] as Array<number>,
        scrollIntoView: "",
30
        scrolling: false
W
wanganxp 已提交
31 32
      }
    },
雪洛's avatar
雪洛 已提交
33 34 35 36
    methods: {
      toTop(){
        this.scrollIntoView = ""
        uni.getElementById("list-view")!.scrollTop = 0
W
wanganxp 已提交
37 38 39
      },
      //用于自动化测试
      listViewScrollByY(y : number) {
40
        const listview = this.$refs["list-view"] as Element
W
wanganxp 已提交
41
        listview.scrollBy(0, y)
雪洛's avatar
雪洛 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
      },
      gotoStickyHeader(id : string) {
        this.scrollIntoView = id
      },
      onScroll() {
        this.scrolling = true
      },
      onScrollEnd() {
        this.scrolling = false
        //滚动后重置scrollIntoView = ""
        if(this.scrollIntoView != "") {
          this.scrollIntoView = ""
        }
W
wanganxp 已提交
55 56 57 58 59 60
      }
    }
  }
</script>

<style>
shutao-dc's avatar
shutao-dc 已提交
61
  .page {
W
wanganxp 已提交
62 63 64 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;
    margin: 5px 0;
    background-color: #fff;
  }
雪洛's avatar
雪洛 已提交
78
</style>