使用Reactjs Material-UI滚动时如何自动隐藏AppBar

时间:2019-01-27 10:04:18

标签: reactjs scroll material-design material-ui

如图1所示滚动时,如何自动隐藏Reactjs <AppBar/>组件?

图。1。 <AppBar/>在滚动时自动隐藏

enter image description here

I am using Material-UI and my code is as follows.

MyAppBar.js
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';

const styles = {
  root: {
    flexGrow: 1,
  },
  grow: {
    flexGrow: 1,
  },
  menuButton: {
    marginLeft: -12,
    marginRight: 20,
  },
};

function ButtonAppBar(props) {
  const { classes } = props;
  return (
    <div className={classes.root}>
      <AppBar position="static">
        <Toolbar>
          <IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
            <MenuIcon />
          </IconButton>
          <Typography variant="h6" color="inherit" className={classes.grow}>
            News
          </Typography>
          <Button color="inherit">Login</Button>
        </Toolbar>
      </AppBar>
    </div>
  );
}

ButtonAppBar.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(ButtonAppBar);

2 个答案:

答案 0 :(得分:0)

我认为该模块将为您提供帮助 react-headroom

答案 1 :(得分:0)

您可以结合使用SlideuseScrollTrigger

要点是,useScrollTrigger(默认情况下)将在您向下滚动且窗口垂直滚动条位置达到某个阈值时返回(true像素)(默认情况下) -向上滚动时返回100。因此,这就是我们拒绝使用false的{​​{1}}道具的原因。

in

Edit Material demo (forked)

您可以使用我在此答案顶部提供的与Slide相关的链接来自定义export default function HideAppBar() { const trigger = useScrollTrigger(); return ( <> <Slide appear={false} direction="down" in={!trigger}> <AppBar> <Toolbar> <Typography variant="h6">Scroll Down to Hide App Bar</Typography> </Toolbar> </AppBar> </Slide> ... </> ); }

之类的选项。

参考:https://material-ui.com/components/app-bar/#hide-app-bar