<template> <div class="version"> <ul ref="containerRef"> <li v-for="item in robotList" :key="item.title"> <img :src="item.url" alt="robot" /> <div class="text"> <p class="title">{{ item.title }}</p> <p class="desc">{{ item.desc }}</p> </div> </li> <li class="load-status">{{ hasMore ? '继续滚动加载' : '没有更多了' }}</li> </ul> </div></template><script setup lang="ts">import { onBeforeUnmount, onMounted, ref } from 'vue'interface RobotItem { url: string title: string desc: string}const pageSize = 10const maxTotal = 100const robotList = ref<RobotItem[]>([])const hasMore = ref(true)const loading = ref(false)const page = ref(1)const containerRef = ref<HTMLElement | null>(null)const fetchListApi = async (pageIndex: number) => { const currentTotal = (pageIndex - 1) * pageSize return Array.from({ length: pageSize }, (_, index) => ({ url: `https://robohash.org/${currentTotal + index}.png?set=set4&size=200x200`, title: `机器猫 ${currentTotal + index + 1}`, desc: `描述 ${currentTotal + index + 1}`, }))}const onLoadMore = async () => { if (loading.value || !hasMore.value) { return } loading.value = true const data = await fetchListApi(page.value) if (data.length > 0) { robotList.value = [...robotList.value, ...data] page.value += 1 } if (robotList.value.length >= maxTotal) { hasMore.value = false } loading.value = false}const handleScroll = () => { const element = containerRef.value if (!element) { return } const { scrollTop, scrollHeight, clientHeight } = element if (scrollTop + clientHeight >= scrollHeight - 50) { onLoadMore() }}onMounted(() => { onLoadMore() containerRef.value?.addEventListener('scroll', handleScroll)})onBeforeUnmount(() => { containerRef.value?.removeEventListener('scroll', handleScroll)})</script><style scoped>.version { height: 100vh; background-color: #fff;}.version ul { height: 100vh; margin: 0; overflow-y: auto; padding: 0;}.version li { display: flex; border-bottom: 1px solid #eee; padding: 15px; list-style: none;}.version li img { width: 50px; height: 50px; margin-right: 15px;}.text { display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 8px;}.title { margin: 0; color: #000; font-size: 16px; font-weight: 700;}.desc { margin: 0; color: #666; font-size: 12px;}.load-status { justify-content: center; color: #999;}</style><template> <main class="load-refresh-showcase"> <section class="showcase-shell"> <header class="showcase-header"> <div> <p>Interaction Demo</p> <h1>上拉加载下拉刷新</h1> </div> <div class="version-switch" aria-label="选择演示版本"> <button v-for="item in versions" :key="item.key" type="button" :class="{ active: activeVersion === item.key }" @click="activeVersion = item.key" > {{ item.label }} </button> </div> </header> <div class="phone-frame"> <component :is="activeComponent" /> </div> </section> </main></template><script lang="ts" setup>import { computed, ref } from 'vue'import VersionOne from './version-1.vue'import VersionTwo from './version-2.vue'const versions = [ { key: 'version-1', label: '刷新 + 加载', component: VersionOne }, { key: 'version-2', label: '上拉加载', component: VersionTwo },] as constconst activeVersion = ref<(typeof versions)[number]['key']>('version-1')const activeComponent = computed( () => versions.find((item) => item.key === activeVersion.value)?.component ?? VersionOne,)</script><style scoped>.load-refresh-showcase { min-height: 100%; padding: 28px 20px 44px; color: #252933;}.showcase-shell { display: grid; gap: 22px; justify-items: center; margin: 0 auto; max-width: 1040px;}.showcase-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; width: 100%;}.showcase-header p,.showcase-header h1 { margin: 0;}.showcase-header p { color: #1e80ff; font-size: 13px; font-weight: 800;}.showcase-header h1 { margin-top: 6px; font-size: 28px; line-height: 1.25;}.version-switch { display: inline-flex; gap: 6px; border: 1px solid #e4e6eb; border-radius: 6px; background: #fff; padding: 4px;}.version-switch button { border: 0; border-radius: 4px; background: transparent; color: #515767; cursor: pointer; font: inherit; font-size: 14px; font-weight: 700; padding: 8px 12px;}.version-switch button.active,.version-switch button:hover { background: rgba(30, 128, 255, 0.1); color: #1e80ff;}.phone-frame { width: min(420px, 100%); height: min(720px, calc(100vh - 220px)); min-height: 520px; overflow: hidden; border: 1px solid #e4e6eb; border-radius: 8px; background: #fff; box-shadow: 0 12px 32px rgba(29, 33, 41, 0.08);}.phone-frame :deep(.version) { height: 100%;}@media (max-width: 720px) { .showcase-header { align-items: flex-start; flex-direction: column; } .phone-frame { height: 640px; }}</style><template> <main class="load-refresh-page"> <section class="load-refresh-panel"> <h1>上拉加载下拉刷新</h1> <nav class="version-links"> <RouterLink v-for="item in list" :key="item.path" :to="item.path"> {{ item.title }} </RouterLink> </nav> </section> </main></template><script setup lang="ts">const list = [ { title: '版本1: 上拉加载下拉刷新', path: '/interaction/load-refresh/version-1', }, { title: '版本2: 上拉加载', path: '/interaction/load-refresh/version-2', },]</script><style scoped>.load-refresh-page { min-height: 100vh; padding: 24px; color: #111827;}.load-refresh-panel { max-width: 960px; margin: 0 auto; border: 1px solid #e5e7eb; border-radius: 8px; background: #fff; padding: 24px;}.load-refresh-panel h1 { margin: 0 0 20px; font-size: 24px; font-weight: 600;}.version-links { display: flex; flex-wrap: wrap; gap: 12px;}.version-links a { border: 1px solid #d1d5db; border-radius: 6px; padding: 10px 16px; color: #111827; text-decoration: none;}.version-links a:hover { border-color: #42b883; color: #42b883;}</style><template> <section ref="containerRef" class="version version1" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd" > <div class="pull-refresh" :style="{ height: `${pullDistance}px`, opacity: pullDistance > 0 ? 1 : 0 }" > {{ refreshing ? '正在刷新...' : '下拉刷新' }} </div> <ul> <li v-for="item in list" :key="item.title"> <img :src="item.url" alt="robot" /> <div class="text"> <p class="title">{{ item.title }}</p> <p class="desc">{{ item.desc }}</p> </div> </li> </ul> <div class="load-status"> {{ loading ? '加载中...' : hasMore ? '上拉加载' : '没有更多了' }} </div> </section></template><script setup lang="ts">import { onBeforeUnmount, onMounted, ref } from 'vue'interface RobotItem { url: string title: string desc: string}const pageSize = 10const maxTotal = 100const list = ref<RobotItem[]>([])const page = ref(1)const loading = ref(false)const hasMore = ref(true)const refreshing = ref(false)const pullDistance = ref(0)const containerRef = ref<HTMLElement | null>(null)let startY = 0const fetchListApi = async (pageIndex: number) => { await new Promise((resolve) => window.setTimeout(resolve, 800)) const currentTotal = (pageIndex - 1) * pageSize const remain = maxTotal - currentTotal const length = remain > pageSize ? pageSize : remain if (length <= 0) { return [] } return Array.from({ length }, (_, index) => ({ url: `https://robohash.org/${currentTotal + index}.png?set=set4&size=200x200`, title: `机器猫 ${currentTotal + index + 1}`, desc: `描述 ${currentTotal + index + 1}`, }))}const onRefresh = async () => { if (refreshing.value) { return } refreshing.value = true const data = await fetchListApi(1) list.value = data page.value = 1 hasMore.value = maxTotal > pageSize refreshing.value = false pullDistance.value = 0}const onLoadMore = async () => { if (loading.value || refreshing.value || !hasMore.value) { return } loading.value = true const nextPage = page.value + 1 const data = await fetchListApi(nextPage) if (data.length > 0) { list.value = [...list.value, ...data] page.value = nextPage hasMore.value = list.value.length < maxTotal } else { hasMore.value = false } loading.value = false}const handleScroll = () => { const element = containerRef.value if (!element) { return } const { scrollTop, scrollHeight, clientHeight } = element if (scrollTop + clientHeight >= scrollHeight - 100) { onLoadMore() }}const handleTouchStart = (event: TouchEvent) => { const element = containerRef.value if (element?.scrollTop === 0) { startY = event.touches[0]?.pageY ?? 0 }}const handleTouchMove = (event: TouchEvent) => { const element = containerRef.value if (!element || element.scrollTop > 0) { return } const diff = (event.touches[0]?.pageY ?? startY) - startY if (diff > 0) { pullDistance.value = Math.pow(diff, 0.8) }}const handleTouchEnd = () => { if (pullDistance.value > 60) { onRefresh() } else { pullDistance.value = 0 }}onMounted(() => { onRefresh() containerRef.value?.addEventListener('scroll', handleScroll)})onBeforeUnmount(() => { containerRef.value?.removeEventListener('scroll', handleScroll)})</script><style scoped>.version { position: relative; height: 100vh; overflow: auto; background-color: #fff;}.pull-refresh { display: flex; overflow: hidden; align-items: center; justify-content: center; background: #f5f5f5;}.version ul { margin: 0; padding: 0;}.version li { display: flex; border-bottom: 1px solid #eee; padding: 15px; list-style: none;}.version li img { width: 50px; height: 50px; margin-right: 15px;}.text { display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 8px;}.title { margin: 0; color: #000; font-size: 16px; font-weight: 700;}.desc { margin: 0; color: #666; font-size: 12px;}.load-status { padding: 20px; color: #999; text-align: center;}</style>