手机端弹出层滚动导致底部滚动的问题解决
问题描述:
点开弹窗时,外层html或body添加自己的class(我此处是 .stop-touching)
关闭弹窗时移除刚才的class,
上两步主要用到classList的add和remove
点开时记住外部scrollTop,
关闭时设置scrollingElement.scrollTop为刚才记下的scrollTop(我本地出现了滚动时底部正常,关闭时底部变为最上方情况)
完毕。
代码如下:
// 动态添加到body上的样式
.stop-touching {
width: 100%;
height: 100%;
overflow: hidden;
position: fixed;
}
componentDidMount() {
let scrollTop = document.scrollingElement.scrollTop;
this.setState({scrollTop});
document.body.classList.add('stop-touching');
document.body.style.top = -scrollTop + 'px';
}
componentWillUnmount() {
document.body.classList.remove('stop-touching');
document.body.style.top = null;
document.scrollingElement.scrollTop = this.state.scrollTop;
}