iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >react-beautiful-dnd如何实现组件拖拽
  • 783
分享到

react-beautiful-dnd如何实现组件拖拽

2023-06-20 20:06:56 783人浏览 泡泡鱼
摘要

这篇文章将为大家详细讲解有关React-beautiful-dnd如何实现组件拖拽,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.安装在已有react项目中 执行以下命令 so easy。# 

这篇文章将为大家详细讲解有关React-beautiful-dnd如何实现组件拖拽,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1.安装

在已有react项目中 执行以下命令 so easy。

# yarnyarn add react-beautiful-dnd # npmnpm install react-beautiful-dnd --save

2.api

详情查看 官方文档。

3.react-beautiful-dnd demo

1 demo1 纵向组件拖拽

效果下图:

react-beautiful-dnd如何实现组件拖拽

demo1.gif

实现代码:

import React, { Component } from "react";import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd"; //初始化数据const getItems = count =>  Array.from({ length: count }, (v, k) => k).map(k => ({    id: `item-${k + 1}`,    content: `this is content ${k + 1}`  })); // 重新记录数组顺序const reorder = (list, startIndex, endIndex) => {  const result = Array.from(list);   const [removed] = result.splice(startIndex, 1);   result.splice(endIndex, 0, removed);  return result;}; const grid = 8; // 设置样式const getItemStyle = (isDragging, draggableStyle) => ({  // some basic styles to make the items look a bit nicer  userSelect: "none",  padding: grid * 2,  margin: `0 0 ${grid}px 0`,   // 拖拽的时候背景变化  background: isDragging ? "lightgreen" : "#ffffff",   // styles we need to apply on draggables  ...draggableStyle}); const getListStyle = () => ({  background: 'black',  padding: grid,  width: 250});   export default class ReactBeautifulDnd extends Component {  constructor(props) {    super(props);    this.state = {      items: getItems(11)    };    this.onDragEnd = this.onDragEnd.bind(this);  }   onDragEnd(result) {    if (!result.destination) {      return;    }     const items = reorder(      this.state.items,      result.source.index,      result.destination.index    );     this.setState({      items    });  }    render() {    return (      <DragDropContext onDragEnd={this.onDragEnd}>        <center>          <Droppable droppableId="droppable">            {(provided, snapshot) => (              <div              //provided.droppableProps应用的相同元素.                {...provided.droppableProps}                // 为了使 droppable 能够正常工作必须 绑定到最高可能的DOM节点中provided.innerRef.                ref={provided.innerRef}                style={getListStyle(snapshot)}              >                {this.state.items.map((item, index) => (                  <Draggable key={item.id} draggableId={item.id} index={index}>                    {(provided, snapshot) => (                      <div                        ref={provided.innerRef}                        {...provided.draggableProps}                        {...provided.dragHandleProps}                        style={getItemStyle(                          snapshot.isDragging,                          provided.draggableProps.style                        )}                      >                        {item.content}                      </div>                    )}                  </Draggable>                ))}                {provided.placeholder}              </div>            )}          </Droppable>        </center>      </DragDropContext>    );  }}

2 demo2 水平拖拽

效果下图:

react-beautiful-dnd如何实现组件拖拽

demo2.gif

实现代码: 其实和纵向拖拽差不多 Droppable 中 多添加了一个排列顺序的属性,direction="horizontal"

import React, { Component } from "react";import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";  const getItems = count => (  Array.from({ length: count }, (v, k) => k).map(k => ({    id: `item-${k + 1}`,    content: `this is content ${k + 1}`  }))) // 重新记录数组顺序const reorder = (list, startIndex, endIndex) => {  const result = Array.from(list);  //删除并记录 删除元素  const [removed] = result.splice(startIndex, 1);  //将原来的元素添加进数组  result.splice(endIndex, 0, removed);  return result;}; const grid = 8;  // 设置样式const getItemStyle = (isDragging, draggableStyle) => ({  // some basic styles to make the items look a bit nicer  userSelect: "none",  padding: grid * 2,  margin: `0 ${grid}px 0 0 `,   // 拖拽的时候背景变化  background: isDragging ? "lightgreen" : "#ffffff",   // styles we need to apply on draggables  ...draggableStyle}); const getListStyle = () => ({  background: 'black',  display: 'flex',  padding: grid,  overflow: 'auto',});  class ReactBeautifulDndHorizontal extends Component {  constructor(props) {    super(props);    this.state = {      items: getItems(10)    };    this.onDragEnd = this.onDragEnd.bind(this);  }   onDragEnd(result) {    if (!result.destination) {      return;    }     const items = reorder(      this.state.items,      result.source.index,      result.destination.index    );     this.setState({      items    });  }   render() {    return (      <DragDropContext onDragEnd={this.onDragEnd}>        <Droppable droppableId="droppable" direction="horizontal">          {(provided, snapshot) => (            <div              {...provided.droppableProps}              ref={provided.innerRef}              style={getListStyle(snapshot.isDragginGover)}            >              {this.state.items.map((item, index) => (                <Draggable key={item.id} draggableId={item.id} index={index}>                  {(provided, snapshot) => (                    <div                      ref={provided.innerRef}                      {...provided.draggableProps}                      {...provided.dragHandleProps}                      style={getItemStyle(                        snapshot.isDragging,                        provided.draggableProps.style                      )}                    >                      {item.content}                    </div>                  )}                </Draggable>              ))}              {provided.placeholder}            </div>          )}        </Droppable>      </DragDropContext>    )  }} export default ReactBeautifulDndHorizontal

3 demo3实现一个代办事项的拖拽(纵向 横向拖拽)

