惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Vercel News
Vercel News
F
Fortinet All Blogs
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
U
Unit 42
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
S
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
Attack and Defense Labs
Attack and Defense Labs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hacker News: Ask HN
Hacker News: Ask HN
Jina AI
Jina AI
Schneier on Security
Schneier on Security
W
WeLiveSecurity
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
博客园 - 聂微东

博客园 - ฅ˙-˙ฅ

用display实现效果:上面根据内容自适应高度;下面撑满所有,并且超出时候显示滚动条 react更改多层对象变量的方法 react umi model使用注意事项 博文阅读密码验证 - 博客园 antd input控制只能输入数字并进行格式化显示(antd 3版本) react函数式组件:父组件调用子组件方法 react form表单中自定义组件的数据双向绑定实现 vue3+vant h5: Rem 移动端布局适配之postcss-pxtorem和lib-flexible 项目记录文档 前端mock方案:使用json-server mac安装使用nginx 博文阅读密码验证 - 博客园 umi修改antd主题颜色(通过less文件修改) 部署一个vue项目到阿里云服务器 vue项目通过点击按钮实现复制图片(非图片url和base64),可发送到聊天框 vue重新刷新当前路由(非浏览器强刷,不会出现闪屏) 使用Vue.extend实现iview Upload在单文件上传时,拖拽多个文件给出错误提示 uni-app怎么使用路由守卫,并且路由配置和pages.json中只写一套 uni-app小程序iPhone X适配底部栏黑横线 使用css自定义变量实现实现主题切换功能
拖动改变顺序
ฅ˙-˙ฅ · 2022-10-25 · via 博客园 - ฅ˙-˙ฅ

效果: 将被拖动的元素放在拖动结束时的元素后面

实现

jsx

import React, { useState } from 'react';
import styles from './index.less';

let dragStartIndex = 0; // 记录被拖动的元素index
let dragEndIndex = 0; // 鼠标移动时,被悬浮的元素index

export default () => {
  const [list, setLIst] = useState([]);
  const [highLightIndex, setHighLightIndex] = useState(undefined); // 鼠标移动时被悬浮的元素(用来高亮定位)

  // 拖动开始: 记录被拖动元素
  const onDragStart = (index) => {
    dragStartIndex = index;
  };

  // 拖动进入事件: 记录被悬浮的元素
  const onDragEnter = (index) => {
    dragEndIndex = index;
    setHighLightIndex(index);
  };

  // 拖动结束:改变数组,去掉dragStartIndex,插入dragEndIndex后面位置;
  const onDragEnd = () => {
    if (dragEndIndex === dragStartIndex) {
      setHighLightIndex(undefined);
      return;
    }

    const tabHeadersCopy = [...list];
    const dragingTab = tabHeadersCopy[dragStartIndex];

    // 为了实现拖动元素放到被浮动的元素后面,从前往后拖动和从后往前拖动,增删数组稍有差异
    if (dragEndIndex > dragStartIndex) {
      tabHeadersCopy.splice(dragStartIndex, 1);
      tabHeadersCopy.splice(dragEndIndex, 0, dragingTab);
    } else {
      tabHeadersCopy.splice(dragEndIndex + 1, 0, dragingTab);
      tabHeadersCopy.splice(dragStartIndex + 1, 1);
    }

    setLIst(tabHeadersCopy);
    setHighLightIndex(undefined);
  };

  return (
    <div className={styles['tabs-container']}>
      {list &&
        list.length > 0 &&
        list.map((tab, index) => (
          <div
            key={tab.id}
            draggable={true}
            onDragStart={() => onDragStart(index)}
            onDragEnd={onDragEnd}
            onDragEnter={() => onDragEnter(index)}
            style={{ borderColor: index === highLightIndex ? 'red' : '' }}
          >
            {tab.name}
          </div>
        ))}
    </div>
  );
};

参考实现: react中实现拖拽排序react-dnd