亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js 實(shí)現(xiàn)拖拽排序詳情

 更新時(shí)間:2021年11月08日 10:18:55   作者:名字起太長(zhǎng)會(huì)有傻子跟著念  
這篇文章主要介紹了js 實(shí)現(xiàn)拖拽排序,拖拽排序?qū)τ谛』锇閭儊?lái)說(shuō)應(yīng)該不陌生,平時(shí)工作的時(shí)候,可能會(huì)選擇使用類似Sortable.js這樣的開(kāi)源庫(kù)來(lái)實(shí)現(xiàn)需求。但在完成需求后,大家有沒(méi)有沒(méi)想過(guò)拖拽排序是如何實(shí)現(xiàn)的呢?感興趣得話一起來(lái)看看下面文章得小心內(nèi)容吧

1、前言

拖拽排序?qū)τ谛』锇閭儊?lái)說(shuō)應(yīng)該不陌生,平時(shí)工作的時(shí)候,可能會(huì)選擇使用類似Sortable.js這樣的開(kāi)源庫(kù)來(lái)實(shí)現(xiàn)需求。但在完成需求后,大家有沒(méi)有沒(méi)想過(guò)拖拽排序是如何實(shí)現(xiàn)的呢?我花了點(diǎn)時(shí)間研究了一下,今天分享給大家。

2、實(shí)現(xiàn)

 {
    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; // 父級(jí)元素
        this.cloneElementClassName = options.cloneElementClassName; // 克隆元素類名
        this.isPointerdown = false;
        this.diff = { x: 0, y: 0 }; // 相對(duì)于上一次移動(dòng)差值
        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 = []; // 用于保存拖拽項(xiàng)getBoundingClientRect()方法獲得的數(shù)據(jù)
        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) {
        // 如果是鼠標(biāo)點(diǎn)擊,只響應(yīng)左鍵
        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++) {
                // 碰撞檢測(cè)
                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; // 觸發(fā)重繪
                        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);
    }
}
// 實(shí)例化
new Draggable({
    element: document.querySelector('.grid'),
    cloneElementClassName: 'clone-grid-item'
});

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

3、為何不使用HTML拖放API實(shí)現(xiàn)?

因?yàn)樵?code>HTML拖放API在移動(dòng)端無(wú)法使用,所以為了兼容PC端和移動(dòng)端,使用了PointerEvent事件實(shí)現(xiàn)拖拽邏輯。

4、總結(jié)

拖拽排序的基本功能已經(jīng)實(shí)現(xiàn),但還存在很多不足。像嵌套拖拽,跨列表拖拽,拖拽到底部自動(dòng)滾動(dòng)等功能都未實(shí)現(xiàn)。

到此這篇關(guān)于js 實(shí)現(xiàn)拖拽排序詳情的文章就介紹到這了,更多相關(guān)js 實(shí)現(xiàn)拖拽排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Tree-Shaking?機(jī)制快速掌握

    Tree-Shaking?機(jī)制快速掌握

    這篇文章主要為大家介紹了Tree-Shaking?機(jī)制的快速掌握教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • vue長(zhǎng)列表優(yōu)化之虛擬列表實(shí)現(xiàn)過(guò)程詳解

    vue長(zhǎng)列表優(yōu)化之虛擬列表實(shí)現(xiàn)過(guò)程詳解

    前端的業(yè)務(wù)開(kāi)發(fā)中會(huì)遇到不使用分頁(yè)方式來(lái)加載長(zhǎng)列表的需求,下面這篇文章主要給大家介紹了關(guān)于vue長(zhǎng)列表優(yōu)化之虛擬列表實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Vue如何使用混合Mixins和插件開(kāi)發(fā)詳解

    Vue如何使用混合Mixins和插件開(kāi)發(fā)詳解

    這篇文章主要介紹了Vue如何使用混合Mixins和插件開(kāi)發(fā)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 讓webpack+vue-cil項(xiàng)目不再自動(dòng)打開(kāi)瀏覽器的方法

    讓webpack+vue-cil項(xiàng)目不再自動(dòng)打開(kāi)瀏覽器的方法

    今天小編就為大家分享一篇讓webpack+vue-cil項(xiàng)目不再自動(dòng)打開(kāi)瀏覽器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn)

    Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn)

    這篇文章主要介紹了Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • elementui實(shí)現(xiàn)表格自定義排序的示例代碼

    elementui實(shí)現(xiàn)表格自定義排序的示例代碼

    本文主要介紹了elementui實(shí)現(xiàn)表格自定義排序的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • vue3+uniapp 上傳附件的操作代碼

    vue3+uniapp 上傳附件的操作代碼

    uni-file-picker搭配uni.uploadFile在使用問(wèn)題上踩了不少坑,我至今還是沒(méi)辦法在不改uniapp源碼基礎(chǔ)上實(shí)現(xiàn)限制重復(fù)文件的上傳,這篇文章介紹vue3+uniapp 上傳附件的操作代碼,感興趣的朋友一起看看吧
    2024-01-01
  • vue Nprogress進(jìn)度條功能實(shí)現(xiàn)常見(jiàn)問(wèn)題

    vue Nprogress進(jìn)度條功能實(shí)現(xiàn)常見(jiàn)問(wèn)題

    這篇文章主要介紹了vue Nprogress進(jìn)度條功能實(shí)現(xiàn),NProgress是頁(yè)面跳轉(zhuǎn)是出現(xiàn)在瀏覽器頂部的進(jìn)度條,本文通過(guò)實(shí)例代碼給大家講解,需要的朋友可以參考下
    2021-07-07
  • Vue組件通信$attrs、$listeners實(shí)現(xiàn)原理解析

    Vue組件通信$attrs、$listeners實(shí)現(xiàn)原理解析

    這篇文章主要介紹了Vue組件通信$attrs、$listeners實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 基于Vue制作組織架構(gòu)樹組件

    基于Vue制作組織架構(gòu)樹組件

    最近公司在做一個(gè)基于vue開(kāi)發(fā)的項(xiàng)目,項(xiàng)目需要開(kāi)發(fā)一個(gè)展示組織架構(gòu)的樹組件,在網(wǎng)上搜了半天,沒(méi)有找到合適的,下面小編給大家分享一個(gè)基于Vue制作組織架構(gòu)樹組件,需要的朋友參考下吧
    2017-12-12

最新評(píng)論