feat: 添加背景滚动功能

This commit is contained in:
zheng020 2026-05-19 19:53:00 +08:00
parent 6d36f308a3
commit 7dd947ca12
3 changed files with 57 additions and 4 deletions

View File

@ -75,7 +75,7 @@ const props = defineProps({
isActive: { type: Boolean, default: true }, // isActive: { type: Boolean, default: true }, //
}) })
const emit = defineEmits(['cardClick']) const emit = defineEmits(['cardClick', 'scroll'])
// ========== ========== // ========== ==========
const rpx2px = (rpx) => Math.round(uni.getSystemInfoSync().windowWidth / 750 * rpx) const rpx2px = (rpx) => Math.round(uni.getSystemInfoSync().windowWidth / 750 * rpx)
@ -121,18 +121,44 @@ const iosScrollPaused = ref(true)
const startIOSAutoScroll = () => { const startIOSAutoScroll = () => {
if (!isComponentMounted || !isIOS) return if (!isComponentMounted || !isIOS) return
iosScrollPaused.value = false iosScrollPaused.value = false
// iOS scrollLeft
clearInterval(iosScrollEmitTimer)
iosScrollEmitTimer = setInterval(() => {
if (!isComponentMounted) return
// iOS CSS 0 totalWidth.value
const scrollDist = totalWidth.value
const duration = scrollDist / AUTO_SCROLL_SPEED_IOS
const elapsed = (Date.now() % duration)
const pos = (elapsed / duration) * scrollDist
emit('scroll', pos)
}, 16)
} }
const stopIOSAutoScroll = () => { const stopIOSAutoScroll = () => {
iosScrollPaused.value = true iosScrollPaused.value = true
clearInterval(iosScrollEmitTimer)
iosScrollEmitTimer = null
} }
const pauseIOSAutoScroll = () => { const pauseIOSAutoScroll = () => {
iosScrollPaused.value = true iosScrollPaused.value = true
clearInterval(iosScrollEmitTimer)
iosScrollEmitTimer = null
} }
const resumeIOSAutoScroll = () => { const resumeIOSAutoScroll = () => {
iosScrollPaused.value = false iosScrollPaused.value = false
// iOS
clearInterval(iosScrollEmitTimer)
iosScrollEmitTimer = setInterval(() => {
if (!isComponentMounted) return
const scrollDist = totalWidth.value
const duration = scrollDist / AUTO_SCROLL_SPEED_IOS
const elapsed = (Date.now() % duration)
const pos = (elapsed / duration) * scrollDist
emit('scroll', pos)
}, 16)
} }
let rafId = null let rafId = null
let userInteracting = false let userInteracting = false
@ -141,6 +167,8 @@ let appendTimer = null // 防抖定时器
let autoScrollPos = 0 // let autoScrollPos = 0 //
let momentumTimer = null // iOS/Android let momentumTimer = null // iOS/Android
let scrollUpdateTimer = null // scrollLeft Android let scrollUpdateTimer = null // scrollLeft Android
let iosScrollEmitTimer = null // iOS
let iosCurrentScrollPos = 0 // iOS CSS
const startAutoScroll = () => { const startAutoScroll = () => {
if (!isComponentMounted || isIOS) return if (!isComponentMounted || isIOS) return
@ -269,6 +297,9 @@ const onScroll = (e) => {
if (!isComponentMounted) return if (!isComponentMounted) return
currentScrollLeft = e.detail.scrollLeft currentScrollLeft = e.detail.scrollLeft
//
emit('scroll', currentScrollLeft)
// iOS // iOS
if (isIOS && isManualScrolling) { if (isIOS && isManualScrolling) {
return return

View File

@ -1,7 +1,14 @@
<template> <template>
<view class="square-container"> <view class="square-container">
<!-- 固定背景 --> <!-- 可横向滑动的背景 -->
<image class="background-fixed" src="/static/square/squearbj.png" mode="aspectFill" /> <view class="bg-wrapper">
<image
class="background-fixed"
:style="{ transform: `translateX(${-bgScrollLeft}px)` }"
src="/static/square/squearbj1.png"
mode="aspectFill"
/>
</view>
<!-- 横向瀑布流卡片层内部自带横向滚动 --> <!-- 横向瀑布流卡片层内部自带横向滚动 -->
<WaterfallGrid <WaterfallGrid
@ -13,6 +20,7 @@
:category="activeContentTab" :category="activeContentTab"
:isActive="isActive" :isActive="isActive"
@cardClick="handleCardClick" @cardClick="handleCardClick"
@scroll="handleWaterfallScroll"
class="fall-bg" class="fall-bg"
/> />
@ -105,11 +113,19 @@ const {
// banner(216+360rpx) + tab(16+80rpx) + (8rpx) 680rpx // banner(216+360rpx) + tab(16+80rpx) + (8rpx) 680rpx
const bannerBottomPx = computed(() => Math.round(screenWidth.value / 750 * 715)) const bannerBottomPx = computed(() => Math.round(screenWidth.value / 750 * 715))
const bgScrollLeft = ref(0)
// ========== Handlers ========== // ========== Handlers ==========
const handleCardClick = (card) => { const handleCardClick = (card) => {
// WaterfallGrid // WaterfallGrid
} }
const handleWaterfallScroll = (scrollLeft) => {
// 30%
bgScrollLeft.value = scrollLeft * 0.3
}
const handleActivityClick = (item) => { const handleActivityClick = (item) => {
uni.navigateTo({ uni.navigateTo({
url: `/pages/support-activity/index?id=${item.id}`, url: `/pages/support-activity/index?id=${item.id}`,
@ -260,13 +276,19 @@ onUnmounted(() => {
margin-bottom: 8rpx; margin-bottom: 8rpx;
} */ } */
.background-fixed { .bg-wrapper {
position: absolute; position: absolute;
top: 0; top: 0;
left: 0; left: 0;
width: 100%; width: 100%;
height: 110%; height: 110%;
z-index: 0; z-index: 0;
overflow: hidden;
}
.background-fixed {
width: 300%;
height: 100%;
} }
.nav-mask { .nav-mask {

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 MiB