欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue移動端實現(xiàn)左滑編輯與刪除的全過程

 更新時間:2021年05月23日 10:14:08   作者:趙小懶豬cium  
vue.js是現(xiàn)在流行的js框架之一,vue 是一套用于構(gòu)建用戶界面的漸進式j(luò)avascript框架,這篇文章主要給大家介紹了關(guān)于vue移動端實現(xiàn)左滑編輯與刪除的相關(guān)資料,需要的朋友可以參考下

前言

根據(jù)項目需要使用 Vue-touch 實現(xiàn)了一個vue移動端的左滑編輯和刪除功能,廢話不多說,先看效果圖,然后上代碼吧!

方法如下:

第一步:安裝   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這一塊是我頁面需要左滑刪除的項目內(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>無</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: "", // 滑動的樣式
      wd: 0, // 編輯和刪除按鈕的寬度之和
      swipeWd: 0, // 已經(jīng)滑動的距離
      activeId: "", // 實際是上一次的活動id
    //以上四個變量必須保留,下面的三個可以刪除
      page: 1,
      size: 10,
      list: []
    };
  },
  methods: {
//請求列表數(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 = ""; //將上一次的活動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) {
// 雖然請求刪除接口刪除了列表其中的某一項內(nèi)容,但是頁面上還有
//因此需要在本地數(shù)組中也刪除,這樣才完美,下面這行代碼比較重要,可以寫在你刪除接口成功后的地方
            this.list.splice(index, 1); 
            this.modal.toastFun("刪除成功");
          } else if (res.code == 20000) {
            this.modal.toastFun(res.message);
          }
        })
        .catch(err => {});
    },
 
// 以下三個方法全部拷貝,無需修改
//滑動位置
    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;
    },
//滑動位置
    onPanMove(event) {
      event.preventDefault();
      let deltaX = event.deltaX;
      // 組件向左移動直到最大距離
      if (deltaX < 0 && deltaX > -this.wd) {
        // 向左滑動
        this.swipe = "transform:translateX(" + deltaX + "px)";
        this.swipeWd = deltaX;
      }
 
      if (deltaX > 0 && deltaX <= this.wd && this.swipeWd < 0) {
        // 向右滑動
        let wx = deltaX + this.swipeWd;
        this.swipe = "transform:translateX(" + wx + "px)";
      }
    },
 // 結(jié)束位置
    onPanEnd(event) {
      event.preventDefault();
      // 判斷向左移動的距離是否大于二分之一
      let deltaX = event.deltaX;
      if (deltaX < 0) {
        if (deltaX <= -this.wd / 2) {
          // 向左滑動超過二分之一
          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) {
          // 向左滑動超過二分之一
          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%; // 超過100%
  transition: 0.1s ease 0s; // 過渡效果
}
.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ù)自己的需求稍做修改即可,寫的很詳細,到此這篇關(guān)于vue移動端實現(xiàn)左滑編輯與刪除的全過程的文章就介紹到這了,更多相關(guān)vue左滑編輯與刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • VUE前端刪除和批量刪除實現(xiàn)代碼

    VUE前端刪除和批量刪除實現(xiàn)代碼

    這篇文章主要給大家介紹了關(guān)于VUE前端刪除和批量刪除的相關(guān)資料, 在實際的開發(fā)中,我們可以使用Vue.js來快速實現(xiàn)批量刪除功能,文中給出了詳細的代碼示例,需要的朋友可以參考下
    2023-07-07
  • vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決

    vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決

    這篇文章主要介紹了vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決,只需要在設(shè)置handle屬性就可以了,.defaultTypeTag 是要拖拽的塊的類名,要注意的是需要做點擊事件的項不能包含在這個類名里面,不然會無法觸發(fā)點擊事件,詳細解決辦法跟隨小編一起學(xué)習(xí)吧
    2022-05-05
  • vue子組件獲取到它父組件數(shù)據(jù)的4種方法

    vue子組件獲取到它父組件數(shù)據(jù)的4種方法

    這篇文章主要給大家介紹了關(guān)于vue子組件獲取到它父組件數(shù)據(jù)的4種方法,對于vue來說組件之間的消息傳遞是非常重要的,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-08-08
  • 詳解vue computed的緩存實現(xiàn)原理

    詳解vue computed的緩存實現(xiàn)原理

    這篇文章主要介紹了vue computed的緩存實現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下
    2021-04-04
  • Vue整合Node.js直連Mysql數(shù)據(jù)庫進行CURD操作過程詳解

    Vue整合Node.js直連Mysql數(shù)據(jù)庫進行CURD操作過程詳解

    這篇文章主要給大家分享Vue整合Node.js,直連Mysql數(shù)據(jù)庫進行CURD操作的詳細過程,文中有詳細的代碼講解,具有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • Vue中使用JsonView來展示Json樹的實例代碼

    Vue中使用JsonView來展示Json樹的實例代碼

    這篇文章主要介紹了Vue之使用JsonView來展示Json樹的實例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Vue.js一個文件對應(yīng)一個組件實踐

    Vue.js一個文件對應(yīng)一個組件實踐

    Vue.js實現(xiàn)"一個文件對應(yīng)一個組件",無需webpack等工具,按需加載組件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • vue路由攔截及頁面跳轉(zhuǎn)的設(shè)置方法

    vue路由攔截及頁面跳轉(zhuǎn)的設(shè)置方法

    這篇文章主要介紹了vue路由攔截及頁面跳轉(zhuǎn)的設(shè)置方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-05-05
  • vue生成隨機驗證碼的示例代碼

    vue生成隨機驗證碼的示例代碼

    本篇文章主要介紹了vue生成隨機驗證碼的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧‘
    2017-09-09
  • Vue實現(xiàn)五子棋小游戲

    Vue實現(xiàn)五子棋小游戲

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評論