feat: C端展示区改为单张居中无缝轮播并接入PhotoSwipe图片视频查看器,轮播间隔支持后台配置

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-30 19:09:13 +08:00
co-authored by Cursor
parent f8430bda90
commit 6141086ecf
11 changed files with 539 additions and 404 deletions
+399 -46
View File
@@ -38,30 +38,83 @@
</div>
</div>
<!-- 图片 / 视频轮播 -->
<div v-if="data.artwork.images?.length" class="carousel">
<div
v-for="(m, i) in data.artwork.images"
:key="i"
class="carousel-item"
@click="openViewer(data.artwork.images, i)"
>
<video
v-if="isVideo(m)"
:src="m.url"
class="carousel-media"
muted
playsinline
preload="metadata"
/>
<img v-else :src="m.url" :alt="m.name" class="carousel-media" loading="lazy" />
<div v-if="isVideo(m)" class="play-overlay">
<svg viewBox="0 0 64 64" width="56" height="56" aria-hidden="true">
<circle cx="32" cy="32" r="30" fill="rgba(0,0,0,0.42)" />
<polygon points="26,21 46,32 26,43" fill="#fff" />
</svg>
<!-- 图片 / 视频轮播:单张居中、两侧露出相邻媒体边界、定时切换、克隆实现无缝循环 -->
<div
v-if="data.artwork.images?.length"
ref="carouselEl"
class="carousel"
@pointerdown="onDragStart"
@pointermove="onDragMove"
@pointerup="onDragEnd"
@pointercancel="onDragEnd"
@mouseenter="pauseAuto"
@mouseleave="resumeAuto"
>
<div class="carousel-track" :style="trackStyle" @transitionend="onTransitionEnd">
<div
v-for="(s, i) in loopSlides"
:key="i"
class="carousel-slide"
:class="{ current: i === pos }"
@click="onSlideClick(i, s)"
>
<video
v-if="isVideo(s.media)"
:src="s.media.url"
class="carousel-media"
muted
playsinline
preload="metadata"
/>
<img v-else :src="s.media.url" :alt="s.media.name" class="carousel-media" loading="lazy" />
<div v-if="isVideo(s.media)" class="play-overlay">
<svg viewBox="0 0 64 64" width="56" height="56" aria-hidden="true">
<circle cx="32" cy="32" r="30" fill="rgba(0,0,0,0.42)" />
<polygon points="26,21 46,32 26,43" fill="#fff" />
</svg>
</div>
</div>
</div>
<!-- 左右切换箭头(PC 悬停显示,移动端常驻) -->
<button
v-if="data.artwork.images.length > 1"
type="button"
class="carousel-nav prev"
:aria-label="t('viewer.prev')"
@pointerdown.stop
@click.stop="go(pos - 1)"
>
‹
</button>
<button
v-if="data.artwork.images.length > 1"
type="button"
class="carousel-nav next"
:aria-label="t('viewer.next')"
@pointerdown.stop
@click.stop="go(pos + 1)"
>
›
</button>
</div>
<!-- 媒体计数 + 当前位置指示 -->
<div v-if="data.artwork.images?.length > 1" class="carousel-ind">
<div class="carousel-dots">
<button
v-for="(m, i) in data.artwork.images"
:key="i"
type="button"
class="dot"
:class="{ active: i === carouselIndex }"
:aria-label="String(i + 1)"
@click="goToReal(i)"
/>
</div>
<div class="carousel-count">
<span v-if="isVideo(data.artwork.images[carouselIndex])" class="carousel-count-ico">▶</span>
{{ carouselIndex + 1 }} / {{ data.artwork.images.length }}
</div>
</div>
<!-- 基础信息 -->
@@ -178,9 +231,6 @@
<footer class="footer muted">{{ appName }}</footer>
</template>
<!-- 媒体查看器:图片可双指缩放/拖动,视频弹窗播放 -->
<media-viewer v-model:show="viewerShow" :items="viewerItems" :start="viewerStart" />
<!-- 轻提示 -->
<transition name="toast">
<div v-if="toast" class="toast">{{ toast }}</div>
@@ -189,13 +239,13 @@
</template>
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue';
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import dayjs from 'dayjs';
import { fetchTrace, fetchAppInfo } from '@/api';
import { setLocale } from '@/locales';
import MediaViewer from '@/components/MediaViewer.vue';
import { openMediaViewer } from '@/composables/openMediaViewer';
const route = useRoute();
const { t, locale } = useI18n();
@@ -205,10 +255,6 @@ const data = ref<any>(null);
const appName = ref('');
const toast = ref('');
const viewerShow = ref(false);
const viewerItems = ref<any[]>([]);
const viewerStart = ref(0);
// 节点图片默认收缩,避免页面过长;点击展开
const expanded = ref<Set<number>>(new Set());
function toggleNode(i: number) {
@@ -244,9 +290,208 @@ function safeUrl(v?: string): string {
return /^(https?:\/\/|\/)/i.test(v) ? v : '';
}
function openViewer(items: any[], index: number) {
viewerItems.value = items || [];
viewerStart.value = index;
viewerShow.value = true;
openMediaViewer(items, index);
}
// 顶部媒体轮播:单张居中、两侧露出相邻媒体边界、定时切换并带切换动画。
// 通过在首尾各复制一张「克隆」实现无缝循环:滑到克隆张后,动画结束瞬间无动画跳回真实张,
// 因此无论手动还是自动,永远不会出现“最后一张直接跳回第一张”的违和回跳。
// 后台设置 carouselInterval = 单张停留秒数(支持小数),0 表示不自动切换(仍可手动循环)。
const carouselEl = ref<HTMLElement | null>(null);
const carouselInterval = ref(3); // 秒/张,来自 /settings/public
const pos = ref(0); // 当前在 loopSlides 中的下标
const viewW = ref(0); // 容器宽度
const slideW = ref(0); // 单张布局宽度
const stride = ref(0); // 相邻两张中心间距(含间隙)
const dragDelta = ref(0); // 拖动中的临时位移
const dragging = ref(false);
const noAnim = ref(true); // true 时禁用过渡(用于克隆瞬移、初始化、resize、拖动)
let autoTimer: ReturnType<typeof setTimeout> | undefined;
let ro: ResizeObserver | undefined;
let startX = 0;
let startY = 0;
let axisLocked = false;
let moved = false;
const carouselCount = computed(() => data.value?.artwork?.images?.length || 0);
// 首尾各加一张克隆:[克隆(末), 真0..真N-1, 克隆(首)]
const loopSlides = computed<{ media: any; real: number }[]>(() => {
const imgs = data.value?.artwork?.images || [];
if (imgs.length <= 1) return imgs.map((m: any, idx: number) => ({ media: m, real: idx }));
const arr: { media: any; real: number }[] = [];
arr.push({ media: imgs[imgs.length - 1], real: imgs.length - 1 });
imgs.forEach((m: any, idx: number) => arr.push({ media: m, real: idx }));
arr.push({ media: imgs[0], real: 0 });
return arr;
});
// 当前真实下标(用于计数 / 圆点高亮)
const carouselIndex = computed(() => {
const n = carouselCount.value;
if (n <= 1) return 0;
return ((pos.value - 1) % n + n) % n;
});
const intervalMs = computed(() => Math.round(carouselInterval.value * 1000));
const autoEnabled = computed(() => intervalMs.value > 0 && carouselCount.value > 1);
// 轨道位移:把第 pos 张居中;拖动时叠加 dragDelta
const baseTrackX = computed(() => (viewW.value - slideW.value) / 2 - pos.value * stride.value);
const trackStyle = computed(() => ({
transform: `translateX(${baseTrackX.value + dragDelta.value}px)`,
transitionDuration: noAnim.value || dragging.value ? '0ms' : '450ms',
}));
function measureCarousel() {
const el = carouselEl.value;
if (!el) return;
viewW.value = el.clientWidth;
const track = el.querySelector('.carousel-track') as HTMLElement | null;
const slides = track?.children;
if (slides && slides.length) {
const a = slides[0] as HTMLElement;
slideW.value = a.offsetWidth; // offset* 取布局尺寸,不受 CSS transform 缩放影响
stride.value =
slides.length > 1 ? (slides[1] as HTMLElement).offsetLeft - a.offsetLeft : a.offsetWidth;
}
}
function clearAuto() {
if (autoTimer) {
clearTimeout(autoTimer);
autoTimer = undefined;
}
}
function scheduleAuto() {
clearAuto();
if (!autoEnabled.value) return;
autoTimer = setTimeout(() => next(), intervalMs.value);
}
function pauseAuto() {
clearAuto();
}
function resumeAuto() {
scheduleAuto();
}
// 切换到 loopSlides 的第 p 张(带动画),并重置自动计时
function go(p: number) {
pos.value = p;
scheduleAuto();
}
function next() {
go(pos.value + 1);
}
// 跳到真实第 i 张
function goToReal(i: number) {
go(i + 1);
}
// 动画结束后若停在克隆张,则无动画瞬移到对应真实张,实现无缝循环
async function onTransitionEnd(e: TransitionEvent) {
// 仅响应轨道自身的 transform 过渡;忽略子卡片缩放过渡的冒泡事件
if (e.target !== e.currentTarget || e.propertyName !== 'transform') return;
const n = carouselCount.value;
if (n <= 1) return;
let target = pos.value;
if (pos.value > n) target = pos.value - n; // 越过末尾克隆 → 回到真实首部
else if (pos.value < 1) target = pos.value + n; // 越过开头克隆 → 回到真实尾部
if (target === pos.value) return;
noAnim.value = true;
pos.value = target;
await nextTick();
requestAnimationFrame(() =>
requestAnimationFrame(() => {
noAnim.value = false;
}),
);
}
function onSlideClick(i: number, slide: { media: any; real: number }) {
if (moved) {
moved = false;
return; // 刚才是拖动,不触发点击
}
if (i === pos.value) {
openViewer(data.value?.artwork?.images || [], slide.real); // 点当前张 → 打开查看器
} else {
go(i); // 点两侧露出的相邻张 → 切到它
}
}
function onDragStart(e: PointerEvent) {
if (carouselCount.value <= 1) return;
dragging.value = true;
moved = false;
axisLocked = false;
startX = e.clientX;
startY = e.clientY;
dragDelta.value = 0;
pauseAuto();
// 注意:不在此处 setPointerCapture,否则会把后续 click 改派到容器,
// 导致 PC 上卡片的 @click 不触发、无法点击查看。仅在确认拖动后再捕获。
}
function onDragMove(e: PointerEvent) {
if (!dragging.value) return;
const dx = e.clientX - startX;
const dy = e.clientY - startY;
if (!axisLocked) {
if (Math.abs(dx) < 6 && Math.abs(dy) < 6) return;
axisLocked = true;
if (Math.abs(dy) > Math.abs(dx)) {
// 纵向手势 → 放弃拖动,交还页面滚动
dragging.value = false;
dragDelta.value = 0;
return;
}
// 确认为横向拖动后再捕获指针,保证拖出元素也能继续跟手
carouselEl.value?.setPointerCapture?.(e.pointerId);
}
moved = true;
dragDelta.value = dx;
}
function onDragEnd(e?: PointerEvent) {
if (!dragging.value) return;
dragging.value = false;
const dd = dragDelta.value;
dragDelta.value = 0;
if (e && carouselEl.value?.hasPointerCapture?.(e.pointerId)) {
carouselEl.value.releasePointerCapture(e.pointerId);
}
const threshold = Math.max(40, slideW.value * 0.18);
if (dd <= -threshold) go(pos.value + 1);
else if (dd >= threshold) go(pos.value - 1);
else scheduleAuto(); // 位移不够 → 回弹(pos 不变,过渡自动复位),恢复自动
}
function onCarouselResize() {
noAnim.value = true;
measureCarousel();
requestAnimationFrame(() =>
requestAnimationFrame(() => {
noAnim.value = false;
}),
);
}
// 数据 / 间隔变化时重置轮播
async function resetCarousel() {
clearAuto();
ro?.disconnect();
pos.value = carouselCount.value > 1 ? 1 : 0;
noAnim.value = true;
dragDelta.value = 0;
await nextTick();
measureCarousel();
if (carouselEl.value) {
ro = new ResizeObserver(onCarouselResize);
ro.observe(carouselEl.value);
}
requestAnimationFrame(() =>
requestAnimationFrame(() => {
noAnim.value = false;
scheduleAuto();
}),
);
}
function toggleLang() {
setLocale(locale.value === 'zh-CN' ? 'en-US' : 'zh-CN');
@@ -270,7 +515,6 @@ async function copy(text?: string) {
async function load(code: string) {
loading.value = true;
data.value = null;
viewerShow.value = false;
expanded.value = new Set();
try {
data.value = await fetchTrace(code);
@@ -285,11 +529,23 @@ onMounted(async () => {
try {
const info = await fetchAppInfo();
appName.value = info?.appName || t('app.name');
const iv = Number(info?.carouselInterval);
if (Number.isFinite(iv) && iv >= 0) carouselInterval.value = iv;
} catch {
appName.value = t('app.name');
}
});
// 数据或间隔变化时重置轮播
watch([() => data.value?.artwork?.images, carouselInterval], () => {
resetCarousel();
});
onUnmounted(() => {
clearAuto();
ro?.disconnect();
});
watch(
() => route.params.code,
(code) => {
@@ -401,23 +657,120 @@ watch(
word-break: break-all;
}
.carousel {
display: flex;
gap: 8px;
overflow-x: auto;
scroll-snap-type: x mandatory;
padding: 0 12px;
-webkit-overflow-scrolling: touch;
}
.carousel-item {
position: relative;
width: 86%;
overflow: hidden;
touch-action: pan-y; /* 纵向交还页面滚动,横向手势由 JS 处理 */
user-select: none;
}
.carousel-track {
display: flex;
gap: 12px;
align-items: center;
transition-property: transform;
transition-timing-function: cubic-bezier(0.22, 0.61, 0.36, 1);
will-change: transform;
}
.carousel-slide {
position: relative;
flex: 0 0 80%;
max-width: 420px;
height: 240px;
border-radius: 14px;
scroll-snap-align: center;
flex-shrink: 0;
background: #e7ecf3;
overflow: hidden;
cursor: pointer;
/* 非当前张略缩小并降低不透明度,突出居中张 */
transform: scale(0.9);
opacity: 0.55;
transition:
transform 0.45s cubic-bezier(0.22, 0.61, 0.36, 1),
opacity 0.45s ease;
}
.carousel-slide.current {
transform: scale(1);
opacity: 1;
}
/* 左右切换箭头 */
.carousel-nav {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 36px;
height: 56px;
border: none;
border-radius: 10px;
background: rgba(15, 123, 99, 0.42);
color: #fff;
font-size: 26px;
line-height: 1;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
z-index: 2;
opacity: 0;
transition: opacity 0.2s ease;
-webkit-tap-highlight-color: transparent;
}
.carousel:hover .carousel-nav {
opacity: 1;
}
.carousel-nav.prev {
left: 14px;
}
.carousel-nav.next {
right: 14px;
}
.carousel-nav:active {
background: rgba(15, 123, 99, 0.7);
}
/* 触摸设备没有 hover:常驻显示,方便点击 */
@media (hover: none) {
.carousel-nav {
opacity: 0.85;
}
}
/* 媒体计数 + 当前位置指示 */
.carousel-ind {
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
margin-top: 10px;
}
.carousel-dots {
display: flex;
align-items: center;
gap: 6px;
}
.carousel-dots .dot {
width: 7px;
height: 7px;
padding: 0;
border: none;
border-radius: 50%;
background: #cfd8e3;
cursor: pointer;
transition:
width 0.25s ease,
background 0.25s ease;
}
.carousel-dots .dot.active {
width: 18px;
border-radius: 4px;
background: #0f7b63;
}
.carousel-count {
display: inline-flex;
align-items: center;
gap: 3px;
font-size: 12px;
color: #6b7785;
font-variant-numeric: tabular-nums;
}
.carousel-count-ico {
font-size: 10px;
color: #0f7b63;
}
.carousel-media {
width: 100%;