vue移動(dòng)端實(shí)現(xiàn)左滑編輯與刪除的全過(guò)程
前言
根據(jù)項(xiàng)目需要使用 Vue-touch 實(shí)現(xiàn)了一個(gè)vue移動(dòng)端的左滑編輯和刪除功能,廢話不多說(shuō),先看效果圖,然后上代碼吧!
方法如下:
第一步:安裝 vue-touch
npm install vue-touch@next --save
第二步:main.js 中引入
import VueTouch from 'vue-touch'; Vue.use(VueTouch, { name: 'v-touch' });
第三步:使用(用v-touch包住你要左滑刪除的內(nèi)容)
<div class="wrap"> <v-touch style="margin-bottom:10px" v-on:panstart="onPanStart(key)" v-on:panmove="onPanMove" v-on:panend="onPanEnd" v-for="(item, key) in list" :key="key" > <!-- 下面div這一塊是我頁(yè)面需要左滑刪除的項(xiàng)目?jī)?nèi)容,你可以替換成你自己的 --> <div class="item df_sb item-p" :style="activeId === key ? swipe : ''"> <p class="left-img"> <img :src="item.image_url" alt> </p> <p class="url" v-if="item.redirect_url != '' ">{{item.redirect_url}}</p> <p class="city nothave" v-else>無(wú)</p> <p class="city">{{item.city}}</p> <div class="edit-delete df_sad" :ref="'editBtn' + key"> <div class="edit" @click="editFun('edit',item.id,item.image_url,item.redirect_url)"> <img src="../../assets/images/adver/ic_xiugai.png" alt> </div> <p class="edit-line"></p> <div class="ad-delete" @click="deleteFun(key,item.id)"> <img src="../../assets/images/adver/ic_shanchu.png" alt> </div> </div> </div> </v-touch> </div>
第四步:定義變量,以及方法,下面代碼可直接拷貝,將不需要的刪除換成自己的,需要的留著就行
<script> import httpApi from "../../http/httpApi"; export default { name: "", data() { return { swipe: "", // 滑動(dòng)的樣式 wd: 0, // 編輯和刪除按鈕的寬度之和 swipeWd: 0, // 已經(jīng)滑動(dòng)的距離 activeId: "", // 實(shí)際是上一次的活動(dòng)id //以上四個(gè)變量必須保留,下面的三個(gè)可以刪除 page: 1, size: 10, list: [] }; }, methods: { //請(qǐng)求列表數(shù)據(jù) getList($state) { let params = new URLSearchParams(); params.append("page", this.page); params.append("size", this.size); this.$post(httpApi.BANNERLIST, params) .then(res => { if (res.code == 10000) { if (res.data) { this.list = this.list.concat(res.data.list); this.page++; if (res.data.list.length === 10) { $state.loaded(); } else { $state.complete(); } } else { $state.complete(); } } else { $state.complete(); } }) .catch(err => { console.log(err); }); }, // 編輯 editFun(type, image_id, image, url) { this.$router.push({ path: "/issueAdvertising", }); }, // 刪除 deleteFun(index, image_id) { this.activeId = ""; //將上一次的活動(dòng)id制空 let params = new URLSearchParams(); params.append("agent_id", this.id); params.append("image_id", image_id); this.$post(httpApi.DELETEBANNER, params) .then(res => { if (res.code == 10000) { // 雖然請(qǐng)求刪除接口刪除了列表其中的某一項(xiàng)內(nèi)容,但是頁(yè)面上還有 //因此需要在本地?cái)?shù)組中也刪除,這樣才完美,下面這行代碼比較重要,可以寫在你刪除接口成功后的地方 this.list.splice(index, 1); this.modal.toastFun("刪除成功"); } else if (res.code == 20000) { this.modal.toastFun(res.message); } }) .catch(err => {}); }, // 以下三個(gè)方法全部拷貝,無(wú)需修改 //滑動(dòng)位置 onPanStart(id) { event.preventDefault(); // 獲取右側(cè)按鈕寬度 let str = "editBtn" + id; this.wd = 1.2 * this.$refs[str][0].offsetWidth; // 初始化 if (this.activeId != id) { this.swipe = "transform:translateX(0px)"; this.swipeWd = 0; } this.activeId = id; }, //滑動(dòng)位置 onPanMove(event) { event.preventDefault(); let deltaX = event.deltaX; // 組件向左移動(dòng)直到最大距離 if (deltaX < 0 && deltaX > -this.wd) { // 向左滑動(dòng) this.swipe = "transform:translateX(" + deltaX + "px)"; this.swipeWd = deltaX; } if (deltaX > 0 && deltaX <= this.wd && this.swipeWd < 0) { // 向右滑動(dòng) let wx = deltaX + this.swipeWd; this.swipe = "transform:translateX(" + wx + "px)"; } }, // 結(jié)束位置 onPanEnd(event) { event.preventDefault(); // 判斷向左移動(dòng)的距離是否大于二分之一 let deltaX = event.deltaX; if (deltaX < 0) { if (deltaX <= -this.wd / 2) { // 向左滑動(dòng)超過(guò)二分之一 this.swipe = "transform:translateX(" + -this.wd + "px)"; this.swipeWd = -this.wd; } else { this.swipe = "transform:translateX(0px)"; this.swipeWd = 0; } } else { if (this.swipeWd < 0 && deltaX >= this.wd / 2) { // 向左滑動(dòng)超過(guò)二分之一 this.swipe = "transform:translateX(0px)"; this.swipeWd = 0; } else { this.swipe = "transform:translateX(" + this.swipeWd + "px)"; } } } }, }; </script>
style
我只貼出了上面代碼的css樣式,根據(jù)需求自行刪減吧,有需要的留著,不需要的刪除,需要改變的自行修改
.wrap { width: 100%; height: 100%; overflow: hidden; } .item { padding-left: 40px; height: 120px; background: #ffffff; align-items: center; flex-direction: inherit; .left-img { width: 120px; height: 100px; overflow: hidden; img { min-width: 120px; height: 100px; } } } .url { width: 400px; padding: 10px 30px 0; box-sizing: border-box; word-wrap: break-word; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .city { text-align: center; min-width: 100px; } .item-p { color: #333333; font-size: 22px; } .nothave { color: #999999; } .hint { height: 40px; align-items: center; margin-bottom: 30px; } .line { width: 250px; height: 1px; background: #999999; opacity: 0.5; } .item { width: 120%; // 超過(guò)100% transition: 0.1s ease 0s; // 過(guò)渡效果 } .edit-line { width: 2px; height: 80px; background: rgba(255, 255, 255, 1); } .edit-delete { width: 160px; height: 100%; background: rgba(255, 126, 34, 1); opacity: 0.8; align-items: center; } .edit, .ad-delete { img { width: 42px; height: 42px; } } .add-btn { width: 200px; height: 80px; background: rgba(255, 126, 34, 1); box-shadow: 0px 0px 5px 0px rgba(221, 221, 236, 1); border-radius: 40px; text-align: center; line-height: 80px; color: #ffffff; font-size: 30px; position: fixed; bottom: 8%; left: 50%; transform: translateX(-50%); }
總結(jié)
有需要的拿走根據(jù)自己的需求稍做修改即可,寫的很詳細(xì),到此這篇關(guān)于vue移動(dòng)端實(shí)現(xiàn)左滑編輯與刪除的全過(guò)程的文章就介紹到這了,更多相關(guān)vue左滑編輯與刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?draggable組件實(shí)現(xiàn)拖拽及點(diǎn)擊無(wú)效問(wèn)題的解決
這篇文章主要介紹了vue?draggable組件實(shí)現(xiàn)拖拽及點(diǎn)擊無(wú)效問(wèn)題的解決,只需要在設(shè)置handle屬性就可以了,.defaultTypeTag 是要拖拽的塊的類名,要注意的是需要做點(diǎn)擊事件的項(xiàng)不能包含在這個(gè)類名里面,不然會(huì)無(wú)法觸發(fā)點(diǎn)擊事件,詳細(xì)解決辦法跟隨小編一起學(xué)習(xí)吧2022-05-05詳解vue computed的緩存實(shí)現(xiàn)原理
這篇文章主要介紹了vue computed的緩存實(shí)現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-04-04Vue整合Node.js直連Mysql數(shù)據(jù)庫(kù)進(jìn)行CURD操作過(guò)程詳解
這篇文章主要給大家分享Vue整合Node.js,直連Mysql數(shù)據(jù)庫(kù)進(jìn)行CURD操作的詳細(xì)過(guò)程,文中有詳細(xì)的代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07Vue中使用JsonView來(lái)展示Json樹的實(shí)例代碼
這篇文章主要介紹了Vue之使用JsonView來(lái)展示Json樹的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Vue.js一個(gè)文件對(duì)應(yīng)一個(gè)組件實(shí)踐
Vue.js實(shí)現(xiàn)"一個(gè)文件對(duì)應(yīng)一個(gè)組件",無(wú)需webpack等工具,按需加載組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10vue路由攔截及頁(yè)面跳轉(zhuǎn)的設(shè)置方法
這篇文章主要介紹了vue路由攔截及頁(yè)面跳轉(zhuǎn)的設(shè)置方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05