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;
    }
  • 相关阅读:
    实际运用中DataSet、DataTable、DataRow点滴
    SQL语句AND 和 OR执行的优先级
    CS程序,服务器端弹出MessageBox.Show()之类的UI操作???禁止
    使用动态SQL语句实现简单的行列转置(动态产生列)
    表的行列转置
    统计每种车型的数量
    由CAST()函数在.NET1.1和.NET4.0下处理机制不同所引发的BUG
    转载——网站重构的8点建议
    float,double和decimal类型
    优化DB2缓冲页的大小
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13704536.html
Copyright © 2011-2022 走看看