滚动时在 React Sticky 中制作 Material UI 组件(不是 AppBar)

IT技术 css reactjs material-ui
2021-04-29 05:31:15

所以我让 AppBar 工作正常。我什至没有为此使用粘性,但我认为我需要某种粘性样式来使页面中的元素在用户滚动时基本上粘在顶部。我基本上想要这个。

https://www.w3schools.com/howto/howto_css_sticky_element.asp

但是在我的 React 页面中,我希望我的ProgressBar组件保持在顶部。这个下面的组件RenderTeamSelections是一个大桌子,它会ProgressBar很快地推开视野。ProgressBar当用户从表中进行选择时,我想保持在视图中。这是相关的代码。

 return (

      <div className={rootClassName}>
        <div className="g-row">
          <div className="g-col">
            <AccountProfile {...this.props} />
            <br />
            <Team team={this.props.team} profileDetail={profileDetail} />
            <br />
            <ProgressBar {...this.props} />
            <br />
            <RenderTeamSelections {...this.props] />
          </div>
        </div>
      </div>

    );

我熟悉 withStyles 的使用,并在ProgressBar“固定”、“粘滞”和“-webkit-粘滞”等位置上进行了一些设置,但是当我滚动时还没有让它粘在顶部。任何帮助将不胜感激。我没有看到任何与此场景直接相关的 Material 文档。

2个回答

我能够让它与材料 ui 主题一起使用

这是我的风格。我添加了一个 z-index,因为<RenderTeamSelections {...this.props] />当我滚动时,我的表选择和表头数据在粘性中仍然可见

这是带有样式的最终组件。

const styles = theme => ({
    root: {
        background: 'white',
        position: '-webkit-sticky',
        position: 'sticky',
        top: 20,
        bottom: 20, 
        paddingTop: '40px',
        paddingBottom: '40px',
        zIndex: 5,
    },
    details: {
        display: 'flex'
    },
    progressWrapper: {
        marginTop: theme.spacing(2)
    },
    linearProgress: {
        height: 20
    },
});


export function ProgressBar(props) {
    const { profileDetail, classes } = props;
    const [completeness, setCompleteness] = useState(0)

    useEffect(() => {
        if (profileDetail) {
            setCompleteness(profileDetail.teamKeysTier1.split(",").filter(x => { return x.length != 0 }).length + profileDetail.teamKeysTier2.split(",").filter(x => { return x.length != 0 }).length)
        }
    }, [profileDetail])

    return (
        <Portlet className={classes.root} >
            <PortletContent >
                {completeness > 8 ?
                    <div className={classes.progressWrapper}>
                        <Typography variant="h3" color="textSecondary">Team Selection Completeness: {completeness * 10 + 10}%</Typography>
                        <br /> 
                        <br />
                        <LinearProgress
                            className={classes.linearProgress}
                            value={completeness * 10 + 10}
                            variant="determinate"
                            position="fixed"
                        /> </div> :
                    <div className={classes.progressWrapper}>
                        <Typography variant="h3" color="textSecondary">Team Selection Completeness: {completeness * 10}%</Typography>
                        <br /> 
                        <br />
                        <LinearProgress
                            className={classes.linearProgress}
                            value={completeness * 10}
                            variant="determinate"
                            position="fixed"
                        />
                    </div>}
            </PortletContent>
        </Portlet>

    )

}

export default withStyles(styles)(ProgressBar);

在大多数组件上,您可以添加{position: 'fixed'}. 所以尝试在组件样式中使用它。如果这不起作用,请尝试更改 zIndex。

例子:

<Paper style={{position: 'fixed'}}>
   <List className={classes.list} component="nav" aria-label="main mailbox folders"
      subheader={<ListSubheader component="div" id="nested-list-subheader">Navigation</ListSubheader>}>
          <ListItem button>
             <ListItemIcon>
               <SubjectIcon/>
             </ListItemIcon>
             <ListItemText primary="Overview" />
          </ListItem>
  </List>