最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年11月08日 10:18:55   作者:名字起太長會有傻子跟著念  
這篇文章主要介紹了js 實現(xiàn)拖拽排序,拖拽排序對于小伙伴們來說應該不陌生,平時工作的時候,可能會選擇使用類似Sortable.js這樣的開源庫來實現(xiàn)需求。但在完成需求后,大家有沒有沒想過拖拽排序是如何實現(xiàn)的呢?感興趣得話一起來看看下面文章得小心內容吧

1、前言

拖拽排序對于小伙伴們來說應該不陌生,平時工作的時候,可能會選擇使用類似Sortable.js這樣的開源庫來實現(xiàn)需求。但在完成需求后,大家有沒有沒想過拖拽排序是如何實現(xiàn)的呢?我花了點時間研究了一下,今天分享給大家。

2、實現(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; // 父級元素
        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()方法獲得的數(shù)據
        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; // 觸發(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);
    }
}
// 實例化
new Draggable({
    element: document.querySelector('.grid'),
    cloneElementClassName: 'clone-grid-item'
});

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

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

因為原生HTML拖放API在移動端無法使用,所以為了兼容PC端和移動端,使用了PointerEvent事件實現(xiàn)拖拽邏輯。

4、總結

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

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

相關文章

  • vue插件及修改ip啟動端口解析

    vue插件及修改ip啟動端口解析

    這篇文章主要為大家介紹了vue插件及修改ip啟動端口的解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • vue3時間插件之Moment.js使用教程

    vue3時間插件之Moment.js使用教程

    這篇文章主要給大家介紹了關于vue3時間插件之Moment.js使用的相關資料,需要的朋友可以參考下
    2023-09-09
  • vue 解決數(shù)組賦值無法渲染在頁面的問題

    vue 解決數(shù)組賦值無法渲染在頁面的問題

    今天小編就為大家分享一篇vue 解決數(shù)組賦值無法渲染在頁面的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • vue項目中canvas實現(xiàn)截圖功能

    vue項目中canvas實現(xiàn)截圖功能

    這篇文章主要為大家詳細介紹了vue項目中canvas實現(xiàn)截圖功能,截取圖片的一部分,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 通過實例解析vuejs如何實現(xiàn)調試代碼

    通過實例解析vuejs如何實現(xiàn)調試代碼

    這篇文章主要介紹了通過實例解析vuejs如何實現(xiàn)調試代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Vue中watch與watchEffect的區(qū)別詳細解讀

    Vue中watch與watchEffect的區(qū)別詳細解讀

    這篇文章主要介紹了Vue中watch與watchEffect的區(qū)別詳細解讀,watch函數(shù)與watchEffect函數(shù)都是監(jiān)聽器,在寫法和用法上有一定區(qū)別,是同一功能的兩種不同形態(tài),底層都是一樣的,需要的朋友可以參考下
    2023-11-11
  • 解決vue2 在mounted函數(shù)無法獲取prop中的變量問題

    解決vue2 在mounted函數(shù)無法獲取prop中的變量問題

    這篇文章主要介紹了vue2 在mounted函數(shù)無法獲取prop中的變量的解決方法 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Vue中的常用指令及用法總結

    Vue中的常用指令及用法總結

    在本篇文章里小編給大家整理的是一篇關于Vue中的常用指令及用法總結內容,對此有興趣的朋友們可以跟著學習下。
    2022-01-01
  • Vue Element前端應用開發(fā)之開發(fā)環(huán)境的準備工作

    Vue Element前端應用開發(fā)之開發(fā)環(huán)境的準備工作

    這篇文章主要介紹了Vue Element前端應用開發(fā)之開發(fā)環(huán)境的準備工作,對Vue感興趣的同學,可以來學習一下
    2021-05-05
  • vue學習教程之帶你一步步詳細解析vue-cli

    vue學習教程之帶你一步步詳細解析vue-cli

    這篇文章的主題是vue-cli的理解?;蛟S,很多人在開發(fā)vue的時候,我們會發(fā)現(xiàn)一個問題——只會去用,而不明白它的里面的東西?,F(xiàn)在的框架可以說是足夠的優(yōu)秀,讓開發(fā)者不用為搭建開發(fā)環(huán)境而煩惱。但是有時候,我們還是得回到原始生活體驗一下,才能夠讓自己更上層樓。
    2017-12-12

最新評論

巴中市| 勐海县| 晋城| 犍为县| 丘北县| 盐津县| 金门县| 高邑县| 琼结县| 平阳县| 扶绥县| 正定县| 嘉荫县| 永修县| 班玛县| 依安县| 苍梧县| 焉耆| 沙坪坝区| 临安市| 西峡县| 抚顺县| 原平市| 安塞县| 甘洛县| 图木舒克市| 牡丹江市| 汾阳市| 尖扎县| 冷水江市| 丹棱县| 南岸区| 浙江省| 通海县| 闵行区| 天峻县| 岳西县| 潮安县| 固镇县| 罗田县| 承德县|