React Typescript音乐播放器项目笔记:6、scroll组件:移动端滚动

19 篇文章 0 订阅
17 篇文章 0 订阅

项目地址https://github.com/BUPTlhuanyu/react-music-lhy

scroll组件:移动端滚动

import React, {Component} from 'react'
import BScroll from 'better-scroll'

interface scrollProps{
    children:any,
    scrollStyle:any
}

interface scrollState{
    click:boolean,
    probeType:number
}

export default class Scroll extends Component<scrollProps, scrollState>{
    wrapper:any;
    wrapperBs:any;
    constructor(props: scrollProps){
        super(props);
        this.wrapper = React.createRef();
        this.wrapperBs = null;
        this.state = {
            click:true,
            probeType:1
        }
    }
    componentDidMount(){
        setTimeout(() => {
            this._initScroll()
        }, 20)
    }

    _initScroll() {
        if (!this.wrapper.current) {
            return
        }
        this.wrapperBs = new BScroll(this.wrapper.current, {
            probeType: this.state.probeType,
            click: this.state.click
        })
    }
    refresh() {
        this.wrapperBs && this.wrapperBs.refresh()
    }
    render(){
        return (
            <div ref={this.wrapper} className={this.props.scrollStyle}>
                {this.props.children}
            </div>
        )
    }
}

在Recommend.tsx组件中用Scroll组件如下:

        <div className="recommend">
                <Scroll scrollStyle="recommend-content">
                    <div>
                        <div className="slider-wrapper">
							...
                        </div>
                        <div className="recommend-list">
							...
                        </div>
                    </div>
                </Scroll>
        </div>

打印this.wrapperBs如下:

其中

wrapperheight:579
scrollerHeight:2705
hasVerticalScroll:true

wrapperheight为BScroll.wrapper存储的DOM节点的高度,即

<Scroll scrollStyle="recommend-content">
	...
</Scroll>

scrollerHeight为BScroll.scroller存储的DOM节点的高度,即Scroll组件的第一个子节点,即

<div>
	...
</div>

只有wrapperheight比scrollerHeight小的时候,hasVerticalScroll为true,从而运行滑动。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值