zoukankan      html  css  js  c++  java
  • Scrollbar滚动条组件的实现

    div滚动条可以做多个区块的多内容展示

    使用方式:

    import Scrollbar from '../common/scrollbar'
    
    <Scrollbar className={styles.body} scroll={this.onScroll}>
    {内部内容}
    </Scrollbar>

    引入直接使用,内部是要展示的长内容

    styles.body设置区块占用宽高,scroll是滚动到底部时才进行触发,一般用于做滚动加载下一页数据关于滚动加载,可在之后文章加入。

    先看实现:

    div的onScroll滚动事件,当滚动到距离底部50px以内时触发外部传入的scroll里面的事件。

    Scrollbar内部的dom节点会显示在children中。

    import React from "react";
    
    export default class Scrollbar extends React.Component {
      onScroll = () => {
        const parentHeight = this.scrollbar.offsetHeight || this.scrollbar.innerHeight;
        const childrenHeight = this.view.offsetHeight;
        const scrollTop = this.scrollbar.scrollTop;
        const {scroll} = this.props;
        childrenHeight - parentHeight - scrollTop < 50 && scroll && scroll();
      };
    
      render() {
        const {className, children} = this.props;
        return <div className={className} ref={ref => this.scrollbar = ref} onScroll={this.onScroll.bind(this)}>
          <div ref={ref => this.view = ref}>
            {children}
          </div>
        </div>
      }
    }

    欢迎一起交流!
    【http://wuhairui.cnblogs.com/】

  • 相关阅读:
    tomcat与resin的比较
    Linux Resin 安装配置
    [BZOJ3456]城市规划
    ZJOI 2017 仙人掌
    「LibreOJ NOI Round #1」动态几何问题
    [SDOI2015]约数个数和
    codeforce 940F
    codeforce 940F
    codeforce 940E
    [NOI2009]植物大战僵尸
  • 原文地址:https://www.cnblogs.com/wuhairui/p/13673408.html
Copyright © 2011-2022 走看看