如图1所示滚动时,如何自动隐藏Reactjs <AppBar/>
组件?
<AppBar/>
在滚动时自动隐藏
I am using Material-UI and my code is as follows.
MyAppBar.jsimport 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);
答案 0 :(得分:0)
我认为该模块将为您提供帮助 react-headroom
答案 1 :(得分:0)
您可以结合使用Slide
和useScrollTrigger
要点是,useScrollTrigger
(默认情况下)将在您向下滚动且窗口垂直滚动条位置达到某个阈值时返回(true
像素)(默认情况下) -向上滚动时返回100
。因此,这就是我们拒绝使用false
的{{1}}道具的原因。
in
您可以使用我在此答案顶部提供的与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>
...
</>
);
}