yanjinhui
10 天以前 c5de0d98241f8c8349fa38851b77efcfc61e4d26
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<script lang="ts" setup>
import { propTypes } from '@/utils/propTypes'
import { useDesign } from '@/hooks/web/useDesign'
import { ref, nextTick, unref, onMounted, watch } from 'vue'
import { useEventListener, useIntersectionObserver } from '@vueuse/core'
import { debounce } from 'lodash-es'
 
const { getPrefixCls } = useDesign()
 
const prefixCls = getPrefixCls('waterfall')
 
const emit = defineEmits(['loadMore'])
 
const prop = defineProps({
  data: propTypes.arrayOf(propTypes.any),
  reset: propTypes.bool.def(true),
  width: propTypes.number.def(200),
  gap: propTypes.number.def(20),
  props: propTypes.objectOf(propTypes.string).def({
    src: 'src',
    height: 'height'
  }),
  cols: propTypes.number.def(undefined),
  loadingText: propTypes.string.def('加载中...'),
  loading: propTypes.bool.def(false),
  end: propTypes.bool.def(false),
  endText: propTypes.string.def('没有更多了'),
  autoCenter: propTypes.bool.def(true),
  layout: propTypes.oneOf(['javascript', 'flex']).def('flex')
})
 
const wrapEl = ref<HTMLDivElement>()
 
const heights = ref<number[]>([])
 
const wrapHeight = ref(0)
 
const wrapWidth = ref(0)
 
const loadMore = ref<HTMLDivElement>()
 
// 首先确定列数 = 页面宽度 / 图片宽度
const innerCols = ref(0)
 
const filterData = ref<any[]>([])
 
const filterWaterfall = async () => {
  filterData.value = []
  const { props, width, gap } = prop
  const data = prop.data as any[]
  await nextTick()
 
  const container = unref(wrapEl) as HTMLElement
  if (!container) return
  innerCols.value = prop.cols ?? Math.floor(container.clientWidth / (width + gap))
 
  const length = data.length
  for (let i = 0; i < length; i++) {
    if (i < unref(innerCols)) {
      heights.value[i] = data[i][props.height as string]
      filterData.value.push({
        ...data[i],
        top: 0,
        left: i * (width + gap)
      })
    } else {
      // 其他行,先找出最矮的那一列 和 索引
      // 假设最小高度是第一个元素
      let minHeight = heights.value[0]
      let index = 0
      // 找出最小高度
      for (let j = 1; j < unref(innerCols); j++) {
        if (unref(heights)[j] < minHeight) {
          minHeight = unref(heights)[j]
          index = j
        }
      }
 
      // 更新最矮高度
      heights.value[index] += data[i][props.height as string] + gap
      filterData.value.push({
        ...data[i],
        top: minHeight + gap,
        left: index * (width + gap)
      })
    }
  }
  wrapHeight.value = Math.max(...unref(heights))
  wrapWidth.value = unref(innerCols) * (width + gap) - gap
}
 
const flexWaterfall = async () => {
  const { width, gap } = prop
  const data = prop.data as any[]
  await nextTick()
 
  const container = unref(wrapEl) as HTMLElement
  if (!container) return
  innerCols.value = prop.cols ?? Math.floor(container.clientWidth / (width + gap))
 
  const length = data.length
  // 根据列数,创建数组
  const arr = new Array(unref(innerCols)).fill([])
  // 循环data,依次插入到arr中
  for (let i = 0; i < length; i++) {
    const index = i % unref(innerCols)
    arr[index] = [...arr[index], data[i]]
  }
  filterData.value = arr
}
 
const initLayout = () => {
  const { layout } = prop
  if (layout === 'javascript') {
    filterWaterfall()
  } else if (layout === 'flex') {
    flexWaterfall()
  }
}
 
watch(
  () => [prop.data, prop.cols],
  () => {
    initLayout()
  },
  {
    immediate: true
  }
)
 
onMounted(() => {
  if (unref(prop.reset)) {
    useEventListener(window, 'resize', debounce(initLayout, 300))
  }
  useIntersectionObserver(
    unref(loadMore),
    ([{ isIntersecting }]) => {
      if (isIntersecting && !prop.loading && !prop.end) {
        emit('loadMore')
      }
    },
    {
      threshold: 0.1
    }
  )
})
</script>
 
<template>
  <div
    :class="[
      prefixCls,
      'flex',
      'items-center',
      {
        'justify-center': autoCenter
      }
    ]"
    ref="wrapEl"
    :style="{
      height: `${layout === 'javascript' ? wrapHeight + 40 : 'auto'}px`
    }"
  >
    <template v-if="layout === 'javascript'">
      <div class="relative" :style="{ width: `${wrapWidth}px`, height: `${wrapHeight + 40}px` }">
        <div
          v-for="(item, $index) in filterData"
          :class="[
            `${prefixCls}-item__${$index}`,
            {
              absolute: layout === 'javascript'
            }
          ]"
          :key="`water-${$index}`"
          :style="{
            width: `${width}px`,
            height: `${item[props.height as string]}px`,
            top: `${item.top}px`,
            left: `${item.left}px`
          }"
        >
          <img :src="item[props.src as string]" class="w-full h-full block" alt="" srcset="" />
        </div>
        <div
          ref="loadMore"
          class="h-40px flex justify-center absolute w-full"
          :style="{
            top: `${wrapHeight + gap}px`
          }"
        >
          {{ end ? endText : loadingText }}
        </div>
      </div>
    </template>
    <template v-else-if="layout === 'flex'">
      <div
        class="relative flex pb-40px"
        :style="{
          width: cols ? '100%' : 'auto'
        }"
      >
        <div
          v-for="(item, $index) in filterData"
          :key="`waterWrap-${$index}`"
          class="flex-1"
          :style="{
            marginRight: $index === filterData.length - 1 ? '0' : `${gap}px`
          }"
        >
          <div
            v-for="(child, i) in item"
            :key="`waterWrap-${$index}-${i}`"
            :style="{
              marginBottom: `${gap}px`,
              width: cols ? '100%' : `${width}px`,
              height: cols ? 'auto' : `${child[props.height as string]}px`
            }"
          >
            <img :src="child[props.src as string]" class="w-full h-full block" alt="" srcset="" />
          </div>
        </div>
        <div
          ref="loadMore"
          class="h-40px flex justify-center absolute w-full items-center"
          :style="{
            bottom: 0
          }"
        >
          {{ end ? endText : loadingText }}
        </div>
      </div>
    </template>
  </div>
</template>