css - 模态打开时防止滚动
问题描述
我的反应应用程序上有一个模态,但是当模态处于活动状态时,我正在努力停用溢出。我正在使用以下逻辑:
零件:
import React, { Component } from 'react';
import classes from './Modal.css';
import BackDrop from '../Backdrop/Backdrop';
class Modal extends Component {
componentDidUpdate() {
if (this.props.show) {
document.body.style.overflow = 'hidden';
}
}
shouldComponentUpdate(nextProps, nextState) {
return (
nextProps.show !== this.props.show ||
nextProps.children !== this.props.children
);
}
render() {
return (
<div>
<BackDrop show={this.props.show} clicked={this.props.clicked} />
<div
className={
this.props.singleItemOptions ? classes.ItemOptions : classes.Modal
}
style={{
transform: this.props.show ? 'translateY(0)' : 'translateY(-100vh)',
opacity: this.props.show ? '1' : '0',
}}
>
{this.props.children}
</div>
</div>
);
}
}
export default Modal;
我检查了 DOM 树,当模式处于活动状态时,样式属性出现在 body 元素标记中。不知道可能是什么问题。我期待这会起作用,因为其他人使用它。这可能与我的应用程序上的其他 CSS 样式有关吗?
解决方案
推荐阅读
- vtiger - 在 Vtiger7 中,vtigercrm/index.php?module=Leads&view=Edit 的 edit.js 文件的位置是什么
- selenium-webdriver - 如果不执行其他操作(例如清除字段),如果表值等于预期值,则想要执行操作
- mongoengine - 烧瓶棉花糖自定义字段
- rest - 当我在 Java 中使用 @jsonPropretyorder 时,属性显示 2 次
- arrays - Powershell Group-对象数组列表
- c# - 是否可以在 C# 中动态启用和禁用 CORS?
- javascript - URL 链接到 # 页面位置和 Owl Carousel 滑块位置
- android - Couchbase Mobile 2.0 在 MutableDocument 中设置数组
- ajax - 如何使用 Bootstrap 4 为表中的行创建模式
- javascript - 点击表单标签向上移动但当用户点击输入字段时标签不会恢复正常