Im Reaktionsmodus kann nicht vertikal gescrollt werden
P粉760675452
P粉760675452 2023-09-16 11:18:16
0
1
1093

Ich versuche, in meinem React-Projekt ein Modal anzuzeigen. Wenn das Modal angezeigt wird, setze ich den Überlauf des Körpers über useEffect auf ausgeblendet (andernfalls wird die Bildlaufleiste für den Hauptinhalt unterhalb des Modals angezeigt). Es funktioniert perfekt, um das Scrollen des Hintergrundinhalts zu verhindern, aber das Problem ist, dass ich im Modal auch nicht scrollen kann. Der Hauptseitencode lautet wie folgt:

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"));

Dies ist der Code für das 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;

Dies ist ein Live-Beispiel von Codesandbox: https://codesandbox.io/s/lockingmodal-8mvn36?file=/index.js:1160-4493

P粉760675452
P粉760675452

Antworte allen(1)
P粉317679342

将这两行添加到您的类“modal-background”

height:50vh;
overflow:auto;
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage