css 我怎样才能使react组件从dom中完全消失,从而保留动画?

mmvthczy  于 2023-08-08  发布在  React
关注(0)|答案(2)|浏览(171)

App.jsx

  1. import React, { useState } from "react";
  2. import Modal from "./components/Modal/Modal";
  3. export default function App() {
  4. const [showModal, setShowModal] = useState(false);
  5. return (
  6. <div className="app">
  7. <p className="ppp">Sample text</p>
  8. <div className="modal_menu_wrap">
  9. <div className="modal_menu">
  10. <button
  11. onClick={() => {
  12. setShowModal(true);
  13. }}
  14. className="open-btn"
  15. >
  16. Open/Close
  17. </button>
  18. <Modal active={showModal} setShowModal={setShowModal} />
  19. </div>
  20. </div>
  21. </div>
  22. );
  23. }

字符串
index.css

  1. .open-btn {
  2. padding: 10px 15px;
  3. border: none;
  4. background-color: black;
  5. color: aliceblue;
  6. cursor: pointer;
  7. }
  8. .open-btn:hover {
  9. scale: 1.05;
  10. }
  11. .modal_menu_wrap {
  12. display: flex;
  13. justify-content: center;
  14. border: 1px solid rgb(255, 0, 0) !important;
  15. }
  16. .modal_menu {
  17. width: 100%;
  18. max-width: 200px;
  19. border: 1px solid rgb(255, 0, 0) !important;
  20. }


Modal.jsx

  1. import styles from "./modal.module.css";
  2. import React, { useEffect, useState } from "react";
  3. export default function Modal({ active, setShowModal }) {
  4. const [showContent, setShowContent] = useState(false);
  5. const closeModal = () => {
  6. setShowContent(false);
  7. setTimeout(() => {
  8. setShowModal(false);
  9. }, 500);
  10. };
  11. useEffect(() => {
  12. if (active) {
  13. setShowContent(true);
  14. }
  15. }, [active]);
  16. if (!active) return null;
  17. return (
  18. <div
  19. className={
  20. showContent ? `${styles.modal} ${styles.showModal}` : `${styles.modal}`
  21. }
  22. >
  23. <p className="ppp"></p>
  24. <div onClick={closeModal} className={styles.closeBtn}>
  25. <span>&times;</span>
  26. </div>
  27. </div>
  28. );
  29. }


modal.module.css

  1. .modal {
  2. position: absolute;
  3. width: 100%;
  4. max-width: 200px;
  5. height: 300px;
  6. background-color: azure;
  7. border-radius: 10px;
  8. z-index: 2;
  9. opacity: 0.5;
  10. border: 1px solid rgb(255, 0, 0) !important;
  11. transform: translateY(-100vh);
  12. transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  13. }
  14. .showModal {
  15. opacity: 1;
  16. transform: translateY(0);
  17. z-index: 2;
  18. }
  19. .closeBtn {
  20. position: absolute;
  21. top: 10px;
  22. right: 20px;
  23. cursor: pointer;
  24. z-index: 9;
  25. border: 1px solid rgb(255, 0, 0) !important;
  26. }
  27. .closeBtn span {
  28. display: block;
  29. font-size: 2rem;
  30. font-weight: bold;
  31. transition: transform 0.2s ease;
  32. }
  33. .closeBtn span:hover {
  34. transform: scale(1.2);
  35. }


当您单击“打开/关闭”按钮以及在元素字段外单击时,元素必须出现和消失。我为按钮指定了一个值onClick={() => {setShowModal( !showModal);}},但在本例中,动画消失了。元素必须从dom中完全消失。不应有带十字的按钮。演示-https://codesandbox.io/s/serverless-dust-8vj86d?file=/src/index.css

lbsnaicq

lbsnaicq1#

简单编辑2个文件

第一个

使打开/关闭按钮通过将新值设置为旧值的倒数来切换状态!showModal在App.js

  1. <button
  2. onClick={() => {
  3. setShowModal(!showModal);
  4. }}
  5. className="open-btn"
  6. >
  7. Open/Close
  8. </button>

文件中
然后
我删除了if (!active) return null;这一行,这是一个糟糕的使用react的方法!无意冒犯,但这可能会影响应用程序的性能。
并在此之后添加了一个检查,如果它是活动的,则会删除该类
active && showContent,这意味着如果active为== true,则执行文件Modal.jsx中运算符右侧的代码

  1. <div
  2. className={
  3. active && showContent
  4. ? `${styles.modal} ${styles.showModal}`
  5. : `${styles.modal}`
  6. }
  7. >


最后的代码是:

App.jsx

  1. import React, { useState } from "react";
  2. import Modal from "./components/Modal/Modal";
  3. export default function App() {
  4. const [showModal, setShowModal] = useState(false);
  5. return (
  6. <div className="app">
  7. <p className="ppp">Sample text</p>
  8. <div className="modal_menu_wrap">
  9. <div className="modal_menu">
  10. <button
  11. onClick={() => {
  12. setShowModal(!showModal);
  13. }}
  14. className="open-btn"
  15. >
  16. Open/Close
  17. </button>
  18. <Modal active={showModal} setShowModal={setShowModal} />
  19. </div>
  20. </div>
  21. </div>
  22. );
  23. }

Modal.jsx

  1. import styles from "./modal.module.css";
  2. import React, { useEffect, useState } from "react";
  3. export default function Modal({ active, setShowModal }) {
  4. const [showContent, setShowContent] = useState(false);
  5. const closeModal = () => {
  6. setShowContent(false);
  7. setTimeout(() => {
  8. setShowModal(false);
  9. }, 500);
  10. };
  11. useEffect(() => {
  12. if (active) {
  13. setShowContent(true);
  14. }
  15. }, [active]);
  16. return (
  17. <div
  18. className={
  19. active && showContent
  20. ? `${styles.modal} ${styles.showModal}`
  21. : `${styles.modal}`
  22. }
  23. >
  24. <p className="ppp"></p>
  25. <div onClick={closeModal} className={styles.closeBtn}>
  26. <span>&times;</span>
  27. </div>
  28. </div>
  29. );
  30. }

展开查看全部
ipakzgxi

ipakzgxi2#

更好的方法是将动画添加到类中。
然后将组件的样式设置为动画最后一帧中的状态。同样简单的是,当您将className添加到组件时,动画发生并通过在动画结束时删除className返回到最后一个状态。所以你可以编辑

  1. <button
  2. onClick={() => {
  3. {/* add the className for the open stat of the componant here */}
  4. setShowModal(true);
  5. }}
  6. className="open-btn"
  7. >
  8. Open/Close
  9. </button>

字符串

相关问题