vue如何實現(xiàn)拖動圖片進行排序Vue.Draggable
拖動圖片進行排序Vue.Draggable
好久沒接觸vue的項目了,最近接到一個vue圖片處理的小項目,有一項需求是要實現(xiàn)拖動圖片就行排序。
接到這個需求之后立馬想到了Vue.Draggable這款基于Sortable.js實現(xiàn)的vue拖拽插件
下面是實現(xiàn)過程
第一步:安裝Vue.Draggable。兩種方式npm和yarn (我用的npm)
npm i -S vuedraggable yarn add vuedraggable
第二步:全局安裝完成之后在main.js中全局引用也行在需要的頁面也用也沒問題 (我因為就一個頁面需要我就單頁面引入的)
<script> import draggable from "vuedraggable" export default { ? components: { ? ? draggable ? } } </script>
第三步:安裝和引用都完成了,現(xiàn)在開始使用了。
<template> ? <div class="col"> ? ? <draggable v-model="data" filter=".forbid" animation="300" :move="onMove"> ? ? ? <transition-group> ? ? ? ? <div v-for="(item, index) in data" :key="index"> ? ? ? ? ? <div class="img-hover"> ? ? ? ? ? ? <img :src="'http://xxxxx' + item.img"/> ? ? ? ? ? </div> ? ? ? ? </div> ? ? ? </transition-group> ? ? </draggable> ? </div> </template>
注意:
/** ?*代碼中的data為你的數據列表,onMove為你在拖動圖片是觸發(fā)的函數方法。 ?*transition-group標簽下一定是v-for循環(huán),不然會報錯。 **/
這樣,一個簡單的圖片拖拽排序的簡單demo就完成了。
項目draggable拖拽移動圖片順序
第一步、導入
npm i vuedraggable
第二步、組件引入
import draggable from 'vuedraggable';
第三步、定義組件
components: { draggable},
第四步、頁面中使用
<template> ? <div class="app-container"> ? ? ? <div :class="canEdit? 'dargBtn-lock el-icon-unlock': 'dargBtn-lock el-icon-lock' " @click="removeEvent()">{{canEdit? '調整':'鎖定'}}</div> ? ? ? <ul class="projset-content"> ? ? ? ? <draggable ? ? ? ? ? :move="onMove" ? ? ? ? ? :list="imgList" ? ? ? ? ? handle=".dargBtn" ? ? ? ? ? :animation="200" ? ? ? ? ? filter=".undraggable" ? ? ? ? > ? ? ? ? ? <li v-for="(item, index) in imgList" :key="index" :class="canEdit ? 'draggable' : 'undraggable'"> ? ? ? ? ? ? <div class="dargBtn"> ? ? ? ? ? ? ? <svg-icon icon-class="drag" /> ? ? ? ? ? ? </div> ? ? ? ? ? ? <img :src="item.path" alt=""> ? ? ? ? ? ? <span>{{item.name}}</span> ? ? ? ? ? </li> ? ? ? ? </draggable> ? ? ? </ul> ? </div> </template>
<script> ? import draggable from 'vuedraggable'; ? export default { ? ? components: { draggable}, ? ? data(){ ? ? ? return{ ? ? ? ? canEdit:true, ? ? ? ? imgList: [ ? ? ? ? ? { ? ? ? ? ? ? path: 'https://lupic.cdn.bcebos.com/20210629/3000005161_14.jpg', ? ? ? ? ? ? name: '1', ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? path: 'https://lupic.cdn.bcebos.com/20210629/26202931_14.jpg', ? ? ? ? ? ? name: '2', ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? path: 'https://lupic.cdn.bcebos.com/20210629/27788166_14.jpg', ? ? ? ? ? ? name: '3', ? ? ? ? ? } ? ? ? ? ] ? ? ? } ? ? }, ? ? created() { ? ? }, ? ? mounted(){}, ? ? methods:{ ? ? ? onMove(relatedContext, draggedContext){ ? ? ? ? console.log(relatedContext.relatedContext.list); ? ? ? }, ? ? ? removeEvent (item) { ? ? ? ? if(this.canEdit){ ? ? ? ? ? this.canEdit = false; ? ? ? ? }else{ ? ? ? ? ? this.canEdit = true; ? ? ? ? } ? ? ? ? console.log(this.canEdit); ? ? ? } ? ? } ? } </script>
<style scoped lang="scss"> ? .app-container{ ? ? background: #ffffff; ? ? height: 100%; ? ? .dargBtn-lock{ ? ? ? margin: 0px 50px; ? ? ? color: #2ea9df; ? ? } ? ? .projset-content{ ? ? ? list-style-type: none; ? ? ? position: relative; ? ? ? li{ ? ? ? ? display: inline-block; ? ? ? ? margin: 10px; ? ? ? } ? ? ? img{ ? ? ? ? width: 141px; ? ? ? ? height: 100px; ? ? ? } ? ? ? span{ ? ? ? ? justify-content: center; ? ? ? ? display: flex; ? ? ? } ? ? ? .dargBtn{ ? ? ? ? position: absolute; ? ? ? ? line-height: 100px; ? ? ? ? text-align: center; ? ? ? ? width: 141px; ? ? ? ? height: 100px; ? ? ? ? display: none; ? ? ? ? color: white; ? ? ? ? background: rgba(101, 101, 101, 0.6); ? ? ? } ? ? ? .draggable{ ? ? ? ? cursor: pointer; ? ? ? ? width: 141px; ? ? ? ? height: 100px; ? ? ? } ? ? ? .draggable:hover .dargBtn{ ? ? ? ? display: block; ? ? ? } ? ? } ? } </style>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
iview實現(xiàn)select tree樹形下拉框的示例代碼
這篇文章主要介紹了iview實現(xiàn)select tree樹形下拉框的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-12-12vue-cli-service和webpack-dev-server的區(qū)別及說明
這篇文章主要介紹了vue-cli-service和webpack-dev-server的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10Vue3+TS+Vant3+Pinia(H5端)配置教程詳解
這篇文章主要介紹了Vue3+TS+Vant3+Pinia(H5端)配置教程詳解,需要的朋友可以參考下2023-01-01