zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【LOJ#10027】魔板
    【LOJ#2653】山峰和山谷
    【POJ2449】第k短路
    【HAOI2008】移动玩具
    【洛谷P1379】八数码难题
    【NOIP2002】字串变换
    【CH2501】矩阵距离
    【CH2601】电路维修
    【NOIP2009】靶形数独
    树的子结构
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13704536.html
Copyright © 2011-2022 走看看