ScrollableFamilyList.vue 9.73 KB
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
<template>
  <view class="scrollable-family-list" :style="{ height: containerHeight }">
    <scroll-view
      class="scroll-container"
      :scroll-y="true"
      :scroll-top="scrollTop"
      :enable-back-to-top="false"
      :scroll-with-animation="true"
      :enhanced="false"
      :bounces="false"
      :show-scrollbar="false"
      @scroll="onScroll"
      @scrolltoupper="onScrollToUpper"
      @scrolltolower="onScrollToLower"
    >
      <view class="content-wrapper" :style="{ opacity: contentOpacity }">
        <view
          v-for="(item, index) in currentPageData"
          :key="`${currentPage}-${index}`"
          class="family-item"
          :style="getItemStyle(index)"
        >
          <view class="family-content">
            <!-- 头像 -->
            <view class="family-avatar-container">
              <image
                :src="item.avatar"
                class="family-avatar"
                mode="aspectFill"
              />
            </view>

            <!-- 信息区域 -->
            <view class="family-info">
              <!-- 标题行 -->
              <view class="family-title-row">
                <text class="family-name">{{ item.familyName }}</text>
              </view>

              <!-- 介绍 -->
              <text class="family-intro">{{ item.familyIntro }}</text>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, computed, watch, onMounted, nextTick } from 'vue'
import Taro, { usePageScroll } from '@tarojs/taro'

// Props
const props = defineProps({
  // 家庭数据列表
  familyData: {
    type: Array,
    default: () => []
  },
  // 容器高度
  height: {
    type: String,
    default: '600rpx'
  },
  // 每页显示的行数
  itemsPerPage: {
    type: Number,
    default: 5
  },
  // 是否监听外部容器滚动
  listenExternalScroll: {
    type: Boolean,
    default: false
  },
  // 添加额外占位,用于触发滚动事件
  extraPlaceholder: {
    type: Number,
    default: 1
  }
})

// 响应式数据
const scrollTop = ref(0)
const currentPage = ref(0)
const contentOpacity = ref(1)
const isTransitioning = ref(false)
const lastScrollTop = ref(0)
const scrollDirection = ref('') // 'up' | 'down'

// 计算属性
const containerHeight = computed(() => props.height)

const totalPages = computed(() => {
  return Math.ceil(props.familyData.length / props.itemsPerPage)
})

const currentPageData = computed(() => {
  const start = currentPage.value * props.itemsPerPage
  // 每页多显示一个item,有利于滚动触发
  const end = start + props.itemsPerPage + props.extraPlaceholder
  return props.familyData.slice(start, end)
})

// 随机水平位置数组
const randomOffsets = ref([])

// 生成按行数规律排列的偏移
const generateRandomOffsets = () => {
  // 为实际显示的item数量生成偏移(包括多出的一个)
  const actualItemCount = Math.min(props.itemsPerPage + props.extraPlaceholder, props.familyData.length - currentPage.value * props.itemsPerPage)

  randomOffsets.value = Array.from({ length: actualItemCount }, (_, index) => {
    // 按行数规律排列:奇数行(1,3,5...)靠左,偶数行(2,4,6...)靠右
    const rowNumber = index + 1 // 行号从1开始

    if (rowNumber % 2 === 1) {
      // 奇数行靠左边容器
      return 0  // 不偏移,靠左
    } else {
      // 偶数行靠右边容器,使用特殊标记
      return 100  // 使用特殊值标记需要贴右边
    }
  })
}

// 获取每个项目的样式
const getItemStyle = (index) => {
  const offset = randomOffsets.value[index] || 0
  const rowNumber = index + 1 // 行号从1开始

  // 偶数行贴右边的特殊处理
  if (rowNumber % 2 === 0 && offset === 100) {
    return {
      marginBottom: '24rpx',
      maxWidth: '100%',
      paddingLeft: '0',
      paddingRight: '0',
      display: 'flex',
      justifyContent: 'flex-end', // 让内容靠右对齐
      // 不使用transform,完全依靠justify-content来贴右边
    }
  }

  // 奇数行靠左的处理
  return {
    transform: `translateX(${offset}rpx)`,
    marginBottom: '24rpx',
    maxWidth: '100%',
    paddingLeft: '0',
    paddingRight: '0'
  }
}

// 滚动事件处理
const onScroll = (e) => {
  if (isTransitioning.value) return

  const currentScrollTop = e.detail.scrollTop
  const delta = currentScrollTop - lastScrollTop.value

  // 提高阈值,减少误触发
  if (Math.abs(delta) > 10) {
    scrollDirection.value = delta > 0 ? 'down' : 'up'
  }

  lastScrollTop.value = currentScrollTop
}

// 滚动到顶部 - 优化触发逻辑
const onScrollToUpper = () => {
  if (props.listenExternalScroll || isTransitioning.value) return

  // 添加延迟防抖,避免快速触发
  setTimeout(() => {
    if (scrollDirection.value === 'up' && !isTransitioning.value) {
      changePage('prev')
    }
  }, 100)
}

