【react-draggable实际应用】实现左右拖拽

news/2024/7/15 18:25:12 标签: react.js, javascript, ecmascript

1、所实现拖拽效果

1、第一种实现效果

react-darggable实现左右拖拽

2、第二种实现效果

react-darggable实现左右拖拽2

2、第一种实现方法

1、tsx代码

javascript">import React, { Component, useState } from 'react';
import Draggable from 'react-draggable';
import classNames from 'classnames';

import styles from './index.less';

/**
 * @name vhToPx 以window.innerWidth为基准,传入vh值,返回px值
 * @param value 传入的vh数值 如50
 */
const vhToPx = (value: number) => {
  var vh = window.innerWidth / 100;
  return value * vh;
};

export const dtl = () => {
  // 各宽度的初始值
  const initialSize = {
    initialLeftBoxWidth: vhToPx(60), // 左边区块初始宽度
    leftBoxWidth: vhToPx(60), // 左边区块初始宽度
    leftBoxMinWidth: vhToPx(40), // 左边区块最小宽度
    leftBoxMaxWidth: vhToPx(80), // 左边区块最大宽度
    dragBoxBackground: 'blue', // 拖拽盒子的背景色
  };
  // 实际要使用sizeParam这个值,要保持初始值不变
  const [sizeParam, setSizeParam] = useState(initialSize);

  /**
   * @name changeSizeParam 改变sizeParam的值
   * @param params 传入的值
   */
  const changeSizeParam = (params: any) => {
    setSizeParam({...sizeParam, ...params});
  };

  /**
   * @name onDrag react-draggable拖拽事件
   * @param ev DraggableEvent
   * @param ui DraggableData
   */
  const onDrag = (ev: any, ui: any) => {
    const {initialLeftBoxWidth} = sizeParam;
    const newLeftBoxWidth = ui.x + initialLeftBoxWidth;
    changeSizeParam({
      leftBoxWidth: newLeftBoxWidth,
      dragBoxBackground: '#FFB6C1',
    });
  };

  /**
   * @name onDragStop react-draggable拖拽结束事件
   */
  const onDragStop = () => {
    changeSizeParam({
      dragBoxBackground: 'blue',
    });
  };

  return (
    <div className={classNames(styles['dtl-content'])}>
      <div
        className={'dtl-left'}
        style={{width: `${sizeParam.leftBoxWidth}px`}}
      >
        <h1>
          左左左左
        </h1>
      </div>
      <div
        className={'dtl-right'}
        style={{width: `calc(100% - ${sizeParam.leftBoxWidth}px)`}}
      >
        <Draggable
          axis={'x'}
          defaultPosition={{x: 0, y: 0}}
          bounds={{
            left: sizeParam.leftBoxMinWidth - sizeParam.initialLeftBoxWidth,
            right: sizeParam.leftBoxMaxWidth - sizeParam.initialLeftBoxWidth,
          }}
          onDrag={onDrag}
          onStop={onDragStop}
        >
          <div
            className={'dragBox'}
            style={{
              left: `${sizeParam.initialLeftBoxWidth - 5}px`,
              background: `${sizeParam.dragBoxBackground}`,
            }}
          />
        </Draggable>
        <h1> 右右</h1>
      </div>
    </div>
  );
};

export default dtl;

2、样式代码

.dtl-content {
  display: flex;
  justify-content: flex-start;
  flex-direction: row;
  flex-wrap: nowrap;

  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  background-color: #f5f5f7;

  :global {
    .dtl-left {
      position: relative;
      height: 100vh;
      padding: 20px;
      background-color: green;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      flex-grow: 1;
    }

    .dtl-right {
      height: 100vh;
      padding: 20px;
      background-color: red;
      flex-grow: 1;
      z-index: 100;
    }

    .dragBox {
      position: absolute;
      top: 0;
      width: 5px;
      height: 100vh;
      cursor: col-resize;
      z-index: 1000;
    }
  }
}

3、 第二种实现方法

1、代码

javascript">import React, { Component } from 'react';
import Draggable from 'react-draggable';
import styled from 'styled-components';

// 容器
const Container = styled.div`
  display: flex;
  justify-content: flex-start;
`;

// 左边内容部分
const LeftContent = styled.div`
  position: relative;
  width: ${(props) => props.width}px;
  height: 100vh;
  padding: 20px;
  background-color: #e6e6fa;
  overflow: hidden;
  flex-grow: 1;
`;

// 拖拽部分
const DraggableBox = styled.div`
  position: absolute;
  left: ${(props) => props.left}px;
  top: 0;
  width: 5px;
  height: 100vh;
  background-color: ${(props) => props.background};
  cursor: col-resize;
  z-index: 1000;
`;

// 右边内容部分
const RightContent = styled.div`
  width: calc(100% - ${(props) => props.leftBoxWidth}px);
  height: 100vh;
  padding: 20px;
  background-color: #fff;
  flex-grow: 1;
  z-index: 100;
`;

const Li = styled.li`
  white-space: nowrap;
`;

function vhToPx(value) {
  var vh = window.innerWidth / 100;
  return value * vh;
}

