Javascript 如何在不滚动整个页面的情况下向下滚动组件
我有一个react元素,它是一个聊天框。现在我想滚动到此框的底部。当我使用scrollintoview时,它会滚动到页面底部并滚动聊天记录。我不想滚动页面Javascript 如何在不滚动整个页面的情况下向下滚动组件,javascript,reactjs,Javascript,Reactjs,我有一个react元素,它是一个聊天框。现在我想滚动到此框的底部。当我使用scrollintoview时,它会滚动到页面底部并滚动聊天记录。我不想滚动页面 class Preview extends Component { messageEndRef = React.createRef(); ......... componentDidMount = () => { this.scrollToBottom(); }; componentDidUpdate() {
class Preview extends Component {
messageEndRef = React.createRef();
.........
componentDidMount = () => {
this.scrollToBottom();
};
componentDidUpdate() {
if (this.messageEndRef.current) {
this.scrollToBottom();
}
}
.........
scrollToBottom = () => {
this.messageEndRef.current.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' });
};
.........
有没有办法只滚动预览而不滚动整个页面?您试过吗
this.messageEndRef.current.scrollTop=this.messageEndRef.current.scrollHeight
yes我尝试过这个,它阻止了聊天框和页面的滚动。但是,我想滚动消息框,保持页面位置与上一页相同