iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >js 实现拖拽排序详情
  • 607
分享到

js 实现拖拽排序详情

2024-04-02 19:04:59 607人浏览 薄情痞子
摘要

目录1、前言2、实现3、为何不使用html拖放api实现?4、总结1、前言 拖拽排序对于小伙伴们来说应该不陌生,平时工作的时候,可能会选择使用类似Sortable.js这样的开源库来

1、前言

拖拽排序对于小伙伴们来说应该不陌生,平时工作的时候,可能会选择使用类似Sortable.js这样的开源库来实现需求。但在完成需求后,大家有没有没想过拖拽排序是如何实现的呢?我花了点时间研究了一下,今天分享给大家。

2、实现


 {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.grid {
    display: flex;
    flex-wrap: wrap;
    margin: 0 -15px -15px 0;
    touch-action: none;
    user-select: none;
}

.grid-item {
    width: 90px;
    height: 90px;
    line-height: 88px;
    text-align: center;
    margin: 0 15px 15px 0;
    background: #FFF;
    border: 1px solid #d6d6d6;
    list-style: none;
}

.active {
    background: #c8ebfb;
}

.clone-grid-item {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1;
    width: 90px;
    height: 90px;
    line-height: 88px;
    text-align: center;
    background: #FFF;
    border: 1px solid #d6d6d6;
    opacity: 0.8;
    list-style: none;
}


<ul class="grid">
    <li class="grid-item">item1</li>
    <li class="grid-item">item2</li>
    <li class="grid-item">item3</li>
    <li class="grid-item">item4</li>
    <li class="grid-item">item5</li>
    <li class="grid-item">item6</li>
    <li class="grid-item">item7</li>
    <li class="grid-item">item8</li>
    <li class="grid-item">item9</li>
    <li class="grid-item">item10</li>
</ul>

采用ES6 Class写法:


class Draggable {
    constructor(options) {
        this.parent = options.element; // 父级元素
        this.cloneElementClassName = options.cloneElementClassName; // 克隆元素类名
        this.isPointerdown = false;
        this.diff = { x: 0, y: 0 }; // 相对于上一次移动差值
        this.drag = { element: null, index: 0, lastIndex: 0 }; // 拖拽元素
        this.drop = { element: null, index: 0, lastIndex: 0 }; // 释放元素
        this.clone = { element: null, x: 0, y: 0 };
        this.lastPointermove = { x: 0, y: 0 };
        this.rectList = []; // 用于保存拖拽项getBoundinGClientRect()方法获得的数据
        this.init();
    }
    init() {
        this.getRect();
        this.bindEventListener();
    }
    // 获取元素位置信息
    getRect() {
        this.rectList.length = 0;
        for (const item of this.parent.children) {
            this.rectList.push(item.getBoundingClientRect());
        }
    }
    handlePointerdown(e) {
        // 如果是鼠标点击,只响应左键
        if (e.pointerType === 'mouse' && e.button !== 0) {
            return;
        }
        if (e.target === this.parent) {
            return;
        }
        this.isPointerdown = true;
        this.parent.setPointerCapture(e.pointerId);
        this.lastPointermove.x = e.clientX;
        this.lastPointermove.y = e.clientY;
        this.drag.element = e.target;
        this.drag.element.classList.add('active');
        this.clone.element = this.drag.element.clonenode(true);
        this.clone.element.className = this.cloneElementClassName;
        this.clone.element.style.transition = 'none';
        const i = [].indexOf.call(this.parent.children, this.drag.element);
        this.clone.x = this.rectList[i].left;
        this.clone.y = this.rectList[i].top;
        this.drag.index = i;
        this.drag.lastIndex = i;
        this.clone.element.style.transfORM = 'translate3D(' + this.clone.x + 'px, ' + this.clone.y + 'px, 0)';
        document.body.appendChild(this.clone.element);
    }
    handlePointermove(e) {
        if (this.isPointerdown) {
            this.diff.x = e.clientX - this.lastPointermove.x;
            this.diff.y = e.clientY - this.lastPointermove.y;
            this.lastPointermove.x = e.clientX;
            this.lastPointermove.y = e.clientY;
            this.clone.x += this.diff.x;
            this.clone.y += this.diff.y;
            this.clone.element.style.transform = 'translate3d(' + this.clone.x + 'px, ' + this.clone.y + 'px, 0)';
            for (let i = 0; i < this.rectList.length; i++) {
                // 碰撞检测
                if (e.clientX > this.rectList[i].left && e.clientX < this.rectList[i].right &&
                    e.clientY > this.rectList[i].top && e.clientY < this.rectList[i].bottom) {
                    this.drop.element = this.parent.children[i];
                    this.drop.lastIndex = i;
                    if (this.drag.element !== this.drop.element) {
                        if (this.drag.index < i) {
                            this.parent.insertBefore(this.drag.element, this.drop.element.nextElementSibling);
                            this.drop.index = i - 1;
                        } else {
                            this.parent.insertBefore(this.drag.element, this.drop.element);
                            this.drop.index = i + 1;
                        }
                        this.drag.index = i;
                        const dragRect = this.rectList[this.drag.index];
                        const lastDragRect = this.rectList[this.drag.lastIndex];
                        const dropRect = this.rectList[this.drop.index];
                        const lastDropRect = this.rectList[this.drop.lastIndex];
                        this.drag.lastIndex = i;
                        this.drag.element.style.transition = 'none';
                        this.drop.element.style.transition = 'none';
                        this.drag.element.style.transform = 'translate3d(' + (lastDragRect.left - dragRect.left) + 'px, ' + (lastDragRect.top - dragRect.top) + 'px, 0)';
                        this.drop.element.style.transform = 'translate3d(' + (lastDropRect.left - dropRect.left) + 'px, ' + (lastDropRect.top - dropRect.top) + 'px, 0)';
                        this.drag.element.offsetLeft; // 触发重绘
                        this.drag.element.style.transition = 'transform 150ms';
                        this.drop.element.style.transition = 'transform 150ms';
                        this.drag.element.style.transform = 'translate3d(0px, 0px, 0px)';
                        this.drop.element.style.transform = 'translate3d(0px, 0px, 0px)';
                    }
                    break;
                }
            }
        }
    }
    handlePointerup(e) {
        if (this.isPointerdown) {
            this.isPointerdown = false;
            this.drag.element.classList.remove('active');
            this.clone.element.remove();
        }
    }
    handlePointercancel(e) {
        if (this.isPointerdown) {
            this.isPointerdown = false;
            this.drag.element.classList.remove('active');
            this.clone.element.remove();
        }
    }
    bindEventListener() {
        this.handlePointerdown = this.handlePointerdown.bind(this);
        this.handlePointermove = this.handlePointermove.bind(this);
        this.handlePointerup = this.handlePointerup.bind(this);
        this.handlePointercancel = this.handlePointercancel.bind(this);
        this.getRect = this.getRect.bind(this);
        this.parent.addEventListener('pointerdown', this.handlePointerdown);
        this.parent.addEventListener('pointermove', this.handlePointermove);
        this.parent.addEventListener('pointerup', this.handlePointerup);
        this.parent.addEventListener('pointercancel', this.handlePointercancel);
        window.addEventListener('scroll', this.getRect);
        window.addEventListener('resize', this.getRect);
        window.addEventListener('orientationchange', this.getRect);
    }
    unbindEventListener() {
        this.parent.removeEventListener('pointerdown', this.handlePointerdown);
        this.parent.removeEventListener('pointermove', this.handlePointermove);
        this.parent.removeEventListener('pointerup', this.handlePointerup);
        this.parent.removeEventListener('pointercancel', this.handlePointercancel);
        window.removeEventListener('scroll', this.getRect);
        window.removeEventListener('resize', this.getRect);
        window.removeEventListener('orientationchange', this.getRect);
    }
}
// 实例化
new Draggable({
    element: document.querySelector('.grid'),
    cloneElementClassName: 'clone-grid-item'
});

Demo:jsdemo.codeman.top/html/dragga…

3、为何不使用HTML拖放API实现?

因为原生HTML拖放API在移动端无法使用,所以为了兼容PC端和移动端,使用了PointerEvent事件实现拖拽逻辑。

4、总结

拖拽排序的基本功能已经实现,但还存在很多不足。像嵌套拖拽,跨列表拖拽,拖拽到底部自动滚动等功能都未实现。

到此这篇关于js 实现拖拽排序详情的文章就介绍到这了,更多相关js 实现拖拽排序内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: js 实现拖拽排序详情

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

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

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

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

下载Word文档
猜你喜欢
  • js 实现拖拽排序详情
    目录1、前言2、实现3、为何不使用HTML拖放API实现?4、总结1、前言 拖拽排序对于小伙伴们来说应该不陌生,平时工作的时候,可能会选择使用类似Sortable.js这样的开源库来...
    99+
    2022-11-12
  • JavaScript实现拖拽排序的方法详解
    目录实现原理概述代码实现完整代码实现可拖拽排序的菜单效果大家想必都很熟悉,本次我们通过一个可拖拽排序的九宫格案例来演示其实现原理。 先看一下完成效果: 实现原理概述 拖拽原理 当鼠...
    99+
    2022-11-13
  • 原生JS实现拖拽排序的示例代码
    目录HTML中的拖拽事件(drag & drop)Coding完整代码说到拖拽,应用场景不可谓不多。无论是打开电脑还是手机,第一眼望去的界面都是可拖拽的,靠拖拽实现...
    99+
    2022-12-08
    JS实现拖拽排序 JS拖拽排序 JS 排序
  • vue实现拖拽排序效果
    本文实例为大家分享了vue实现拖拽排序效果的具体代码,供大家参考,具体内容如下 效果预览 组件 drag.vue <template>   <Transition...
    99+
    2022-11-13
  • Javascript实现拖拽排序的代码
    运行环境:vue3.2以上,复制张贴运行即可看效果效果如下: <template> <div class="container"> <tr...
    99+
    2022-11-13
  • Vue实用功能之实现拖拽元素、列表拖拽排序
    目录Vue实现拖拽元素、列表拖拽排序组件使用补充:排序动画总结Vue实现拖拽元素、列表拖拽排序 需求:    1、左右两个容器,左边和右边的元素可以拖动...
    99+
    2022-11-13
    vue列表拖拽排序 vue实现拖拽功能 vue实现组件拖拽
  • vue中实现拖拽排序功能的详细教程
    目录原生拖拽 API 实现拖拽设置元素 dragable拖放事件拖拽排序拖拽API + 防抖实现vue awe-dnd 拖拽组件安装 awe-dnd 组件库在 main.js 中将 ...
    99+
    2022-11-13
  • vue实现移动端touch拖拽排序
    目录功能介绍:大致需求:整体思路:简单效果展示:具体实现:一、display:flex+v-for布局:二、touch事件绑定:三、卡片移动:四、获取手指所在位置:五、操作数组(删除...
    99+
    2022-09-27
  • jQuery怎么实现拖拽排序效果
    这篇文章给大家分享的是有关jQuery怎么实现拖拽排序效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。增强动态增加Div效果原来没有新建动作,分析代码后发现很容易增强~~<...
    99+
    2022-10-19
  • JavaScript怎么实现拖拽排序效果
    这篇“JavaScript怎么实现拖拽排序效果”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“JavaScript怎么实现拖拽...
    99+
    2023-06-30
  • react中实现拖拽排序react-dnd功能
    dnd文档 html 拖拽排序 import React, { useState, useRef } from 'react'; import { cloneDeep } from...
    99+
    2023-02-06
    拖拽排序react-dnd react拖拽排序
  • React如何使用sortablejs实现拖拽排序
    目录React使用sortablejs实现拖拽排序sortablejs之强大的拖拽库安装基本示例常用配置总结React使用sortablejs实现拖拽排序 1、使用npm装包 $ n...
    99+
    2023-01-16
    React使用sortablejs sortablejs实现拖拽排序 sortablejs拖拽排序
  • js实现模态框拖拽
    本文实例为大家分享了js实现模态框拖拽的具体代码,供大家参考,具体内容如下 效果展示和需求分析 效果展示 需求分析 点击登录后登录表单和遮罩层显示,点击关闭按钮隐藏。输入密码时可以...
    99+
    2022-11-13
  • Android recyclerview实现拖拽排序和侧滑删除
    Recyclerview现在基本已经替代Listview了,RecyclerView也越来越好用了  当我们有实现条目的拖拽排序和侧滑删除时  可以直接时候...
    99+
    2022-06-06
    recyclerview 排序 Android
  • vue怎么实现列表拖拽排序效果
    这篇文章主要介绍“vue怎么实现列表拖拽排序效果”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue怎么实现列表拖拽排序效果”文章能帮助大家解决问题。效果如下:<template>&nb...
    99+
    2023-06-29
  • js如何实现拖拽功能
    这篇文章主要为大家展示了“js如何实现拖拽功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“js如何实现拖拽功能”这篇文章吧。效果图:(红色方块可任意拖动)代码...
    99+
    2022-10-19
  • js实现拖拽拼图游戏
    本文实例为大家分享了js实现拖拽拼图游戏的具体代码,供大家参考,具体内容如下 该游戏主要使用了一些拖拽事件,以及对数据传递的应用,直接上代码,感兴趣的可以参考 html:代码 ...
    99+
    2022-11-12
  • 使用微信小程序实现拖拽排序功能
    使用微信小程序实现拖拽排序功能 示例代码刚开始学习微信小程序时,我一直以为实现拖拽排序功能是很困难的事情。然而,通过深入研究官方文档和尝试不同的方法,我终于成功地实现了这一功能。在本篇文章中,我将分享实现拖拽排序功能的具体代码示例。首先,在...
    99+
    2023-11-21
    微信小程序 功能实现 拖拽排序
  • 微信小程序怎么实现图片拖拽排序
    这篇文章主要介绍“微信小程序怎么实现图片拖拽排序”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“微信小程序怎么实现图片拖拽排序”文章能帮助大家解决问题。首先来看效果对于组件内部来说。笔者提供了一个参数...
    99+
    2023-06-29
  • 微信小程序实现长按拖拽排序功能
    工作中遇到一个上传图片长按后拖动排序的一个功能,于是写下了一个小demo。希望能对你有遇到的问题有帮助。 演示效果: wxml <view class='outer' >...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作