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

<style>
shutao-dc's avatar
shutao-dc 已提交
60
  .page {
W
wanganxp 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    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;
  }
shutao-dc's avatar
shutao-dc 已提交
77
</style>