gpt4 book ai didi

javascript - 如何在 React 中关闭模态时添加过渡以及如何在模态外单击时关闭?

转载 作者:行者123 更新时间:2023-12-02 21:05:52 24 4
gpt4 key购买 nike

我知道有很多问题。我已经检查过它们,但仍然找不到我的方法。不得不问这个问题,因为我被困住了,无法移动到任何地方。我是新的学习者,仍然是 React 的初学者,并尝试在我的元素中实现 Modals。这里有两个问题。

  1. 如何在关闭时添加过渡?

一旦用户单击卡片,我就会显示带有转换的模式,但是当用户关闭时,由于某种原因,我无法应用转换。

我更改模式打开或关闭的方法,并使用以下代码在 css 中进行转换:

.show .modal-parent {
animation-name: zoom;
animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}

@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}

每当用户单击卡片时,我都会显示 .show 类,并在所有模态内容所在的 .modal-parent 上应用转换。现在,当用户关闭模式时,我想做同样的事情。

  • 在外部点击时如何关闭模式?
  • App.js 文件在这里:

    import React from "react";
    import "./App.css";
    import Cards from "./components/Cards/cards.js";
    import users from "./employees.json";
    import Navbar from "./components/Navbar";
    import Modals from "./components/Modals";

    class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    index: 0,
    open: false,
    };
    this.nextPerson = this.nextPerson.bind(this);
    }

    userIndex = (cardIndex) => {
    this.setState({
    index: cardIndex,
    open: true,
    });
    };

    nextPerson = () => {
    this.setState({
    index: this.state.index + 1,
    });
    };
    previousPerson = () => {
    this.setState({
    index: this.state.index - 1,
    });
    };
    close = () => {
    this.setState({
    open: false,
    });

    };
    render() {
    let person = users[this.state.index];

    return (
    <div className="container">
    <Navbar />
    <div className="team-text"><p> Our team of <span className="team-number">42</span> strategists, designers, engineers, developers and managers<br/>make custom products for startups and leading companies. </p> </div>
    <div className="top-card">
    {users.map((user) => {
    return (
    <Cards
    user={user}
    users={users}
    key={user.id}
    userIndex={this.userIndex}
    />
    );
    })}
    <Modals
    open={this.state.open}
    users={users}
    >
    <div class="modal-parent">
    <div className={`modal-nav ${person.department === "Engineering" ? "engineer" : ""} ${person.department === "Business" ? "business" : ""}${person.department === "Design" ? "design" : ""}`}>
    <div className="modal-close">
    <a
    href="#close"
    title="Close"
    className="close"
    type="button"
    onClick={this.close}
    >
    Close
    </a>
    </div>{" "}
    </div>

    <div className="modal-image">
    <img src={person.avatar} alt="" class="modal-avatar"></img>{" "}
    </div>
    <div> </div>
    <div className="modal-info">
    <h1 className="modal-name">
    {person.firstName} {person.lastName}
    </h1>
    <h5 className="modal-title">{person.jobTitle}</h5>
    <h5 className="modal-department">{person.department}</h5>
    </div>
    <div className="modal-bio">
    <p>{person.bio}</p>
    </div>
    <div className="modal-contacts">
    <a href={`mailto: ${person.contact.phone}`}>
    <span className={`material-icons phone ${person.department === "Engineering" ? "engineer" : ""} ${person.department === "Business" ? "business" : ""}${person.department === "Design" ? "design" : ""}`}>call</span><span className="contact-text">{person.contact.phone}</span>
    </a>{" "}
    <a href={`mailto: ${person.contact.email}`}>
    <span className={`material-icons email ${person.department === "Engineering" ? "engineer" : ""} ${person.department === "Business" ? "business" : ""}${person.department === "Design" ? "design" : ""}`}>email</span><span className="contact-text">{person.contact.email}</span>
    </a>{" "}
    <a href={person.contact.url}>
    <span className={`material-icons computer ${person.department === "Engineering" ? "engineer" : ""} ${person.department === "Business" ? "business" : ""}${person.department === "Design" ? "design" : ""}`}>computer</span><span className="contact-text">{person.contact.url}</span>
    </a>{" "}
    </div>
    <div className="modal-previous-btn">
    <button
    className="previous-button"
    onClick={this.previousPerson}
    disabled={this.state.index <= 0 ? true : false}
    >Previous
    </button>
    </div>
    <div className={`modal-next-btn ${person.department === "Engineering" ? "engineer" : ""} ${person.department === "Business" ? "business" : ""}${person.department === "Design" ? "design" : ""}`}>
    <button
    className="next-button"
    onClick={this.nextPerson}
    disabled={this.state.index >= 41 ? true : false}
    >Next
    </button>
    </div>
    </div>
    </Modals>
    </div>
    </div>
    );
    }
    }

    export default App;


    最佳答案

    要在单击外部时关闭模态框,您必须修改模态框组件。首先,您在开头创建一个引用:

    modalRef = React.createRef();

    然后在渲染函数中使用该引用:

    return (
    <div ref={this.modalRef}...

    您在模态组件的生命周期内监听 mousedown 事件:

    componentDidMount() {
    document.addEventListener("mousedown", this.handleMouseDown);
    }

    componentWillUnmount() {
    document.removeEventListener("mousedown", this.handleMouseDown);
    }

    然后您测试点击是否在处理程序中的引用之外:

    // Mouse click event, if outside of the popup then close it
    handleMouseDown= (event) => {
    if (!this.modalRef.current.contains(event.target)) {
    // Notify the parent with a callback to hide the modal
    };
    }

    关于javascript - 如何在 React 中关闭模态时添加过渡以及如何在模态外单击时关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61230257/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com