javascript - 如何让 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;
}
`;
解决方案
通过使用 Diego Lara 在这个堆栈溢出问题上的答案,我能够完成我想要的事情:How to scroll to bottom in react?
推荐阅读
- dynamics-crm - 如何使用 REST API 检索 Dynamics Sales 数据?
- gradle - Gradle 自定义插件作为独立项目
- arrays - 数组在他们读取的子程序中自动改变
- couchdb - Couchdb 视图生成性能
- java - 如何为包含存储过程调用的方法编写 mokito junit
- rust - 如何在 *c_char 和 Vec 之间进行 memcpy
- python - 移除操作图 tensorflow 以在 CPU 上运行
- sql - 根据参数值在存储过程中使用不同的 WHERE 子句集
- javascript - Angular JS 应用程序:保存 excel 文件并稍后显示
- android - RecyclerView 上的 FirebaseRecyclerAdapter 分页