wepy 小程序 瀑布流模式

前提:

1.小程序使用wepy@1.70框架
2.自定义(商圈)瀑布流

<template lang="wxml">
  <view class="tradingAreaFlow">
    <view class="trading_column" style="height:{{lastboxheight}}px;"  wx:if="{{sqData.length > 0}}">
      <repeat for="{{sqData}}" key="index" index="index" item="item">
        <!-- 卡片Item -->
        <tradeFlowCard :item.sync="item" :isLogin.sync="isLogin"></tradeFlowCard>
      </repeat>
    </view>
  </view>
</template>

<script>
import wepy from 'wepy'
import tradeFlowCard from '@/components/tradeFlowCard'

export default class tradingAreaFlow extends wepy.component {
  components = {
    tradeFlowCard
  }
  props = {
    isLogin: Boolean,
    curTab: {
      type: Number,
      default: 0,
      twoWay: true
    },
    tabsIndex: {
      type: Number,
      default: 0,
      twoWay: true
    },
    tabCur: {
      type: Number,
      default: 0,
      twoWay: true
    },
    tradingList: {
      type: Array,
      default: [],
      twoWay: true
    },
    filterParam: Object
  }
  data = {
    list: [],
    sqData: [],
    imgLoadList: [],
    lastboxheight: 0
  }
  watch = {
    tradingList(newVal) {
      if (this.tabCur === 0) {
        this.imgLocation(newVal)
      }
    }
  }
  thumbsNum(text) {
    return this.strlen(text) > 20 ? 2 : 1
  }
  // 中英文混合字符串长度,英文字符加1,汉字加2
  strlen(str) {
    var len = 0
    for (var i = 0; i < str.length; i++) {
      var c = str.charCodeAt(i)
      // 单字节加1
      if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
        len++
      } else {
        // 汉字加2
        len += 2
      }
    }
    return len
  }
  // 图片定位
  imgLocation(list) {
    // 求出每个盒子的宽度(包括内边距)
    let w = Math.floor((wepy.getSystemInfoSync().windowWidth - 30 - 10) / 2)
    // 求出当前窗口所能承载的瀑布流列数
    let cols = Math.floor(wepy.getSystemInfoSync().windowWidth / w)
    let hArr = [] // 保存每一列的高度

    this.sqData = list.map((item, index) => {
      let h = item.imgUrl ? (this.thumbsNum(item.content) === 2 ? 293 : 269) : (this.thumbsNum(item.content) === 2 ? 125 : 101)
      if (index < cols) {
        hArr[index] = h + 10
        if (index) { // 第二个
          item.top = 0
          item.left = w + 10
        } else { // 第一个
          item.top = 0
          item.left = 0
        }
      } else {
        // 最小高度的列
        let minH = Math.min.apply(null, hArr)
        // 最小高度的列在数组中的索引
        let minHIndex = hArr.indexOf(minH)

        item.top = minH
        item.left = (minHIndex * w) + (minHIndex * 10)
        // console.log(hArr, minH, item.left, index)
        hArr[minHIndex] += h + 10
      }
      this.lastboxheight = Math.max.apply(null, hArr)
      return item
    })
    this.$apply()
  }
}
</script>

<style lang=scss>
@import "../common/style/var.scss";
.trading_column {
  position: relative;
  margin: 0 30rpx;
  // column-count: 2;
  // column-gap:30rpx;
}
.nodata {
  font-size: 28rpx;
  color: $color-ll;
  line-height: 1;
  padding: 44rpx 0;
  text-align: center;
}
.nonext {
  font-size: 28rpx;
  padding: 44rpx 0;
  color: $color-ll;
  border-color: transparent;
  background: transparent;
  text-align: center;
}
.loading {
  width: 36rpx;
  height: 36rpx;
  margin: 0 auto;
  image {
    display: block;
    width: 100%;
    height: 100%;
  }
}
</style>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容