无法在React模态框中垂直滚动。

huangapple go评论80阅读模式
英文:

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 &quot;react&quot;;
import { render } from &quot;react-dom&quot;;
import MyModal from &quot;./MyModal&quot;;
import &quot;./index.css&quot;;

function App() {
  const [showModal, setShowModal] = useState(false);
  return (
    &lt;div className=&quot;wrapper&quot;&gt;
      &lt;button
        onClick={() =&gt; {
          setShowModal((current) =&gt; !current);
        }}
      &gt;
        {`${showModal ? &quot;Hide Modal&quot; : &quot;Show Modal&quot;}`}
      &lt;/button&gt;
      {showModal &amp;&amp; &lt;MyModal {...{ showModal, setShowModal }} /&gt;}
      &lt;p&gt;
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry&#39;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.
      &lt;/p&gt;
      
    &lt;/div&gt;
  );
}

render(&lt;App /&gt;, document.getElementById(&quot;root&quot;));

And this is the code for modal:

import { useEffect } from &quot;react&quot;;
import &quot;./modal.css&quot;;

const MyModal = ({ showModal, setShowModal }) =&gt; {
  useEffect(() =&gt; {
    document.body.style.overflowY = &quot;hidden&quot;;
    return () =&gt; {
      document.body.style.overflowY = &quot;auto&quot;;
    };
  }, []);
  return (
    &lt;div className=&quot;modal-background&quot;&gt;
      &lt;button
        onClick={() =&gt; {
          setShowModal(false);
        }}
      &gt;
        Close
      &lt;/button&gt;
      &lt;p className=&quot;heading&quot;&gt;This is Modal&lt;/p&gt;
      &lt;p&gt;
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever
        since the 1500s, when an unknown printer took a galley of type and
      &lt;/p&gt;
    &lt;/div&gt;
  );
};

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;

huangapple
  • 本文由 发表于 2023年6月14日 23:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定