简体   繁体   English

如何只允许某些组件滚动并保持某些 React 组件固定?

[英]How can I only allow certain components to scroll and keep certain React components fixed?

I am trying to create my own version of the Spotify web app with React, and I have made the necessary components for the player, the menu on the left side, and the actual pages for the artists, tracks, and other sites, but I am trying to position them together.我正在尝试使用 React 创建我自己版本的 Spotify Web 应用程序,并且我已经为播放器制作了必要的组件、左侧的菜单以及艺术家、曲目和其他网站的实际页面,但是我我试图将它们放在一起。 How do I set it up so you can scroll only a certain part of the page like in the app.我如何设置它以便您只能像在应用程序中一样滚动页面的特定部分。 I used postion: fixed;我使用了postion: fixed; for the menu and player to stay static while scrolling the page, but now the other components showing info like albums and playlists will overlap the player and menu.让菜单和播放器在滚动页面时保持静止,但现在显示专辑和播放列表等信息的其他组件将与播放器和菜单重叠。 How do I fix this so that the menu and player do not overlap the other components and the scrolling bar does not go on top of the player once I reach a certain point?如何解决此问题,以便菜单和播放器不会与其他组件重叠,并且一旦到达某个点,滚动条就不会位于播放器的顶部?

Here is a picture for reference of what it should look like.这是一张图片,供参考它应该是什么样子。 The right side shows the scrolling bar and will not overlap with the player ever.右侧显示滚动条,永远不会与播放器重叠。 在此处输入图片说明

This is what my app looks like when I use just a flexbox with the components on the page.当我只使用带有页面组件的 flexbox 时,这就是我的应用程序的样子。 The player and menu on the left hand side will go away once you scroll down far enough and only the menu with the artists and albums will show on screen.一旦向下滚动到足够远,左侧的播放器和菜单就会消失,屏幕上只会显示艺术家和专辑的菜单。 在此处输入图片说明

This third picture shows what happens when I do position:fixed with the menu and player where it bleeds into the other components like the search list, but if you scroll down, the player and menu will stay on the screen while the search component moves.第三张图显示了当我执行 position:fixed 时会发生什么:固定菜单和播放器,它会渗透到其他组件(如搜索列表)中,但如果向下滚动,则播放器和菜单将在搜索组件移动时保留在屏幕上。

return (
        <div className="App">
            <div className="Menu">
                <Menu />
                <Player className="Player" callbackFromParent={this.callBack}/>
            </div>
            <div className="Component">
                <Router>
                <Route path='/ArtistPage' exact component={ArtistPage} />
                <Route path="/search" exact component={Search} />
                <Route path="/AlbumPage" exact component={AlbumPage}/>
                <Route path="/profile" exact component={Profile} />
                <Route path="/playlistPage" exact component={PlaylistPage}/>
                </Router>
            </div>
        </div>
      );

This is my code where I have tried to separate the menu and player together with the rest of the components that change with the page changing.这是我的代码,我试图将菜单和播放器与随页面更改而更改的其余组件分开。

.App {
  display: flex;
  flex-direction: row;
}

.Component {
  vertical-align: left;
}

.Menu {
  display: flex;
  position: fixed;
  flex-direction: column;
}

And this is the css code that shows the formatting for the after picture above of the components bleeding into each other.这是 css 代码,它显示了上面的组件相互渗透后图片的格式。 Thank you for reading.感谢您的阅读。

position: fixed removes the element from the normal flow of the document.位置:固定从文档的正常流中删除元素。

As a result, rest of the elements behaves as if the element (with position: fixed) does not exist leading to overlapping.结果,其余元素的行为就好像元素(位置:固定)不存在导致重叠。

You will have to provide margin-left to the .Component div equal to the width of the .Menu div您必须为 .Component div 提供 margin-left 等于 .Menu div 的宽度

You can try wraping the content of the cards inside a div with height: 100% , and then add overflow-y: auto to your div.您可以尝试将卡片的内容包装在height: 100%的 div 中,然后将overflow-y: auto添加到您的 div。 So the sidebar of your app won`t move.所以你的应用的侧边栏不会移动。

Something like this:像这样的东西:

<div class="app"> 
  <div class="sidebar">
    <!-- Sidebar not scrollable -->
  </div>
  <div class="main-content">
    <!-- All scrollable content should be put in here -->
  </div>
</div>
.app {
  width: 100vw;
  height: 100vh;
}
.sidebar {
  height: 100%;
}
.main-content {
  height: 100%;
  overflow-y: auto; // You can use scroll too
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM