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

vue實現(xiàn)拖拽排序效果

 更新時間:2022年08月30日 10:03:12   作者:朝陽39  
這篇文章主要為大家詳細介紹了vue實現(xiàn)拖拽排序效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)拖拽排序效果的具體代碼,供大家參考,具體內(nèi)容如下

效果預(yù)覽

組件 drag.vue

<template>
? <TransitionGroup name="group-list" tag="ul">
? ? <li
? ? ? v-for="(item, index) in list"
? ? ? :key="item.name"
? ? ? :draggable="item.draggable"
? ? ? :class="[
? ? ? ? 'list-item',
? ? ? ? {
? ? ? ? ? 'is-dragover':
? ? ? ? ? ? index === dropIndex && item.draggable && config.exchange,
? ? ? ? },
? ? ? ]"
? ? ? @dragstart="onDragstart($event, index)"
? ? ? @dragenter="onDragenter(index)"
? ? ? @dragover.prevent="onDragover(index)"
? ? ? @dragleave="onDragleave"
? ? ? @dragend="onDragend"
? ? ? @drop="onDrop"
? ? >
? ? ? <slot :item="item" />
? ? </li>
? </TransitionGroup>
</template>

<script>
export default {
? name: "Draggable",
? props: {
? ? list: {
? ? ? type: Array,
? ? ? default: () => [],
? ? },
? ? config: {
? ? ? type: Object,
? ? ? default: () => ({
? ? ? ? name: "",
? ? ? ? push: true,
? ? ? ? pull: true,
? ? ? ? exchange: true,
? ? ? }),
? ? },
? },

? data() {
? ? return {
? ? ? dragIndex: null,
? ? ? dropIndex: null,
? ? };
? },

? computed: {
? ? isPush() {
? ? ? const { dropIndex, dragIndex } = this;
? ? ? return dropIndex !== null && dragIndex === null;
? ? },

? ? isExchange() {
? ? ? const { dropIndex, dragIndex } = this;
? ? ? return dragIndex !== null && dropIndex !== null;
? ? },

? ? pushCbName() {
? ? ? const {
? ? ? ? config: { name },
? ? ? } = this;
? ? ? return `${name}-push-callback`;
? ? },
? },

? methods: {
? ? onDragstart(e, i) {
? ? ? const {
? ? ? ? list,
? ? ? ? config: { name },
? ? ? ? transferData,
? ? ? } = this;

? ? ? this.dragIndex = i;

? ? ? if (name) {
? ? ? ? transferData({ e, key: name, type: "set", data: list[i] });
? ? ? } else {
? ? ? ? throw new Error("缺少配置關(guān)聯(lián)名name");
? ? ? }

? ? ? this.$emit("drag-start", i);
? ? },

? ? onDragenter(i) {
? ? ? this.dropIndex = i;
? ? ? this.$emit("drag-enter", i);
? ? },

? ? onDragover(i) {
? ? ? const { dragIndex, dropIndex } = this;
? ? ? if (i === dragIndex || i === dropIndex) return;
? ? ? this.dropIndex = i;
? ? ? this.$emit("drag-over", i);
? ? },

? ? onDragleave() {
? ? ? this.dropIndex = null;
? ? },

? ? onDrop(e) {
? ? ? const {
? ? ? ? list,
? ? ? ? dropIndex,
? ? ? ? dragIndex,
? ? ? ? config: { name, push: enablePush, exchange },
? ? ? ? isPush,
? ? ? ? isExchange,
? ? ? ? pushCbName,
? ? ? ? storage,
? ? ? ? resetIndex,
? ? ? ? transferData,
? ? ? } = this;

? ? ? if (dropIndex === dragIndex || !exchange) return;

? ? ? if (isPush) {
? ? ? ? if (!enablePush) {
? ? ? ? ? resetIndex();
? ? ? ? ? return;
? ? ? ? }

? ? ? ? if (name) {
? ? ? ? ? list.splice(
? ? ? ? ? ? dropIndex,
? ? ? ? ? ? 0,
? ? ? ? ? ? transferData({ e, key: name, type: "get" })
? ? ? ? ? );

? ? ? ? ? storage("set", pushCbName, true);
? ? ? ? } else {
? ? ? ? ? resetIndex();
? ? ? ? ? throw new Error("缺少配置關(guān)聯(lián)屬性name");
? ? ? ? }
? ? ? ? resetIndex();
? ? ? ? return;
? ? ? }

? ? ? if (isExchange) {
? ? ? ? const drapItem = list[dragIndex];
? ? ? ? const dropItem = list[dropIndex];
? ? ? ? list.splice(dropIndex, 1, drapItem);
? ? ? ? list.splice(dragIndex, 1, dropItem);
? ? ? }

? ? ? resetIndex();
? ? },

? ? onDragend() {
? ? ? const {
? ? ? ? list,
? ? ? ? dragIndex,
? ? ? ? config: { pull: enablePull },
? ? ? ? pushCbName,
? ? ? ? storage,
? ? ? ? resetIndex,
? ? ? } = this;

? ? ? if (enablePull) {
? ? ? ? const isPushSuccess = storage("get", pushCbName);

? ? ? ? if (isPushSuccess) {
? ? ? ? ? list.splice(dragIndex, 1);
? ? ? ? ? storage("remove", pushCbName);
? ? ? ? }
? ? ? }
? ? ? resetIndex();
? ? ? this.$emit("drag-end");
? ? },

? ? storage(type, key, value) {
? ? ? return {
? ? ? ? get() {
? ? ? ? ? return JSON.parse(localStorage.getItem(key));
? ? ? ? },
? ? ? ? set() {
? ? ? ? ? localStorage.setItem(key, JSON.stringify(value));
? ? ? ? },
? ? ? ? remove() {
? ? ? ? ? localStorage.removeItem(key);
? ? ? ? },
? ? ? }[type]();
? ? },

? ? resetIndex() {
? ? ? this.dropIndex = null;
? ? ? this.dragIndex = null;
? ? },

? ? transferData({ e, key, type, data } = {}) {
? ? ? if (type === "get") {
? ? ? ? return JSON.parse(e.dataTransfer.getData(`${key}-drag-key`));
? ? ? }

? ? ? if (type === "set") {
? ? ? ? e.dataTransfer.setData(`${key}-drag-key`, JSON.stringify(data));
? ? ? }
? ? },
? },
};
</script>

