IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> React使用“re-resizable”实现dom的拉伸缩放效果 -> 正文阅读

[JavaScript知识库]React使用“re-resizable”实现dom的拉伸缩放效果

目录

一、需求

二、插件的安装使用

三、事件

四、案例

五、项目地址?


一、需求

? ? ? ? 在工作中遇到一个需求,做一个类似于调试工具栏的效果,点击某个按钮呼出,并且组件可以很便捷的实现可拖拽改变组件大小。

二、插件的安装使用

插件安装:

npm install --save re-resizable

插件引入:

import { Resizable } from "re-resizable";

基本使用:

import React, {Component} from 'react';
import { Resizable } from "re-resizable";

export default class demo extends Component {
    render() {
        return (
            <Resizable
                defaultSize={{width:400, height:400}}
            >
                你好,大聪明!
            </Resizable>
        );
    }
}

常用属性:

属性说明数据类型
defaultSize初始化默认宽高string?/?number
minWidth / maxWidth最小/最大宽string?/?number
minHeight / maxHeight最小/最大高string?/?number

size

设置宽高的数值object

...

......

更多使用方法详见官网。

https://github.com/bokuweb/re-resizableicon-default.png?t=M85Bhttps://github.com/bokuweb/re-resizable

三、事件

  • onResizeStart?调整组件开始时调用。

  • onResize?调整组件进行时调用。

  • onResizeStop?调整组件完成时调用。

代码:

import React, {Component} from 'react';
import { Resizable } from "re-resizable";

export default class Demo extends Component {

    onResizeStart = (e) => {
        console.log("onResizeStart执行");
        console.log(e);
    };
    onResize = (e) => {
        console.log("onResize执行");
        console.log(e);
    };
    onResizeStop = (e) => {
        console.log("onResizeStop执行");
        console.log(e);
    };

    render() {
        return (
            <Resizable
                style={{background: "red"}}
                defaultSize={{width:400, height:400}}
                onResize={(e) => this.onResize(e)}
                onResizeStart={(e) => this.onResizeStart(e)}
                onResizeStop={(e) => this.onResizeStop(e)}
            >
                你好,大聪明!
            </Resizable>
        );
    }
}

四、案例

这是需求完成以后,页面的整体代码。

import { Button, Icon, Tooltip } from 'antd';
import { connect } from 'dva';
import { Link, routerRedux } from 'dva/router';
import { formatMessage, FormattedMessage } from 'umi-plugin-locale';
import React, { Component } from 'react';
import { Resizable } from "re-resizable";
import WebConsole from './WebConsole';
import globalUtil from '../../utils/global';
import styles from './index.less'


@connect(null, null, null, { withRef: true })
class Shell extends Component {
    constructor(props) {
        super(props)
        this.state = {
            bool: false,
            height: 400
        };
    }
    onResizeStart = (e) => {
    };
    onResize = (e) => {
    };
    // 收起隐藏
    packUpOrDown = (e) => {
        const { bool } = this.state
        if (bool) {
            this.setState({
                height: 450
            })
        } else {
            this.setState({
                height: 40
            })
        }
        this.setState({
            bool: !this.state.bool,
        })
    }
    // 退出shell终端
    terminalRepeal = () => {
        const { dispatch } = this.props
        dispatch({
            type: 'region/terminalRepeal',
            payload: true,
        });
    }
    // 新开页
    newPage = () =>{
       const { dispatch } = this.props
        dispatch({
            type: 'region/terminalRepeal',
            payload: true,
        });
    }
    render() {
        const { bool, height } = this.state
        const eid = globalUtil.getCurrEnterpriseId();
        return (
            <Resizable
                ref="big"
                style={{ position: 'absolute', bottom: 0, zIndex: 9999 }}
                defaultSize={{ height: 400 }}
                size={{ height: height }}
                onResize={(e) => this.onResize(e)}
                onResizeStart={(e) => this.onResizeStart(e)}
                onResizeStop={(e, direction, ref, d) => {
                    this.setState({
                        height: this.state.height + d.height,
                    });
                }}
                minWidth={'100%'}
                minHeight={40}
                maxHeight={'60vh'}
            >
                <div className={styles.shell_head}>
                    <Tooltip placement="top" title={bool ? formatMessage({ id: 'otherEnterprise.shell.show' }) : formatMessage({ id: 'otherEnterprise.shell.Pack_up' })} >
                        <Button type="primary" icon={bool ? "vertical-align-top" : "vertical-align-bottom"} onClick={this.packUpOrDown} style={{ margin: '0 20px 0 0' }} />
                    </Tooltip>
                    <Link
                        to={`/enterprise/${eid}/shell`}
                        target="_blank"
                    >
                        <Tooltip placement="top" title={formatMessage({ id: 'otherEnterprise.shell.new' })} >
                            <Button type="primary" style={{ margin: '0 20px 0 0' }} icon="arrows-alt" onClick={this.newPage}>
                            </Button>
                        </Tooltip>
                    </Link>

                    <Tooltip placement="top" title={formatMessage({ id: 'otherEnterprise.shell.down' })}>
                        <Button type="primary" icon={"close"} onClick={this.terminalRepeal} style={{ margin: '0 20px 0 0' }} />
                    </Tooltip>
                </div>
                <div style={{ padding: "24px 24px 0 24px", height: "100%" }}>
                    <WebConsole height={height} />
                </div>
            </Resizable>

        );
    }
}

export default Shell;

效果:

?

五、项目地址?

主项目地址:

GitHub - goodrain/rainbond: Cloud native multi cloud application management platform | 云原生多云应用管理平台

前端项目地址:

GitHub - goodrain/rainbond-ui: Rainbond front-end project

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-11-05 00:19:49  更:2022-11-05 00:24:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 18:08:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码