my-scroll-view.js 1.38 KB
import wx from '../../utils/wx';

let systemInfo = wx.getSystemInfoSync();

Component({
  properties: {
    isTop: { // 是否展示返回顶部按钮
      type: Boolean,
      value: false
    },
    isShare: { // 是否显示分享
      type: Boolean,
      value: false
    }
  },
  data: {
    isTopShow: true,
    indicatorAnimation: {},
    height: 0,
    scrollTop: 0
  },
  ready: function() {
    this.setData({height: systemInfo.windowHeight});
  },
  methods: {
    tapComponentsMore: function() { // 滚动底部 加载更多 回调
      this.triggerEvent('tapLoadMore', {});
    },
    backToTop: function() { // 返回顶部
      this.setData({scrollTop: 0});
    },
    onScroll: function(e) {
      if (this.data.isTop) { // 返回顶部按钮显示 不显示控制
        let { scrollTop } = e.detail || {};
        let isTopShow = scrollTop > systemInfo.windowHeight * 2 ? true : false;

        if (isTopShow !== this.data.isTopShow) {
          let animtionIndicator = wx.createAnimation({
            duration: 300,
            timingFunction: 'linear',
          });

          if (isTopShow) {
            animtionIndicator.opacity(1).step();
          } else {
            animtionIndicator.opacity(0).step();
          }

          this.setData({
            indicatorAnimation: animtionIndicator.export(),
            isTopShow: isTopShow
          });
        }
      }
    }
  }
});