码迷,mamicode.com
首页 > 移动开发 > 详细

react better-scroll 编写类似手机chrome的header显示隐藏效果

时间:2018-12-21 16:58:34      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:classes   ops   scroll   ase   extends   onscroll   bounce   form   EDA   

关键代码

const H = 50; // header的高度
const H2 = H / 2;
let cy = 0;

class Home extends Component {
  @observable top = 0;
  @observable ms = 0;

  onScroll = ({ y }) => {
    let scrollLength = Math.abs(y - cy);

    if (y < cy) {
      l("paeg up");
      if (this.top >= -H) {
        const newTop = this.top - scrollLength;
        this.top = Math.max(newTop, -H);
      }
    } else if (y > cy) {
      // l("page down");
      if (this.top <= 0) {
        let newTop = this.top + scrollLength;
        this.top = Math.min(newTop, 0);
      }
    } else {
      // l("没动");
    }

    if (cy !== y) cy = y; // 保存这一次的值,在下一次作比较
  };

  onScrollEnd = ({ y }) => {
    const top = Math.abs(this.top);
    this.ms = 200;
    if (top <= H2) {
      l("show");
      this.top = 0;
    } else {
      l("hide");
      this.top = -H;
    }
    this.ms = 0;
  };

  render() {
    const { classes } = this.props;
    return (
      <div class={classes.root}>
        {/* hedaer */}
        <HomeHeader top={this.top} ms={this.ms} />

        <div className={classes.wraper}>
          <Scroll
            onScroll={this.onScroll}
            onScrollEnd={this.onScrollEnd}
            bounce={false}
          >
            <div> ... </div>
          </Scroll>
        </div>
      </div>
    );
  }
}


class HomeHeader extends Component {
  render() {
    const { top, ms } = this.props;
    return (
      <AppBar
        color="inherit"
        position="fixed"
        style={{
          transition: `transform ${ms}ms ease`,
          transform: `translateY(${top}px)`,
        }}
      > ... </AppBar>
    );
  }
}

react better-scroll 编写类似手机chrome的header显示隐藏效果

标签:classes   ops   scroll   ase   extends   onscroll   bounce   form   EDA   

原文地址:https://www.cnblogs.com/ajanuw/p/10156586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!