首页 > 解决方案 > 如何让 MuiList 自动滚动

问题描述

我正在创建一个聊天窗口组件,并且正在使用 Material UI。我原以为在列表周围的 MuiList 或 MuiBox 上放置一个高度或最大高度会使其在每次发送新消息时自动滚动,但它似乎并没有奏效。我对此感到有些困惑,任何方向都会很棒。下面是我的代码。我正在使用 Styled Components 进行样式设置。

export const ChatWindow = styled(MuiBox)`
  && {
    position: absolute;
    right: 0px;
    bottom: 0px;
    box-shadow: 0px 7px 40px 2px rgba(148, 149, 150, 0.3);
    background: ${Color.white};
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: 0.3s ease-in-out;
    border-radius: 5px;
    width: ${props => (props.fullscreen ? '100%' : `${450}px`)};
    height: ${props => (props.fullscreen ? '100%' : `${584}px`)};
    /* max-height: 550px; */
  }
`;

export const ChatWindowHeader = styled(MuiBox)`
  && {
    background: ${Color.primary};
    min-height: 40px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    color: white;
    padding: 10px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
`;

export const MessageList = styled(MuiList)`
  && {
    height: 80%;
    overflow-y: auto;
  }
`;

export const MessageListItem = styled(MuiListItem)`
  && {
    margin: auto;
    padding-bottom: 10px;
    display: flex;
  }
`;

export const MessageReceivedBubble = styled(MuiBox)`
  && {
    padding: 8px 16px;
    margin-bottom: 10px;
    border-radius: 10px 10px 10px 2px;
    font-weight: 300;
    font-size: 14px;
    word-wrap: break-word;
  }
`;

export const MessageSentBubble = styled(MuiBox)`
  && {
    padding: 8px 16px;
    margin-bottom: 10px;
    border-radius: 10px 10px 2px 10px;
    font-weight: 300;
    font-size: 14px;
    word-wrap: break-word;
  }
`;

标签: javascriptcssreactjsmaterial-uistyled-components

解决方案


通过使用 Diego Lara 在这个堆栈溢出问题上的答案,我能够完成我想要的事情:How to scroll to bottom in react?


推荐阅读