class DraggableExp extends Component {
  state = {
    initialLeftBoxWidth: vhToPx(80), // 左边区块初始宽度
    leftBoxWidth: vhToPx(80), // 左边区块初始宽度
    leftBoxMinWidth: vhToPx(40), // 左边区块最小宽度
    leftBoxMaxWidth: vhToPx(80), // 左边区块最大宽度
    dragBoxBackground: 'blue', // 拖拽盒子的背景色
  };

  // 拖动时设置拖动box背景色,同时更新左右box的宽度
  onDrag = (ev, ui) => {
    const { initialLeftBoxWidth } = this.state;
    const newLeftBoxWidth = ui.x + initialLeftBoxWidth;
    this.setState({
      leftBoxWidth: newLeftBoxWidth,
      dragBoxBackground: '#FFB6C1',
    });
  };

  // 拖拽结束,重置drag-box的背景色
  onDragStop = () => {
    this.setState({
      dragBoxBackground: 'blue',
    });
  };

  render() {
    const {
      initialLeftBoxWidth,
      leftBoxWidth,
      leftBoxMinWidth,
      leftBoxMaxWidth,
      dragBoxBackground,
    } = this.state;
    return (
      <Container>
        <LeftContent width={leftBoxWidth}>
          <h1>左边内容左边内容</h1>
        </LeftContent>
        <RightContent leftBoxWidth={leftBoxWidth}>
          <Draggable
            axis="x"
            defaultPosition={{ x: 0, y: 0 }}
            bounds={{
              left: leftBoxMinWidth - initialLeftBoxWidth,
              right: leftBoxMaxWidth - initialLeftBoxWidth,
            }}
            onDrag={this.onDrag}
            onStop={this.onDragStop}
          >
            <DraggableBox
              left={initialLeftBoxWidth - 5}
              background={dragBoxBackground}
            />
          </Draggable>
          <h1>右右右右右右右右</h1>
        </RightContent>
      </Container>
    );
  }
}

export default DraggableExp;


http://www.niftyadmin.cn/n/5250472.html

相关文章

k8s初始化报错 [ERROR CRI]: container runtime is not running: ......

天行健&#xff0c;君子以自强不息&#xff1b;地势坤&#xff0c;君子以厚德载物。 每个人都有惰性&#xff0c;但不断学习是好好生活的根本&#xff0c;共勉&#xff01; 文章均为学习整理笔记&#xff0c;分享记录为主&#xff0c;如有错误请指正&#xff0c;共同学习进步。…

2023年第十届GIAC全球互联网架构大会-核心PPT资料下载

一、峰会简介 谈到一个应用&#xff0c;我们首先考虑的是运行这个应用所需要的系统资源。其次&#xff0c;是关于应用自身的架构模式。最后&#xff0c;还需要从软件工程的不同角度来考虑应用的设计、开发、部署、运维等。架构设计对应用有着深远的影响&#xff0c;它的好坏决…

如何克服微服务测试的挑战,并最大化收益

多年来&#xff0c;微服务一直是行业趋势&#xff0c;但组织却未能从该方法中获益&#xff0c;并因发布失败而苦苦挣扎。这些失败通常归结为测试服务之间的接口以获得预期的质量、安全性和性能的困难。 最终&#xff0c;未能以足够稳健的方式测试这些 API。一线希望是遗留 SOA…

wsl2 ubuntu上搭建OpenIM

文档 踩坑 版本要选择好&#xff0c;不要直接main来跑&#xff0c;目前版本OpenIMServer 有release-v3.3和release-v3.4&#xff0c;对应Chat版本的release-v1.4和release-v1.4。但我跑3.4是有问题的&#xff0c;切到3.3可以跑通。export OPENIM_IP"http://ip:10002"…

vue中设置滚动条的样式

在vue项目中&#xff0c;想要设置如下图中所示滚动条的样式&#xff0c;可以采用如下方式&#xff1a; ​// 直接写在vue.app文件中 ::-webkit-scrollbar {width: 3px;height: 3px; } ::-webkit-scrollbar-thumb { //滑块部分// border-radius: 5px;background-color: #1890ff;…

半导体划片机助力氧化铝陶瓷片切割:科技与工艺的完美结合

在当今半导体制造领域&#xff0c;氧化铝陶瓷片作为一种高性能、高可靠性的材料&#xff0c;被广泛应用于各种电子设备中。而半导体划片机的出现&#xff0c;则为氧化铝陶瓷片的切割提供了新的解决方案&#xff0c;实现了科技与工艺的完美结合。 氧化铝陶瓷片是一种以氧化铝为基…

Python3+Requests+Excel完整接口自动化测试框架的实现

框架整体使用Python3RequestsExcel&#xff1a;包含对实时token的获取 1、------base -------runmethond.py runmethond&#xff1a;对不同的请求方式进行封装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 …

springboot基础(81):设置redis序列化器

前言 序列化器有多种&#xff0c;默认的序列是JdkSerializationRedisSerializer &#xff0c;通常性能较差&#xff0c;相对而言&#xff0c;我们会使用性能最好的Kryo序列化器来序列化。 redis如何配置Kryo序列化器 导入依赖 <dependency><groupId>org.project…