react-beautiful-dnd如何实现组件拖拽

关于“react-beautiful-dnd如何实现组件拖拽”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: react-beautiful-dnd如何实现组件拖拽

本文链接: https://www.lsjlt.com/news/300051.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • react-beautiful-dnd如何实现组件拖拽
    这篇文章将为大家详细讲解有关react-beautiful-dnd如何实现组件拖拽,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.安装在已有react项目中 执行以下命令 so easy。# ...
    99+
    2023-06-20
  • react-beautiful-dnd 实现组件拖拽功能
    目录1.安装2.APi3.react-beautiful-dnd demo3.1 demo1 纵向组件拖拽3.2 demo2 水平拖拽3.3 demo3实现一个代办事项的拖拽(纵向 ...
    99+
    2024-04-02
  • react-dnd如何实现拖拽
    这篇文章主要介绍了react-dnd如何实现拖拽的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇react-dnd如何实现拖拽文章都会有所收获,下面我们一起来看看吧。 ...
    99+
    2024-04-02
  • 使用react-beautiful-dnd实现列表间拖拽踩坑
    目录为什么选用react-beautiful-dnd 基本使用方法 基本概念使用方法使用过程中遇到的问题 总结 参考资料为什么选用react-beautiful-dnd 相比于re...
    99+
    2024-04-02
  • react中实现拖拽排序react-dnd功能
    dnd文档 html 拖拽排序 import React, { useState, useRef } from 'react'; import { cloneDeep } from...
    99+
    2023-02-06
    拖拽排序react-dnd react拖拽排序
  • react拖拽组件react-sortable-hoc如何使用
    这篇“react拖拽组件react-sortable-hoc如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“react...
    99+
    2023-07-05
  • react项目中使用react-dnd实现列表的拖拽排序功能
    目录1.先安装依赖2.创建一个 index.js 文件3.新建example.js文件4.新建TopicLis.js文件5.新建 ItemTypes.js现在有一个新需求就是需要对一...
    99+
    2023-02-06
    react-dnd列表的拖拽排序 react-dnd拖拽排序 react拖拽排序
  • react+react-beautiful-dnd实现代办事项思路详解
    目录react+react-beautiful-dnd应用效果预览实现思路index.js入口文件配置app.jsx主页面配置untils/with-context.js封装工具to...
    99+
    2024-04-02
  • Vue 可拖拽组件Vue Smooth DnD的使用详解
    目录简介和 Demo 展示API: Container属性生命周期回调事件API: Draggable实战简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu...
    99+
    2024-04-02
  • react拖拽组件react-sortable-hoc的使用
    目录1.文件12.文件23.使用使用react-sortable-hoc实现拖拽 如图: 提示:下面案例可供参考 1.文件1 代码如下(示例):文件名称:./dragcompone...
    99+
    2023-02-24
    react拖拽组件 react sortable-hoc
  • Vue可拖拽组件Vue Smooth DnD的使用方法
    本篇内容主要讲解“Vue可拖拽组件Vue Smooth DnD的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue可拖拽组件Vue Smooth DnD的使用方法”吧!目录简介和 De...
    99+
    2023-06-20
  • 怎么实现react拖拽hooks
    这篇文章主要介绍了怎么实现react拖拽hooks,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。前言源码总共也就一百多行,看完这个大致可以理解一些成熟的react拖拽库的实现...
    99+
    2023-06-14
  • React拖拽调整大小的组件
    本文实例为大家分享了React拖拽调整大小的组件,供大家参考,具体内容如下 一、实现流程 1.使用React.cloneElement加强包裹组件,在包裹的组件设置绝对定位,并在组件...
    99+
    2024-04-02
  • react实现拖拽模态框
    前言 实际开发中,模态框展现数据会经常出现.但不幸的是有时功能开发完了,UI同学突然提出需求希望模态框能拖拽.本文使用的模态框由 ant design 3.0 的 Modal 组件封...
    99+
    2024-04-02
  • 怎么在Html5中实现一个react拖拽排序组件
    今天就跟大家聊聊有关怎么在Html5中实现一个react拖拽排序组件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。第一步是先了解H5拖放的相关属性,MDN上有详细的说明,链接为htt...
    99+
    2023-06-09
  • React如何使用sortablejs实现拖拽排序
    目录React使用sortablejs实现拖拽排序sortablejs之强大的拖拽库安装基本示例常用配置总结React使用sortablejs实现拖拽排序 1、使用npm装包 $ n...
    99+
    2023-01-16
    React使用sortablejs sortablejs实现拖拽排序 sortablejs拖拽排序
  • react实现自定义拖拽hook
    前沿 最近发现公司的产品好几个模块用到了拖拽功能,之前拖拽组件是通过Html5 drag Api 实现的但体验并不是很好,顺便将原来的拖拽组建稍做修改,写一个自定义hook,方便大家...
    99+
    2024-04-02
  • VUE使用draggable实现组件拖拽
    本文实例为大家分享了draggable组件拖拽实例,供大家参考,具体内容如下 实现步骤 1、导入draggable依赖 npm i -S vuedraggable 2、引入dragg...
    99+
    2024-04-02
  • 拖拽插件sortable.js如何实现el-table表格拖拽效果
    这篇文章将为大家详细讲解有关拖拽插件sortable.js如何实现el-table表格拖拽效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。问题描述Sortable.js是一款优秀的js拖拽库,因为是原生...
    99+
    2023-06-29
  • vue拖拽组件vuedraggable API options如何实现盒子之间相互拖拽排序
    小编给大家分享一下vue拖拽组件vuedraggable API options如何实现盒子之间相互拖拽排序,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作