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

1、所实现拖拽效果

1、第一种实现效果

react-darggable实现左右拖拽

2、第二种实现效果

react-darggable实现左右拖拽2

2、第一种实现方法

1、tsx代码

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、代码

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;

相关推荐

  1. React实践

    2023-12-10 08:18:04       72 阅读
  2. React实践

    2023-12-10 08:18:04       66 阅读
  3. React实践

    2023-12-10 08:18:04       52 阅读
  4. React 元素教程 react-dnd 实现

    2023-12-10 08:18:04       62 阅读
  5. react实现的插件

    2023-12-10 08:18:04       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-10 08:18:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 08:18:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 08:18:04       82 阅读
  4. Python语言-面向对象

    2023-12-10 08:18:04       91 阅读

热门阅读

  1. ESP32网络编程-OTA方式升级固件(基于Arduino IDE)

    2023-12-10 08:18:04       55 阅读
  2. SQL命令---修改数据库的编码

    2023-12-10 08:18:04       55 阅读
  3. Oracle 怎樣修改DB_NAME

    2023-12-10 08:18:04       51 阅读
  4. BeyondCompare-过期-mac电脑

    2023-12-10 08:18:04       55 阅读
  5. AI 绘画 | Stable Diffusion 黑白老照片上色修复

    2023-12-10 08:18:04       126 阅读
  6. E : B DS二分查找_搜索二维矩阵

    2023-12-10 08:18:04       57 阅读
  7. String 是最基本的数据类型吗?

    2023-12-10 08:18:04       54 阅读
  8. ZooKeeper分布式应用协调服务

    2023-12-10 08:18:04       37 阅读