taro3.x: 记录Taro.createIntersectionObserver不起作用问题

tsx:

复制代码
import React, { Component } from 'react'
import Taro from '@tarojs/taro'
import { View, Text, ScrollView } from '@tarojs/components'
import classnames from 'classnames'

import NavBar from '@components/navbar'
import './index.scss'

const observer: any = null

class Test extends Component {

    constructor(props) {
        super(props)
        this.state = {
            appear: false
        }
    }
    componentDidMount() {
        Taro.createIntersectionObserver(this, { thresholds: [0, 0.25, 0.5, 0.75], observeAll: true })
            .relativeTo('.scroll-view')
            .observe('.ball', (res) => {
                console.log(res, "test");
                this.setState({
                    appear: res.intersectionRatio > 0
                })
            })
    }

    // 对应 onShow
    componentDidShow() { }

    // 对应 onHide
    componentDidHide() {
        if (observer) {
            observer.disconnect()
        }
    }

    // 对应 onError
    componentDidCatchError() { }


    render() {
        const { appear }: any = this.state
        return (
            <View className="container">
                <NavBar title="监听小球" />
                <View className="page-body">
                    <View className="page-section message">
                        <Text>
                            {appear ? '小球出现' : '小球消失'}
                        </Text>
                    </View>
                    <View className="page-section">
                        <ScrollView className={classnames('scroll-view')} scrollY>
                            <View className="scroll-area">
                                <Text className="notice">向下滚动让小球出现</Text>
                                <View className="filling"></View>
                                <View className={classnames('ball')}></View>
                            </View>
                        </ScrollView>
                    </View>
                </View>
            </View>

        )
    }
}

export default Test
复制代码

scss: 

复制代码
.scroll-view {
    height: 400px;
    background: #fff;
    border: 1px solid #ccc;
    box-sizing: border-box;
}

.scroll-area {
    height: 2600px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: 0.5s;
}

.notice {
    margin-top: 300px;
}

.ball {
    width: 400px;
    height: 400px;
    background: #1aad19;
    border-radius: 50%;
}

.filling {
    height: 800px;
}

.message {
    width: 100%;
    display: flex;
    justify-content: center;
}

.message text {
    font-size: 80px;
    font-family: -apple-system-font, Helvetica Neue, Helvetica, sans-serif;
}
复制代码

 

posted @   Nyan  阅读(1765)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示