【问题标题】:How to resize width of Ant Design modal based on different screen sizes?如何根据不同的屏幕尺寸调整 Ant Design modal 的宽度?
【发布时间】:2022-01-09 23:49:42
【问题描述】:

我只希望模态框的最大宽度为 1200 像素,如果屏幕宽度小于 1200 像素,则为 100% 宽度。我试过使用style={{maxWidth: "1200px"}} 但这不起作用,并且在 Ant Design 文档中没有解释如何做到这一点。任何帮助表示赞赏!

import React, { useState } from 'react';
import { Modal, Button } from 'antd';

const App = () => {
  const [isModalVisible, setIsModalVisible] = useState(false);

  const showModal = () => {
    setIsModalVisible(true);
  };

  const handleOk = () => {
    setIsModalVisible(false);
  };

  const handleCancel = () => {
    setIsModalVisible(false);
  };

  return (
    <>
      <Button type="primary" onClick={showModal}>
        Open Modal
      </Button>
      <Modal 
      style={{maxWidth: "1200px"}} 
      title="Basic Modal" 
      visible={isModalVisible} 
      onOk={handleOk} 
      onCancel={handleCancel}
      >
        <p>Some contents...</p>
        <p>Some contents...</p>
        <p>Some contents...</p>
      </Modal>
    </>
  );
};

ReactDOM.render(<App />, mountNode);

【问题讨论】:

    标签: reactjs jsx antd


    【解决方案1】:

    添加此代码并使用 maxModalSize 值

    const [maxModalSize, setMaxModalSize] = useState(1200);
    const resize = () => {
        const maxSize = Math.min(1200, window.innerWidth);
        setMaxModalSize(maxSize);
    };
    
    useEffect(() => {
        window.addEventlistener("resize", resize);
        return () => window.removeEventlistener("resize", resize);
    });
    

    【讨论】:

    猜你喜欢
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    相关资源
    最近更新 更多