// 滚动到底部 - 优化触发逻辑
const onScrollToLower = () => {
  if (props.listenExternalScroll || isTransitioning.value) return

  // 添加延迟防抖,避免快速触发
  setTimeout(() => {
    if (scrollDirection.value === 'down' && !isTransitioning.value) {
      changePage('next')
    }
  }, 100)
}

// 切换页面 - 优化过渡效果
const changePage = async (direction) => {
  if (isTransitioning.value) return

  isTransitioning.value = true

  // 重置滚动方向,避免连续触发
  scrollDirection.value = ''

  // 淡出效果
  contentOpacity.value = 0

  await new Promise(resolve => setTimeout(resolve, 250))

  // 更新页面
  if (direction === 'next') {
    currentPage.value = (currentPage.value + 1) % totalPages.value
  } else {
    currentPage.value = currentPage.value === 0
      ? totalPages.value - 1
      : currentPage.value - 1
  }

  generateRandomOffsets()

  // 重置滚动位置到中间位置,确保有足够的滚动空间
  // 设置一个中间值,让用户可以向上或向下滚动
  scrollTop.value = 100

  await nextTick()

  // 再次确保滚动位置重置,给足够的滚动触发空间
  setTimeout(() => {
    scrollTop.value = 50
  }, 50)

  // 淡入效果
  contentOpacity.value = 1

  // 延长过渡锁定时间,确保动画完成
  setTimeout(() => {
    isTransitioning.value = false
    // 最终重置到一个稳定的中间位置
    scrollTop.value = 80
  }, 400)
}

// 外部滚动监听
const handleExternalScroll = (res) => {
  if (!props.listenExternalScroll || isTransitioning.value) return

  // 获取页面滚动信息
  const { scrollTop } = res
  const windowHeight = Taro.getSystemInfoSync().windowHeight

  // 使用createSelectorQuery获取页面高度
  const query = Taro.createSelectorQuery()
  query.selectViewport().scrollOffset()
  query.exec((queryRes) => {
    if (queryRes && queryRes[0]) {
      const { scrollHeight } = queryRes[0]

      // 判断是否滚动到底部(留一些余量)
      if (scrollTop + windowHeight >= scrollHeight - 50) {
        setTimeout(() => {
          // 触发翻页到下一页
          changePage('next')
        }, 100)
      }
    }
  })
}

// 使用usePageScroll监听页面滚动
if (props.listenExternalScroll) {
  usePageScroll(handleExternalScroll)
}

// 监听数据变化
watch(() => props.familyData, () => {
  currentPage.value = 0
  generateRandomOffsets()
}, { immediate: true })

// 组件挂载
onMounted(() => {
  generateRandomOffsets()
})
</script>

<style>
.scrollable-family-list {
  position: relative;
  width: 100%;
  /* border-radius: 20rpx; */
  overflow: hidden;
  /* background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); */

}

.scroll-container {
  width: 100%;
  height: 100%;
}

.content-wrapper {
  /* padding: 32rpx 0; */
  padding-top: 32rpx;
  transition: opacity 0.3s ease;
  min-height: 100%;
}

.family-item {
  width: 100%;
  transition: transform 0.3s ease;
}

.family-content {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.75);
  border-radius: 30rpx;
  padding: 20rpx 24rpx;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(12rpx);
  border: 2rpx solid rgba(255, 255, 255, 0.5);
  max-width: 550rpx;
  min-width: 350rpx;
  transition: all 0.3s ease;
}

.family-content:hover {
  transform: translateY(-4rpx);
  box-shadow: 0 12rpx 32rpx rgba(0, 0, 0, 0.12);
}

.family-avatar-container {
  flex-shrink: 0;
  margin-right: 20rpx;
  position: relative;
}

.family-avatar {
  width: 64rpx;
  height: 64rpx;
  border-radius: 50%;
  border: 3rpx solid #fff;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
}

.family-avatar-container::after {
  content: '';
  position: absolute;
  top: -3rpx;
  left: -3rpx;
  right: -3rpx;
  bottom: -3rpx;
  border-radius: 50%;
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4);
  z-index: -1;
  animation: rotate 4s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.family-info {
  flex: 1;
  min-width: 0;
}

.family-title-row {
  display: flex;
  align-items: center;
  margin-bottom: 8rpx;
}

.family-name {
  font-weight: 700;
  color: #1a202c;
  font-size: 32rpx;
  margin-right: 12rpx;
  flex-shrink: 0;
  text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.1);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.family-intro {
  color: #4a5568;
  font-size: 26rpx;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  line-height: 1.5;
  font-weight: 500;
}

.page-indicator {
  position: absolute;
  bottom: 16rpx;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 12rpx;
  z-index: 10;
}

.indicator-dot {
  width: 12rpx;
  height: 12rpx;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.4);
  transition: all 0.3s ease;
}

.indicator-dot.active {
  background: rgba(255, 255, 255, 0.9);
  transform: scale(1.2);
}
</style>