Commit 4791f652 authored by hewenhao's avatar hewenhao

feat: tag animate

parent 7b758342
import assets from '@/assets/oss/assets-main' import assets from '@/assets/oss/assets-main'
import { createContext } from 'react'
export const SceneContext = createContext<{ ready: boolean }>({ ready: false })
export type ImageSource = { export type ImageSource = {
w: number; w: number;
......
import { Image } from '@tarojs/components'
import { useEffect, useState, useRef, useMemo } from 'react'
import { usePageScroll, createIntersectionObserver, getCurrentInstance, getMenuButtonBoundingClientRect } from '@tarojs/taro'
type RoundTagProps = {
url: string;
className: string;
id: number | string;
ready: boolean;
}
const RoundTag = (props: RoundTagProps) => {
const nav = useRef(getMenuButtonBoundingClientRect())
const page = useMemo(() => getCurrentInstance().page, [])
const { url, className, id, ready } = props
const scrollRef = useRef(0)
const lastEffectedScroll = useRef(0)
usePageScroll(payload => {
scrollRef.current = payload.scrollTop
})
const uid = useMemo(() => `tag_${id}`, [id])
useEffect(() => {
let observer
if (ready) {
lastEffectedScroll.current = scrollRef.current
const navVal = nav.current
observer = (createIntersectionObserver as any)().relativeToViewport({ bottom: 0, top: -navVal.height - navVal.top - 5 })
.observe(`#${uid}`, ({ intersectionRatio }) => {
const show = intersectionRatio > 0
const isDown = lastEffectedScroll.current < scrollRef.current
if (show && !isDown) {
page?.animate?.(`#${uid}`, [
{ opacity: 0, translateY: 20, scale: [1, 1] },
{ opacity: 1, translateY: 0, scale: [1, 1] },
{ opacity: 1, translateY: 0, scale: [1.2, 1.2], offset: 0.8 },
{ opacity: 1, translateY: 0, scale: [1, 1] },
], 1200, () => {
page?.clearAnimation?.(`#${uid}`, () => {})
})
}
lastEffectedScroll.current = scrollRef.current
})
}
return () => {
if (observer) {
observer.disconnect()
observer = null
}
}
}, [uid, ready, page])
return (
<Image src={url} className={className} id={uid}></Image>
)
}
export default RoundTag
import { View, Image } from '@tarojs/components' import { View, Image } from '@tarojs/components'
import assets from '@/assets/oss/assets-round' import assets from '@/assets/oss/assets-round'
import classNames from 'classnames' import classNames from 'classnames'
import { RoundState, type Round, RoundType, type ImageSource, RoundScore, CurrentRoundId } from './constants' import { RoundState, type Round, RoundType, type ImageSource, RoundScore, CurrentRoundId, SceneContext } from './constants'
import RoundTag from './round-tag'
import styles from './round.module.scss' import styles from './round.module.scss'
...@@ -99,7 +100,7 @@ const getIconClassNames = (type?: RoundType, extra: string[] = []) => { ...@@ -99,7 +100,7 @@ const getIconClassNames = (type?: RoundType, extra: string[] = []) => {
const RoundComp = (props: RoundProps) => { const RoundComp = (props: RoundProps) => {
const { round } = props const { round } = props
const renderRound = () => { const renderRound = (ready: boolean) => {
switch (round.state) { switch (round.state) {
case RoundState.Locked: { case RoundState.Locked: {
return ( return (
...@@ -118,7 +119,7 @@ const RoundComp = (props: RoundProps) => { ...@@ -118,7 +119,7 @@ const RoundComp = (props: RoundProps) => {
return ( return (
<View className={styles.clear}> <View className={styles.clear}>
{!!tag && ( {!!tag && (
<Image src={tag.u} className={styles.tag}></Image> <RoundTag url={tag.u} className={styles.tag} id={round.id} ready={ready} />
)} )}
<Image src={getScoreIcon(round.score).u} className={styles.score} /> <Image src={getScoreIcon(round.score).u} className={styles.score} />
<Image src={getClearIcon(round.type).u} className={getIconClassNames(round.type, [styles.clearIcon])} /> <Image src={getClearIcon(round.type).u} className={getIconClassNames(round.type, [styles.clearIcon])} />
...@@ -139,10 +140,14 @@ const RoundComp = (props: RoundProps) => { ...@@ -139,10 +140,14 @@ const RoundComp = (props: RoundProps) => {
} }
return ( return (
<SceneContext.Consumer>
{({ ready }) => (
<View className={props.className} id={round.state === RoundState.UnLocked ? CurrentRoundId : undefined}> <View className={props.className} id={round.state === RoundState.UnLocked ? CurrentRoundId : undefined}>
{renderRound()} {renderRound(ready)}
<View className={styles.roundName}>{round.name}</View> <View className={styles.roundName}>{round.name}</View>
</View> </View>
)}
</SceneContext.Consumer>
) )
} }
......
...@@ -22,6 +22,7 @@ const ShortCut = (props: ShortCutProps) => { ...@@ -22,6 +22,7 @@ const ShortCut = (props: ShortCutProps) => {
}) })
useEffect(() => { useEffect(() => {
lastEffectedScroll.current = scrollRef.current
let observer = (createIntersectionObserver as any)().relativeToViewport({ bottom: 0 }) let observer = (createIntersectionObserver as any)().relativeToViewport({ bottom: 0 })
.observe(`#${CurrentRoundId}`, ({ intersectionRatio }) => { .observe(`#${CurrentRoundId}`, ({ intersectionRatio }) => {
const show = intersectionRatio > 0 const show = intersectionRatio > 0
......
import { View } from '@tarojs/components' import { View } from '@tarojs/components'
import { getMenuButtonBoundingClientRect, useDidShow, getCurrentInstance, getTabBar, pageScrollTo, getSystemInfoSync } from '@tarojs/taro' import { getMenuButtonBoundingClientRect, useDidShow, getCurrentInstance, getTabBar, pageScrollTo, getSystemInfoSync } from '@tarojs/taro'
import { useState, useMemo, useEffect, useCallback } from 'react' import { useState, useMemo, useEffect, useCallback } from 'react'
import { CurrentRoundId } from './components/scene/constants' import { CurrentRoundId, SceneContext } from './components/scene/constants'
import MainHeader from './components/header' import MainHeader from './components/header'
import MainChapter from './components/chapter' import MainChapter from './components/chapter'
import MainScene from './components/scene' import MainScene from './components/scene'
...@@ -41,7 +41,9 @@ const MainPage = () => { ...@@ -41,7 +41,9 @@ const MainPage = () => {
<MainHeader /> <MainHeader />
<View className={styles.body} style={{ paddingTop: `${nav.height + nav.top + 5}px` }}> <View className={styles.body} style={{ paddingTop: `${nav.height + nav.top + 5}px` }}>
<MainChapter /> <MainChapter />
<SceneContext.Provider value={{ ready: isReady }}>
<MainScene /> <MainScene />
</SceneContext.Provider>
<View className={styles.gap}></View> <View className={styles.gap}></View>
</View> </View>
{isReady && ( {isReady && (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment