代码之家  ›  专栏  ›  技术社区  ›  user2924127

在另一个“粘性”导航栏下面有一个“粘性”按钮

  •  0
  • user2924127  · 技术社区  · 6 年前

    我有一个应用程序文件,其中包含我自己的自定义appbar和不同的页面组件:

    const styles = theme => ({
      appBar: {
              width:'100%',
      },
    });
    
    class App extends Component {
    
      render() {
        const {classes} = this.props;
        return (
          <React.Fragment>
          <CssBaseline />
          <AppBar position="sticky" className={classes.appBar} />
          <Page1 show={someCondition} />
          <Page2 show={someCondition} />
          .
          .
          <Page99 show={someCondition} />
         </React.Fragment>
        );
      }
    }
    

    Appbar是粘性的,所以它总是显示在顶部。 每个页面组件都有一个按钮,该按钮始终位于该页面的顶部:

    const styles = theme => ({
          button: {
                  width:'100%',
          },
        });
    
    class Page99 extends Component {
    
      render() {
        const {classes} = this.props;
        return (
          <React.Fragment>
             <div>
                <Button variant="contained" className= {classes.button}>
                  Action Button
                </Button>
             </div>
            {/* Some other stuff */>
         </React.Fragment>
        );
      }
    }
    

    我知道我希望这个按钮总是在应用程序栏的正下方。因此,当用户向下滚动时,这个按钮应该像appbar一样保持粘性。我试图将定位设置为Stick,希望它能堆叠在它下面,但它不会。appbar是动态的,所以我不知道它的确切高度,因为在不同的分辨率下,它看起来会不同,所以我不能使用固定定位之类的东西。

    2 回复  |  直到 5 年前
        1
  •  0
  •   Aman Seth    6 年前

    您可以将页面容器的位置设置为相对,将按钮设置为绝对。 您可以将其对齐到页面的右上角或任意位置。

    检查一下这把小提琴这是你需要的

    .componentparent {
      position: relative;
      height:100px;
      max-height: 50px;
      overflow: auto;
    }
    
    .button {
      position: fixed;
      top: 30px;
    }
    
    .otherelements{
      top: 70px;
      position: relative;
    }
    <div id="parent-container">
      <div> your app bar </div>
      <div class='componentparent'>
        <button class='button'>my button</button>
        <div class='otherelements'>your component</div>
      </div> 
    </div>
        2
  •  0
  •   Mel    6 年前

    将按钮放在appbar中,将按钮设置为绝对位置,并添加top:100%以将其精确移动到appbar的底部。