index.js 2.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
import TouchEvent from "./utils/touchEvent";
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true
  },
  behaviors: [],
  properties: {
    datas: {
      type: Object
    }
  },
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
    isLoading: false,
    swiperCurIndex: 0,
    slideClass: "",
    lockSwipe: false
  },
  // 数据监听器
  observers: {},
  // 组件生命周期
  lifetimes: {
    created() {
      new TouchEvent(this, "touchCard", {
        swipe: evt => {
          //在touch结束触发,evt.direction代表滑动的方向 ['Up','Right','Down','Left']
          if (evt.direction === "Left") this.next(evt);
          if (evt.direction === "Right") this.prev(evt);
        }
      });
    },
    attached() {}
  },

  // 组件方法
  methods: {
    next(e) {
      if (this.data.lockSwipe) return;
      this.data.lockSwipe = true;
      if (-this.data.swiperCurIndex >= this.data.datas.componentData.goodsList.length - 1) {
        return (this.data.lockSwipe = false);
      }
      const index = e.currentTarget.dataset["index"];
      this.setData(
        { ["datas.componentData.goodsList[" + index + "].slideClass"]: " ani-slide-up" },
        () => {
          this.setData({
            swiperCurIndex: --this.data.swiperCurIndex
          });
        }
      );
      setTimeout(() => {
        this.data.lockSwipe = false;
        this.setData({
          ["datas.componentData.goodsList[" + index + "].slideClass"]: ""
        });
      }, 590);
    },
    prev(e) {
      const index = e.currentTarget.dataset["index"] - 1;
      if (this.data.lockSwipe || index < 0) return;
      this.data.lockSwipe = true;
      this.setData({
        ["datas.componentData.goodsList[" + index + "].slideClass"]: " ani-slide-down",
        swiperCurIndex: ++this.data.swiperCurIndex
      });
      setTimeout(() => {
        this.data.lockSwipe = false;
        this.setData({
          ["datas.componentData.goodsList[" + index + "].slideClass"]: ""
        });
      }, 590);
    }
  },
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data;

      // show事件发生前,页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true
      });

      // 清除定时器等操作
    },
    // 页面尺寸变化时
    resize() {}
  }
};

Component(componentOptions);