我试图在LeftNav中创建一个固定的标题(在本例中为Toolbar),以便当LeftNav滚动时,Toolbar保持在其位置。但是以某种方式将postion: 'fixed'应用于工具栏在LeftNav中似乎不起作用。当LeftNav中的内容大于窗口高度时,整个LeftNav(包括顶部fixed的工具栏)都会滚动。有谁知道如何在LeftNav中创建固定位置元素?

这是供参考的代码:

...
const toolBarStyle = {
  position: 'fixed',
  top: 0,
};
return (
  <LeftNav
    open={open}
    docked={false}
    onRequestChange={onRequestChange}
  >
    <Toolbar style={toolBarStyle}> />
    {this.props.children} // children could be a lot of content
  </LeftNav>
);
...

最佳答案

好,我知道了。我所做的只是将LeftNav本身设置为position: 'fixed',并在内容周围添加了包装div并设置了overflowY: 'auto'。这是代码:

......
render() {
const toolBarStyle = {
  position: 'absolute',
  top: 0,
};
const containerStyle = {
  position: 'fixed',
  top: 0,
  overflowY: 'hidden',
};
const contentContainerStyle = {
  marginTop: 57, // the same height as the toolbar
  width: '100%',
  // you can obtain the window height whatever way you want.
  // I was using Redux so I pass it down from parent component as a prop
  height: this.props.windowSize.height - 57,
  overflowY: 'auto',
};
return (
  <LeftNav
    style={containerStyle}
    docked={false}
    onRequestChange={onRequestChange}
  >
    <Toolbar style={toolBarStyle} />
    <div style={contentContainerStyle}>
      {this.props.children}
    </div>
  </LeftNav>
);
...

关于css - material-ui LeftNav中的固定 header ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36099284/

10-08 23:39