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 }, //
})
const emit = defineEmits(['cardClick'])
const emit = defineEmits(['cardClick', 'scroll'])
// ========== ==========
const rpx2px = (rpx) => Math.round(uni.getSystemInfoSync().windowWidth / 750 * rpx)
@ -121,18 +121,44 @@ const iosScrollPaused = ref(true)
const startIOSAutoScroll = () => {
if (!isComponentMounted || !isIOS) return
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 = () => {
iosScrollPaused.value = true
clearInterval(iosScrollEmitTimer)
iosScrollEmitTimer = null
}
const pauseIOSAutoScroll = () => {
iosScrollPaused.value = true
clearInterval(iosScrollEmitTimer)
iosScrollEmitTimer = null
}
const resumeIOSAutoScroll = () => {
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 userInteracting = false
@ -141,6 +167,8 @@ let appendTimer = null // 防抖定时器
let autoScrollPos = 0 //
let momentumTimer = null // iOS/Android
let scrollUpdateTimer = null // scrollLeft Android
let iosScrollEmitTimer = null // iOS
let iosCurrentScrollPos = 0 // iOS CSS
const startAutoScroll = () => {
if (!isComponentMounted || isIOS) return
@ -269,6 +297,9 @@ const onScroll = (e) => {
if (!isComponentMounted) return
currentScrollLeft = e.detail.scrollLeft
//
emit('scroll', currentScrollLeft)
// iOS
if (isIOS && isManualScrolling) {
return

View File

@ -1,7 +1,14 @@
<template>
<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
@ -13,6 +20,7 @@
:category="activeContentTab"
:isActive="isActive"
@cardClick="handleCardClick"
@scroll="handleWaterfallScroll"
class="fall-bg"
/>
@ -105,11 +113,19 @@ const {
// banner(216+360rpx) + tab(16+80rpx) + (8rpx) 680rpx
const bannerBottomPx = computed(() => Math.round(screenWidth.value / 750 * 715))
const bgScrollLeft = ref(0)
// ========== Handlers ==========
const handleCardClick = (card) => {
// WaterfallGrid
}
const handleWaterfallScroll = (scrollLeft) => {
// 30%
bgScrollLeft.value = scrollLeft * 0.3
}
const handleActivityClick = (item) => {
uni.navigateTo({
url: `/pages/support-activity/index?id=${item.id}`,
@ -260,13 +276,19 @@ onUnmounted(() => {
margin-bottom: 8rpx;
} */
.background-fixed {
.bg-wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 110%;
z-index: 0;
overflow: hidden;
}
.background-fixed {
width: 300%;
height: 100%;
}
.nav-mask {

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 MiB