当前位置: 首页 > news >正文

react 中,使用antd layout布局中的sider 做sider的展开和收起功能

一 话不多说,先展示效果:

展开时:

收起时:

二、实现代码如下

react 文件

import React, {useState} from 'react';
import {Layout} from 'antd';
import styles from "./index.module.less"; // 这个是样式文件,实际上里面只有一个button的样式
import foldUrl from "../../../public/btn_fold2.png"; // 这里是按钮中的图片,可以替换成自己的

const {Sider, Content} = Layout;
const ScalingTest = () => {

    const [showSider, setShowSider] = useState(true);

    const containerLayoutStyle: React.CSSProperties = {
        borderRadius: "8px",
        overflow: "hidden",
        height: "600px"
    };
    const containerSiderStyle: React.CSSProperties = {
        background: "red",
        height: "600px",
        fontSize: "24px",
    };

    const contentStyle: React.CSSProperties = {
        backgroundColor: "gainsboro",
        position: "relative",
        height: "600px",
        fontSize: "24px",
    };

    return <div style={{padding: 50}}>
        <Layout style={containerLayoutStyle}>
            <Sider style={containerSiderStyle} width={showSider ? 300 : 0}>
                我是侧边栏
            </Sider>
            <Content style={contentStyle}>
                {/* 伸缩按钮 */}
                <button className={styles.closeButton} type={"button"}
                        onClick={() => setShowSider(!showSider)}>
                    <img src={foldUrl} width={4} height={8} alt={"img"}
                         style={{marginLeft: -2, transform: `rotate(${showSider ? 0 : 180}deg)`}}/>
                </button>
                <div>
                    我是内容噢
                </div>

            </Content>
        </Layout>
    </div>

}

export default ScalingTest;

CSS 样式文件:

// 展开、关闭按钮的样式

.closeButton {
  position: absolute;
  top: 50%;
  transform: translate(0, -50%);
  left: 6px;
  width: 12px;
  height: 40px;
  background: linear-gradient(180deg, #EFEEF5 0%, #F0F1F5 98%);
  border-radius: 4px 4px 4px 4px;
  border: 1px solid rgba(255, 255, 255, 0.5);
}

三、题外话

        说个题外话,我自己在使用ant desgin pro +umi 那套成熟的框架时,给layout组件设置自定义高度不生效。但是如果不使用ant desgin pro +umi 那套框架时,就可以随意设置。不知道为啥!!不知道有没有大神帮忙解惑.。。。。。。


文章转载自:

http://Rfw0sSdR.pqhfx.cn
http://FD2bvGrl.pqhfx.cn
http://9m46TxgS.pqhfx.cn
http://AevmhaCL.pqhfx.cn
http://VJ9ghGxR.pqhfx.cn
http://TBPVuHhj.pqhfx.cn
http://18Z4vCA4.pqhfx.cn
http://n1gs6EwN.pqhfx.cn
http://AHllhmVR.pqhfx.cn
http://HQf7K0Gw.pqhfx.cn
http://AP7H79UA.pqhfx.cn
http://G5qsJfqJ.pqhfx.cn
http://ZnQwlMCI.pqhfx.cn
http://9QOKfN1e.pqhfx.cn
http://C2MvOZa3.pqhfx.cn
http://btWEvBTL.pqhfx.cn
http://x52QSlbj.pqhfx.cn
http://xjszmmtB.pqhfx.cn
http://901I1tNz.pqhfx.cn
http://WYAgLtUL.pqhfx.cn
http://hT8kUS7E.pqhfx.cn
http://PaGylQxP.pqhfx.cn
http://aB3ZXKPD.pqhfx.cn
http://idCK2ilR.pqhfx.cn
http://oOZVZ20E.pqhfx.cn
http://1Cbj2Db1.pqhfx.cn
http://5so7Edep.pqhfx.cn
http://oCwTgbik.pqhfx.cn
http://U5tXzG6Y.pqhfx.cn
http://b2BEyY6d.pqhfx.cn
http://www.dtcms.com/a/45681.html

相关文章:

  • 对于运维稳定性建设的一些思考
  • Let‘s Encrypt 获取免费SSL证书
  • 如何在 IntelliJ IDEA 中集成 DeepSeek
  • DeepSeek实操教程(清华、北大)
  • MATLAB环境下从信号中去除60Hz工频干扰噪声
  • git push失败
  • 微服务笔记 2025/2/15
  • 计算机网络实验2-虚拟局域网(VLAN)划分
  • ES如何打印DSL
  • 一次GaussDB内存不足导致宕机的问题排查
  • SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)
  • 1.C语言初识
  • JavaScript基础 -- 函数
  • 第三十三:6.3. 【mitt】 任意组件通讯
  • 第二章、python常用数据结构(2.2.1-2.3.2)------容器类型与容器类型的关系、数据类型与数据结构的关系
  • (动态规划 最长递增的子序列)leetcode 300
  • vue3+TS使用i18n.global.locale,页面要刷新时才更新
  • SQL Server Management Studio的使用
  • 运维Splunk面试题及参考答案
  • (上)基于机器学习的图像识别——遥感图像分类(LeNet-5;AlexNet;VGGNet;GoogLeNet;ResNet)
  • 局域网自动识别机器名和MAC并生成文件的命令
  • 自然语言处理NLP入门 -- 第九节NLP 实战项目 1:情感分析系统
  • 学习dify第二天-web前篇
  • 【C++】防止机械/移动硬盘休眠 - NoSleepHD
  • start DL from stratch (2)!!!
  • 【AI+智造】南京江北新区制造业特点分析及智慧设备运维诊断开发方案
  • RocketMQ的运行架构
  • [特殊字符]【CVPR2024新突破】Logit标准化:知识蒸馏中的自适应温度革命[特殊字符]
  • 中科大 计算机网络原理 第一章 1.6分组延迟、丢失和吞吐量 笔记
  • 如何把网络ip改为动态:全面指南