react 中封裝一個簡單的滾動條組件


import React, { Component } from 'react'
//引入better-scroll
import BScroll from 'better-scroll'
import './style.scss'
export default class AppScroll extends Component {
    //利用ref精准的獲取dom元素
    wrapper = React.createRef();
    render() {
        return (
            <div className="scroll-wrapper" ref={this.wrapper}>
              <div className="scroll-content">
                  {this.props.children}
              </div>  
            </div>
        )
    }

    componentDidMount(){
        let scroll = new BScroll(this.wrapper.current)
           // 在用戶需要滾動前及時更新滾動視圖
        scroll.on('beforeScrollStart', ()=>{
            scroll.refresh();
        })
    }
}

 

css

 

.scroll-wrapper{
    width: 100%;
    height: 100%;
    overflow: hidden;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM