【问题标题】:Semantic-UI-react fixed sidebarSemantic-UI-react 固定侧边栏
【发布时间】:2018-02-14 00:35:57
【问题描述】:

用 Google 搜索过,在语义 ui 的文档和问题页面中搜索过,并在 stackoverflow 中搜索过。找不到答案。

在 Semantic-ui-react 中,如何制作内容固定在屏幕上的侧边栏?我目前拥有的是这样的:

<Sidebar.Pushable as={Segment}>
    <Sidebar
        id="sidebar"
        as={Menu}
        animation="overlay"
        direction="right"
        visible={this.state.visible}
        vertical
        inverted
    >
        {this.getMenuItems()}
    </Sidebar>
    <Sidebar.Pusher>
        <Route path="/" component={Filler} />
    </Sidebar.Pusher>
</Sidebar.Pushable>

semantic-ui-react 文档中似乎没有任何词,并使 Sidebar.Pushable、Sidebar 或任何菜单项位置:固定;好像也没用。

【问题讨论】:

    标签: reactjs semantic-ui semantic-ui-react


    【解决方案1】:

    一切都变得更简单了!

    <Sidebar.Pusher style={{overflow: 'scroll', height: '100%'}}>
    

    我想你自己会明白为什么会这样。

    【讨论】:

      【解决方案2】:

      试试下面的代码。

      <Sidebar as={Menu} animation='overlay' icon='labeled' inverted vertical visible width='wide'>
             <Menu.Item as={Link} to="/admin">
                <Icon name='building' />
                Rubykraft
             </Menu.Item>
             <Menu.Item as='a'>
                <Icon name='user' />
                Shan
             </Menu.Item>
             <Menu.Item as='a'>
               <Icon name='user' />
               Vishnu
             </Menu.Item>
      </Sidebar>
      

      【讨论】:

      • 这应该是公认的答案。如果您想从中获得仪表板样式,也可以将其更改为 animation='push'
      【解决方案3】:

      answer 的帮助下,我能够实现一个粘性侧边栏。

      基本上,它指出,为了有一个固定的侧边栏粘在我们的无限滚动页面上,我们必须删除 transform 属性 在父容器上。推理是因为变换将定位上下文从视口更改为 旋转的元素。因此,“固定”子元素的行为就好像它具有“绝对”定位一样。

      我将此添加到 sidebar.overrides 文件中

      /* Page Context */
      .pushable:not(body) {
        transform: none;
      }
      
      .pushable:not(body) > .ui.sidebar,
      .pushable:not(body) > .fixed,
      .pushable:not(body) > .pusher:after {
        position: fixed;
      }
      

      此解决方案适用于基本语义 UI 库。由于语义-ui-react 需要语义-ui,这最终也适用于语义-ui-react 侧边栏。

      【讨论】:

      • 是的!将其放入 semantic/src/modules/sidebar.overrides 然后 gulp npx build-css。超级有用的回复:@GloriousLemon 有正确答案。
      • +1。 transform:'none' 是根据github.com/Semantic-Org/Semantic-UI-React/issues/2897 的密钥。对于不想更改 Semantic UI CSS 文件和/或安装 Gulp 的任何人,您可以在元素上添加 CSS 修改:&lt;Sidebar.Pushable style={{transform: 'none'}}&gt;
      【解决方案4】:

      我使用了来自semantic-uiSidebar 模块的类来创建所需的固定侧边栏。如果您想要更多 Component(ish) 代码,则应将 pusher 类替换为其对应的 Sidebar.Pusher 组件。

      这是我的代码:

      import React, { Component } from 'react'
      import { Dropdown, Icon, Input, Menu } from 'semantic-ui-react'
      
      export default class MySidebar extends Component {
          state = {}
      
          handleItemClick = (e, { name }) => this.setState({ activeItem: name })
      
      
          componentDidMount() {}
      
          render() {
              const { activeItem } = this.state
      
              return(
                  <div className='pusher'>
                      <div className='full height'>
                          <div className='toc'>
                              <Menu className='inverted vertical left fixed'>
                                  <Menu.Item>
                                      Home
                                      <Icon name='dashboard' />
                                      <Menu.Menu>
                                          <Menu.Item name='search' active={activeItem === 'search'} onClick={this.handleItemClick}>
                                              Search
                                          </Menu.Item>
                                          <Menu.Item name='add' active={activeItem === 'add'} onClick={this.handleItemClick}>
                                              Add
                                          </Menu.Item>
                                          <Menu.Item name='about' active={activeItem === 'about'} onClick={this.handleItemClick}>
                                              Remove
                                          </Menu.Item>
                                      </Menu.Menu>
                                  </Menu.Item>
                                  <Menu.Item name='browse' active={activeItem === 'browse'} onClick={this.handleItemClick}>
                                      <Icon name='grid layout' />
                                      Browse
                                  </Menu.Item>
                                  <Menu.Item name='messages' active={activeItem === 'messages'} onClick={this.handleItemClick}>
                                      Messages
                                  </Menu.Item>
      
                                  <Dropdown item text='More'>
                                      <Dropdown.Menu>
                                          <Dropdown.Item icon='edit' text='Edit Profile' />
                                          <Dropdown.Item icon='globe' text='Choose Language' />
                                          <Dropdown.Item icon='settings' text='Account Settings' />
                                      </Dropdown.Menu>
                                  </Dropdown>
                              </Menu>
                          </div>
                          <div className='article'>
                              <div>Content</div>
                          </div>
                      </div>
                  </div>
              )
          }
      }
      

      还有风格:

      .toc {
          width: 200px;
      }
      
      .article {
          margin-left: 210px;
      }
      

      【讨论】:

        【解决方案5】:

        您需要使用一些 CSS/SCSS 手动执行此操作。基本上,您需要将高度设置为固定值。

        @media only screen and (max-width: 768px) {
          .ui.wide.left.sidebar, .ui.wide.right.sidebar {
            height: 100vh !important;
            position: absolute;
          }
        
          .pusher {
            margin-left: 20px;
          }
        }
        
        .pushable {
          min-height: 100vh;
        }
        
        .ui.wide.left.sidebar, .ui.wide.right.sidebar {
          height: 100vh;
          position: fixed !important;
          bottom: 0px !important;
          top: 0px !important;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-09
          • 2018-06-25
          • 1970-01-01
          • 2018-11-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多