英文:
Unable to scroll vertically in modal in React
问题
我试图在我的React项目中显示模态框。当模态框显示时,我通过useEffect
将页面的溢出设置为隐藏(否则,它会在模态框下方显示主页面内容的滚动条)。它完美地阻止了背景内容的滚动,但问题是我无法在模态框中滚动。
以下是主页面的代码:
import React, { useState } from "react";
import { render } from "react-dom";
import MyModal from "./MyModal";
import "./index.css";
function App() {
const [showModal, setShowModal] = useState(false);
return (
<div className="wrapper">
<button
onClick={() => {
setShowModal((current) => !current);
}}
>
{`${showModal ? "隐藏模态框" : "显示模态框"}`}
</button>
{showModal && <MyModal {...{ showModal, setShowModal }} />}
<p>
Lorem Ipsum只是印刷和排版行业的虚拟文字。Lorem Ipsum自1500年代以来一直是该行业的标准虚拟文本,当时一位不知名的印刷商采用了类型的凹版并将其打乱,以制作一个类型的样本书。它不仅存活了五个世纪,还跃入了电子排版的时代,基本上保持不变。它在20世纪60年代与包含Lorem Ipsum段落的Letraset片和最近与Aldus PageMaker等桌面排版软件的版本一起被广泛推广。
</p>
</div>
);
}
render(<App />, document.getElementById("root"));
这是模态框的代码:
import { useEffect } from "react";
import "./modal.css";
const MyModal = ({ showModal, setShowModal }) => {
useEffect(() => {
document.body.style.overflowY = "hidden";
return () => {
document.body.style.overflowY = "auto";
};
}, []);
return (
<div className="modal-background">
<button
onClick={() => {
setShowModal(false);
}}
>
关闭
</button>
<p className="heading">这是模态框</p>
<p>
Lorem Ipsum只是印刷和排版行业的虚拟文字。Lorem Ipsum自1500年代以来一直是该行业的标准虚拟文本,当时一位不知名的印刷商采用了类型的凹版并将其打乱,
</p>
</div>
);
};
export default MyModal;
这是CodeSandbox实时示例链接:https://codesandbox.io/s/lockingmodal-8mvn36?file=/index.js:1160-4493
英文:
I'm trying to show modal in my React Project. I set the overflow of body to hidden through useEffect when the modal is showing (otherwise, it shows the scrollbar of main page's content beneath modal). And it prvents scrolling of background content perfectly but the issue is that I'm unable to scroll in my modal as well.
Here is the code of main page:
import React, { useState } from "react";
import { render } from "react-dom";
import MyModal from "./MyModal";
import "./index.css";
function App() {
const [showModal, setShowModal] = useState(false);
return (
<div className="wrapper">
<button
onClick={() => {
setShowModal((current) => !current);
}}
>
{`${showModal ? "Hide Modal" : "Show Modal"}`}
</button>
{showModal && <MyModal {...{ showModal, setShowModal }} />}
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five
centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release
of Letraset sheets containing Lorem Ipsum passages, and more recently
with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum.
</p>
</div>
);
}
render(<App />, document.getElementById("root"));
And this is the code for modal:
import { useEffect } from "react";
import "./modal.css";
const MyModal = ({ showModal, setShowModal }) => {
useEffect(() => {
document.body.style.overflowY = "hidden";
return () => {
document.body.style.overflowY = "auto";
};
}, []);
return (
<div className="modal-background">
<button
onClick={() => {
setShowModal(false);
}}
>
Close
</button>
<p className="heading">This is Modal</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
</p>
</div>
);
};
export default MyModal;
And here is the codesandbox live example: https://codesandbox.io/s/lockingmodal-8mvn36?file=/index.js:1160-4493
答案1
得分: 1
添加这两行到你的类名为 "modal-background" 的样式中:
height: 50vh;
overflow: auto;
英文:
add these two lines to your class "modal-background"
height:50vh;
overflow:auto;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论