Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复滚动穿透的问题 #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,40 @@ import IDialogPropTypes from './IDialogPropTypes';
function noop() {
}

// 缓存body原有的样式属性
let originPosition;
let originWidth;
let originTop;
// body是否已设置成fixed组织滚动
let fixed = false;
// dialog实例计数
let count = 0;

function fixedBody() {
if (!fixed) { // 避免重复设置造成Body原属性获取失败
const { position, width, top } = document.body.style;
originPosition = position;
originWidth = width;
originTop = top;
const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
document.body.style.position = 'fixed';
document.body.style.width = '100%';
document.body.style.top = `-${scrollTop}px`;
fixed = true;
}
}

function looseBody() {
if (!count) { // 如果当前页面还存在Dialog实例,则不能重置
const body = document.body;
body.style.position = originPosition;
body.style.width = originWidth;
window.scrollTo(0, -parseInt(body.style.top || '0', 10));
body.style.top = originTop;
fixed = false;
}
}

export default class Dialog extends React.Component<IDialogPropTypes, any> {
static defaultProps = {
afterClose: noop,
Expand All @@ -24,7 +58,14 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
footerRef: any;
wrapRef: any;

constructor(props) {
super(props);
count++;
}

componentWillUnmount() {
count--;
looseBody();
// fix: react@16 no dismissing animation
document.body.style.overflow = '';
if (this.wrapRef) {
Expand Down Expand Up @@ -213,6 +254,7 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
const { prefixCls, maskClosable } = props;
const style = this.getWrapStyle();
if (props.visible) {
fixedBody();
style.display = null;
}
return (
Expand Down