index.js 5.31 KB
Newer Older
李嘉林 committed
1 2
const app = getApp();
Component({
3 4 5 6 7 8 9 10 11 12 13 14 15
  // 启用插槽
  options: {
    multipleSlots: true
  },
  properties: {
    note: {
      type: Array
    },
    datas: {
      type: Object
    },
    padding: {
      type: Number
李嘉林 committed
16 17 18 19 20 21
    },
    loading: {
      type: Boolean
    },
    finished: {
      type: Boolean
22 23
    }
  },
李嘉林 committed
24 25 26
  onPageScroll(e) {
    console.log(e);
  },
李嘉林 committed
27 28 29 30 31 32 33
  lifetimes: {
    created() {
      console.log("进入组件created");
      // this.initList();
    },
    attached() {
      console.log("进入组件attached");
34
      // this.initList();
李嘉林 committed
35 36 37
    },
    ready() {
      console.log("进入组件ready");
李嘉林 committed
38 39 40 41 42 43 44 45
      let _this = this;
      let { clientHeight } = this.data;
      wx.getSystemInfo({
        success: function(res) {
          let clientHeight = parseInt(res.windowHeight);
          _this.setData({ clientHeight });
        }
      });
李嘉林 committed
46 47 48 49 50 51 52 53 54
    }
  },
  observers: {},
  data: {
    //列数
    waterfallImgCol: 2,
    //列高度数组
    waterfallDeviationHeight: [0, 0],
    contentList: [],
李嘉林 committed
55 56
    inj: 0,
    clientHeight: 0, //窗口高度
李嘉林 committed
57
    goodsNum: 0,
李嘉林 committed
58 59 60
  },
  methods: {
    // 初始化数组
李嘉林 committed
61
    initList(goodsList) {
李嘉林 committed
62
      // 初始化容器
李嘉林 committed
63
      let { contentList, waterfallDeviationHeight } = this.data;
李嘉林 committed
64 65
      contentList[0] = [];
      contentList[1] = [];
李嘉林 committed
66
      waterfallDeviationHeight = [0, 0];
李嘉林 committed
67
      this.setData({ contentList, waterfallDeviationHeight });
李嘉林 committed
68
      this.reLoadPro(goodsList);
李嘉林 committed
69
    },
李嘉林 committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    async reLoadPro(goodsList) {
      console.log(goodsList,'----------------------75')
      // this.setHeight(this.data.goodsNum);
      this.setHeight1(this.data.goodsNum,goodsList);
      this.setData({
        goodsNum:
        goodsList.length > 0 ? goodsList.length + 1 : 0
      });
    },
    setHeight1(i,goodsList) {
      let itemList = goodsList.splice(i, goodsList.length);
      let { contentList } = this.data;
      itemList.forEach((item, index) => {
        if (index % 2 == 0) {
          contentList[0].push(item);
          console.log(item.productName, "--------位置--左");
        } else {
          contentList[1].push(item);
          console.log(item.productName, "--------位置--右");
        }
      });
      this.setData({ contentList });
李嘉林 committed
92 93 94 95
    },
    setHeight(i) {
      let _this = this;
      let item = this.data.note[i];
李嘉林 committed
96 97 98 99 100
      // console.log(i, "---------------i");
      // console.log(
      //   this.data.waterfallDeviationHeight,
      //   "-------waterfallDeviationHeight"
      // );
李嘉林 committed
101
      let minIndex = this.filterMin();
李嘉林 committed
102
      // console.log(minIndex, "---------------126");
李嘉林 committed
103 104 105
      let contentList = this.data.contentList;
      contentList[minIndex].push(item);
      this.setData({ contentList });
李嘉林 committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      _this
        .createSelectorQuery()
        .select(`.item${minIndex}`)
        .boundingClientRect(function(res) {
          if (res) {
            res.height;
            console.log(res, "--------------res");
            let waterfallDeviationHeight = _this.data.waterfallDeviationHeight;
            waterfallDeviationHeight[minIndex] = res.height;
            _this.setData({ waterfallDeviationHeight });
            if (i >= _this.data.note.length - 1) {
              return;
            }
            i++;
            _this.setHeight(i);
          }
        })
        .exec();
李嘉林 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
      // const queryDom = wx.createSelectorQuery().in(this);
      // queryDom.select(`.item${minIndex}`).boundingClientRect();
      // queryDom.exec(res => {
      //   res[0].height;
      //   console.log(res[0], "--------------res");
      //   let waterfallDeviationHeight = _this.data.waterfallDeviationHeight;
      //   waterfallDeviationHeight[minIndex] = res[0].height;
      //   _this.setData({ waterfallDeviationHeight });
      //   if (i >= _this.data.note.length - 1) {
      //     return;
      //   }
      //   i++;
      //   _this.setHeight(i);
      // });
李嘉林 committed
138 139 140 141 142 143 144 145 146
    },
    filterMin() {
      let val = Math.min.apply(null, this.data.waterfallDeviationHeight);
      // console.log(this.waterfallDeviationHeight,this.waterfallDeviationHeight.indexOf(val),'-------------------167')
      return this.data.waterfallDeviationHeight.indexOf(val);
    },
    imageLoad(ev) {
      let windowWidth = wx.getSystemInfoSync().windowWidth;
      let src = ev.currentTarget.dataset.src,
147 148
        width = ev.detail.width,
        height = ev.detail.height;
李嘉林 committed
149 150
      let newWidth = parseInt(windowWidth * 0.49);
      let newHeight = parseInt(newWidth / (width / height));
151 152 153 154 155 156 157 158
      console.log(
        width,
        height,
        newWidth,
        newHeight,
        ev.target.dataset.id,
        "-----------------------219"
      );
李嘉林 committed
159 160
    },
    getScroll(el) {
李嘉林 committed
161
      console.log(el,'--------------161')
李嘉林 committed
162 163 164 165 166 167 168
      let { finished } = this.properties;
      let _this = this;
      _this
        .createSelectorQuery()
        .select("#waterfallFlow")
        .boundingClientRect(function(res) {
          if (res) {
李嘉林 committed
169
            let scrollTop = parseInt(el.scrollTop);
李嘉林 committed
170 171
            let domHeight = parseInt(res.height);
            // 窗口高度+滚动高度等于 = 元素整体高度 -(预留一段距离加载) 触发父组件查询接口
李嘉林 committed
172
            console.log(scrollTop,finished,'--------------finished')
李嘉林 committed
173 174 175 176
            if (
              scrollTop + _this.data.clientHeight >= domHeight - 150 &&
              finished == false
            ) {
李嘉林 committed
177 178 179 180 181
              _this.triggerEvent("onLoad");
            }
          }
        })
        .exec();
李嘉林 committed
182 183 184
    }
  }
});