Vue拖拽排序組件Vue-Slicksort解讀
一、效果圖

二、安裝組件
npm i vue-slicksort -S
三、使用組件
<div class="maintenance_img mt50 mb50 pl20 pr20" style="font-size: 16px;font-weight: 600;position: relative;">
<!-- 拖拽 -->
<SlickList
:lockToContainerEdges="true"
lockAxis="x"
axis="x"
v-model="maintenanceData.img_list"
@input="getChangeLists"
style="display: flex"
>
<SlickItem style="z-index: 10000" v-for="(item, index) in maintenanceData.img_list" :index="index" :key="index">
<div class="maintenance_top">
<img :src="item" @mouseover="changeMask(index)" @mouseout="changeMask(index)" alt="">
</div>
</SlickItem>
</SlickList>
<div style="display: flex;position: absolute;bottom: -15px;">
<div class="maintenance_icon" v-for="(item, index) in maintenanceData.img_list" :key="index">
<div class="img_bg" :ref="'mask' + index" @mouseover="changeMask(index)" @mouseout="changeMask(index)">
<Icon @click.stop="isImgShow = true;bigImg = item" class="pointer" size="20" color="#000000" type="md-search" />
<Icon @click.stop="downloadImg(item)" class="pointer" size="20" color="#000000" type="md-download" />
<Icon @click.stop="movingItems(4, index)" class="pointer" size="20" color="#000000" type="md-trash" />
</div>
</div>
</div>
<!-- 600*330 -->
<div class="add-img" v-if="maintenanceData.img_list.length<5">
<span>
<Icon type="md-add" size="30"></Icon>
</span>
<p>添加圖片</p>
<input @change="uploadImegs($event, 1)" type="file" accept="image/*" />
</div>
</div>
import { SlickList, SlickItem } from "vue-slicksort";
export default {
components:{
SlickItem,
SlickList
},
data() {
return {
maintenanceData: {
img_list: [], //圖片
},
}
},
created() {
},
methods: {
getChangeLists(vals) {
// 拖拽完成后返回新的排序數(shù)組
console.log(vals);
},
},
}
.maintenance_top {
display: flex;
z-index: 10000;
}
.maintenance_top {
width: 140px;
height: 78px;
border: 1px dashed #ccc;
border-radius: 6px;
display: flex;
align-items: center;
position: relative;
margin-right: 15px;
}
.maintenance_top > img{
max-width: 138px;
max-height: 138px;
border-radius: 6px;
}
.maintenance_icon{
width: 140px;
display: flex;
align-items: center;
position: relative;
margin-right: 15px;
}
.maintenance_img{
display: flex;
}
.maintenance_img>div>div{
z-index: 10000;
}
.maintenance_img > .add-img{
display: block;
width: 140px;
height: 78px;
border-radius: 6px;
}
.img_bg{
width: 100%;
height: 40px;
position: absolute;
bottom: -20px;
left: 0;
border-radius: 6px;
display: none;
align-items: center;
justify-content: space-evenly;
}
四、組件參數(shù)
| 名稱 | 類型 | 默認(rèn)值 | 說明 |
|---|---|---|---|
| value | Array | - | 列表的內(nèi)容 |
| axis | String | y | 列表元素可以被橫向拖拽,縱向拖拽還是網(wǎng)格拖拽。用x,y,xy來表示 |
| lockAxis | Array | - | 用于排序時(shí)在坐標(biāo)系中鎖定元素的移動 |
| helperClass | String | - | helper的自定義樣式類 |
| transitionDuration | Number | 300 | 元素移動動畫的持續(xù)時(shí)間 |
| pressDelay | Number | 0 | 如果需要當(dāng)元素被按下一段時(shí)間再允許拖拽,可以設(shè)置這個(gè)屬性 |
| pressThreshold | Number | 5 | 移動允許被忽略的閾值,單位是像素 |
| distance | Number | 0 | 如果需要在拖拽出一定距離之后才被識別為正在拖拽的元素,可以設(shè)置這個(gè)屬性 |
| useDragHandle | Boolean | false | 如果使用HandleDirective,設(shè)置為true |
| useWindowAsScrollContainer | Boolean | false | 是否設(shè)置window為可滾動的容器 |
| hideSortableGhost | Boolean | true | 是否自動隱藏ghost元素 |
| lockToContainerEdges | Boolean | false | 是否對正在拖拽的元素鎖定容器邊緣 |
| lockOffset | String | 50% | 對正在拖拽的元素鎖定容器邊緣的偏移量 |
| shouldCancelStart | Function | - | 在拖拽開始前這個(gè)方法將被調(diào)用 |
| getHelperDimensions | Function | - | 可選方法({node, index, collection}),用于返回SortableHelper的計(jì)算尺寸 |
五、組件方法
| 名稱 | 參數(shù) | 說明 |
|---|---|---|
| sortStart | event, node, index, collection | 當(dāng)拖拽開始時(shí)觸發(fā) |
| sortMove | event | 當(dāng)拖拽時(shí)鼠標(biāo)移動時(shí)觸發(fā) |
| sortEnd | event, newIndex, oldIndex, collection | 當(dāng)拖拽結(jié)束時(shí)觸發(fā) |
| input | newList | 當(dāng)拖拽結(jié)束后產(chǎn)生新的列表時(shí)觸發(fā) |
HandleDirective
v-handle 指令在可拖動元素內(nèi)部使用。(即用了這個(gè)指令,可以讓拖動只在元素的某個(gè)位置生效)
Container 必須由 :useDragHandle 屬性,且設(shè)置為 true 時(shí)才能正常工作。
這里有關(guān)于此的一個(gè)簡單元素的例子:
<template>
<li class="list-item">
<!-- 拖動只在 span 元素上生效 -->
<span v-handle class="handle"></span>
{{item.value}}
</li>
</template>
<script>
import { ElementMixin, HandleDirective } from 'vue-slicksort';
export default {
mixins: [ElementMixin],
directives: { handle: HandleDirective },
};
</script>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue 配合vue-resource調(diào)用接口獲取數(shù)據(jù)
本篇文章主要介紹了vue 配合vue-resource調(diào)用接口獲取數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
本文記錄作者在vue項(xiàng)目中使用到Google第三方登錄,查詢到的資料文檔也不詳細(xì),故此把自己所遇到的坑及問題詳細(xì)的記錄下來。2021-07-07
vue遞歸組件實(shí)現(xiàn)樹形結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了vue遞歸組件實(shí)現(xiàn)樹形結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
基于vue.js實(shí)現(xiàn)側(cè)邊菜單欄
這篇文章主要為大家詳細(xì)介紹了基于vue.js實(shí)現(xiàn)側(cè)邊菜單欄的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
vue如何通過點(diǎn)擊事件實(shí)現(xiàn)頁面跳轉(zhuǎn)詳解
頁面跳轉(zhuǎn),我們一般都通過路由跳轉(zhuǎn)實(shí)現(xiàn),通常情況下可直接使用router-link標(biāo)簽實(shí)現(xiàn)頁面跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于vue如何通過點(diǎn)擊事件實(shí)現(xiàn)頁面跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2022-07-07
Vue.js如何利用v-for循環(huán)生成動態(tài)標(biāo)簽
這篇文章主要給大家介紹了關(guān)于Vue.js如何利用v-for循環(huán)生成動態(tài)標(biāo)簽的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01

