javascript - Chrome 移动版无法正确更新 CSS 样式
问题描述
我想在将页面滚动到顶部时制作一个固定位置的粘性元素,该元素会粘在页面底部。
它在桌面 Chrome 浏览器上运行正常,但在移动设备上不行。无法找出确切的问题。是 chrome bug,我的代码错了吗?
class App extends React.Component {
state = {
position: 0
}
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll = () => {
const {scrollTop, scrollHeight} = document.documentElement;
const scroll = scrollHeight - scrollTop - window.innerHeight;
if (scroll <= 100) {
this.setState({position: 100 - scroll});
} else if (this.state.position !== 0) {
this.setState({position: 0});
}
}
render() {
return (
<section>
<div className="box" style={{
bottom: this.state.position
}}>
STICKY BOX
</div>
</section>
);
}
}
这是笔:Codepen
解决方案
推荐阅读
- javascript - React Native - 组件异常
- swift - 如何从tableViewCell内的collectionView中的firebase检索数据?
- sql-server - 如何在家用机器上打开 SQL 数据库?
- reactjs - 使用反应钩子快速解释状态管理
- lldb - LLDB - 自动步进并打印行列表,直到断点
- swift - 在同一类中显示相机的正面和背面视图
- amazon-web-services - 来自云端的 aws s3 组件访问访问被拒绝
- assembly - 了解汇编、nasm、x86 中的 printf 函数。我不知道为什么这段代码没有打印出任何东西
- javascript - 异步渲染 React 组件(等待数据返回)
- sql - SQL windows 函数 LEAD/LAG 但只考虑某些值?