<style ?scoped>
.list-item {
? list-style: none;
? position: relative;
? margin-bottom: 10px;
? border-radius: 4px;
? padding: 4px;
? background-color: #fff;
? cursor: move;
}

.list-item.is-dragover::before {
? content: "";
? position: absolute;
? bottom: -4px;
? left: 0;
? width: 100%;
? height: 4px;
? background-color: #0c6bc9;
}

.list-item.is-dragover::after {
? content: "";
? position: absolute;
? bottom: -8px;
? left: -6px;
? border: 3px solid #0c6bc9;
? border-radius: 50%;
? width: 6px;
? height: 6px;
? background-color: #fff;
}

.group-list-move {
? transition: transform 0.8s;
}
</style>

使用范例

index.vue

<template>
? <div class="dragBox">
? ? <Drag style="width: 200px" :list="list1" :config="config1">
? ? ? <template v-slot="{ item }">
? ? ? ? <div class="item">
? ? ? ? ? {{ item.name }}
? ? ? ? </div>
? ? ? </template>
? ? </Drag>

? ? <Drag style="width: 200px" :list="list2" :config="config2">
? ? ? <template v-slot="{ item }">
? ? ? ? <div class="item">
? ? ? ? ? {{ item.name }}
? ? ? ? </div>
? ? ? </template>
? ? </Drag>
? </div>
</template>

<script>
import Drag from "./drag.vue";

export default {
? components: {
? ? Drag,
? },

? data() {
? ? return {
? ? ? list1: new Array(10).fill(0).map((_, i) => ({
? ? ? ? name: `列表1 - ${i + 1}`,
? ? ? ? draggable: true,
? ? ? })),

? ? ? config1: {
? ? ? ? name: "test",
? ? ? ? push: true,
? ? ? ? pull: true,
? ? ? ? exchange: true,
? ? ? },

? ? ? list2: new Array(10).fill(0).map((_, i) => ({
? ? ? ? name: `列表2 - ${i + 1}`,
? ? ? ? draggable: true,
? ? ? })),

? ? ? config2: {
? ? ? ? name: "test",
? ? ? ? push: true,
? ? ? ? pull: true,
? ? ? ? exchange: true,
? ? ? },
? ? };
? },
};
</script>

<style ?scoped>
.dragBox {
? display: flex;
? justify-content: center;
}
.item {
? border: 1px solid #ccc;
? width: 200px;
? height: 30px;
? text-align: center;
}
</style>

參數(shù)說明

list: 渲染列表
config: {
    name: '', // 跨列表關(guān)聯(lián)名,跨列表拖拽時必傳
    push: true, // 當(dāng)前列表是否支持從其他列表push元素進來
    pull: true, // 將當(dāng)前列表的某個元素拖拽并添加到其他列表里,該元素是否從當(dāng)前列表移除
    exchange: true, // 當(dāng)前列表元素之間是否支持交換位置
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue3組件化開發(fā)常用API知識點總結(jié)

    vue3組件化開發(fā)常用API知識點總結(jié)

    Vue是目前Web前端最流行的開發(fā)框架技術(shù),?下面這篇文章主要給大家介紹了關(guān)于vue3組件化開發(fā)常用API的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • vue解決跨域問題(推薦)

    vue解決跨域問題(推薦)

    這篇文章主要介紹了vue解決跨域問題,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • vue使用@include或@mixin報錯的問題及解決

    vue使用@include或@mixin報錯的問題及解決

    這篇文章主要介紹了vue使用@include或@mixin報錯的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • vue日歷/日程提醒/html5本地緩存功能

    vue日歷/日程提醒/html5本地緩存功能

    這篇文章主要介紹了vue日歷/日程提醒/html5本地緩存功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • vue 項目中實現(xiàn)按鈕防抖方法

    vue 項目中實現(xiàn)按鈕防抖方法

    這篇文章主要介紹了vue 項目中實現(xiàn)按鈕防抖方法,首先需要新建 .js文件存放防抖方法,引入防抖文件,methods中添加方法,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • vue虛擬DOM和render()函數(shù)詳解

    vue虛擬DOM和render()函數(shù)詳解

    這篇文章主要講述了Vue.js中虛擬DOM和render()函數(shù)的使用方法,虛擬DOM是Vue.js的核心概念,它通過JavaScript對象來表示DOM樹,從而提高性能,render()函數(shù)比模板更接近編譯器,適用于需要JavaScript編程能力的場景,通過比較更新前后虛擬DOM結(jié)構(gòu)中的差異
    2024-12-12
  • vue計算屬性computed、事件、監(jiān)聽器watch的使用講解

    vue計算屬性computed、事件、監(jiān)聽器watch的使用講解

    今天小編就為大家分享一篇關(guān)于vue計算屬性computed、事件、監(jiān)聽器watch的使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • mui-player自定義底部導(dǎo)航在vue項目中顯示不出來的解決

    mui-player自定義底部導(dǎo)航在vue項目中顯示不出來的解決

    這篇文章主要介紹了mui-player自定義底部導(dǎo)航在vue項目中顯示不出來的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • element中datepicker日期選擇器選擇周一到周日并實現(xiàn)上一周和下一周的方法

    element中datepicker日期選擇器選擇周一到周日并實現(xiàn)上一周和下一周的方法

    最近項目中需要用到日期選擇器,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于element中datepicker日期選擇器選擇周一到周日并實現(xiàn)上一周和下一周的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • Vue父子組件傳值的一些坑

    Vue父子組件傳值的一些坑

    這篇文章主要介紹了Vue父子組件傳值的一些坑,幫助大家更好的理解和使用vue父子組件,感興趣的朋友可以了解下
    2020-09-09

最新評論

望谟县| 辰溪县| 东宁县| 洪湖市| 丹寨县| 普陀区| 香格里拉县| 宣武区| 寿阳县| 宁南县| 新巴尔虎右旗| 响水县| 密云县| 临武县| 镇巴县| 芜湖市| 故城县| 勐海县| 集贤县| 天等县| 西吉县| 固镇县| 永州市| 临颍县| 安阳县| 兴山县| 青田县| 和田市| 阿克陶县| 上蔡县| 宜丰县| 河南省| 广南县| 大冶市| 淮北市| 观塘区| 清镇市| 鹤峰县| 顺昌县| 吴江市| 东安县|