溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue2怎么實(shí)現(xiàn)左滑刪除功能

發(fā)布時(shí)間:2022-04-28 10:47:51 來源:億速云 閱讀:95 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“vue2怎么實(shí)現(xiàn)左滑刪除功能”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“vue2怎么實(shí)現(xiàn)左滑刪除功能”吧!

左滑刪除,很多UI框架里有,比如Mint-UI, Muse-UI等,一開始我們就是用的這兩個(gè),但是我們需求是要:左滑的時(shí)候出現(xiàn)img然后來實(shí)現(xiàn)點(diǎn)擊刪除,如下:

vue2怎么實(shí)現(xiàn)左滑刪除功能

因?yàn)橛肬I框架,還得改底層代碼= =所以,我們小組就直接寫了一個(gè)- -,心累……此組件多地方使用,所以建議還是放到common下。。

<template>
  <div class="left-delete">
    <div class="move"
       @touchstart="_touchStart"
       @touchmove="_touchMove"
       @touchend="_touchEnd"
       :>
      <slot></slot>
    </div>
    <div class="deleteIcon" : @click.prevent="deleteItem(index)"></div>
  </div>
</template>

<script>
  export default {
    props: {
      index: Number
    },
    data() {
      return {
        startX: 0,    //觸摸位置
         moveX: 0,    //滑動(dòng)時(shí)的位置
         disX: 0,    //移動(dòng)距離
         txtStyle: '',
        delWidth: 200,
        top: '',
        zIndex: 'z-index:-1',
      }
    },
    methods: {
      _touchStart: function(ev) {
        ev = ev || event;
        if(ev.touches.length == 1){
          // 手指按下的時(shí)候記錄按下的位置
          this.startX = ev.touches[0].clientX;
          console.log(this.startX)
        }
      },
      _touchMove: function(ev) {
        ev = ev || event;
        if(ev.touches.length == 1) {
          // 滑動(dòng)過程中的實(shí)時(shí)位置
          this.moveX = ev.touches[0].clientX
          // 滑動(dòng)過程中實(shí)時(shí)計(jì)算滑動(dòng)距離
          this.disX = this.startX - this.moveX;
          // console.log('disX==>',this.disX)
          // 如果是向右滑動(dòng)或者只是點(diǎn)擊,不改變滑動(dòng)位置
          if(this.disX < 0 || this.disX == 0) {
            // console.log('沒有移動(dòng)');
            this.txtStyle = "transform:translateX(0rem)";
          }else if (this.disX > 0) {
如果是向左滑動(dòng),則實(shí)時(shí)給這個(gè)根元素一個(gè)向左的偏移-left,當(dāng)偏移量到達(dá)固定值delWidth時(shí),固定元素的偏移量為 delWidth
            this.txtStyle = "transform:translateX(-" + this.disX/100 + "rem)";
            if (this.disX >= this.delWidth/100) {
              this.txtStyle = "transform:translateX(-" + this.delWidth/100 + "rem)";
              this.zIndex = "z-index:" + 10 + "rem";
            }
          }
        }
      },
      _touchEnd: function(ev) {
        if (event.changedTouches.length == 1) {
          this.startX = 0;
          this.zIndex = "z-index:" + -1 + "rem";
          console.log(event.changedTouches[0].clientX)
          // 手指移動(dòng)結(jié)束后的水平位置
          let endX = event.changedTouches[0].clientX;
          // 觸摸開始與結(jié)束,手指移動(dòng)的距離
          this.disX = this.startX - endX;
          //如果距離小于刪除按鈕的1/2,不顯示刪除按鈕
        }
      },
      deleteItem: function(index) {
        this.$emit('deleteItem', index);
      }
    }
  }
</script>

<style>
  .left-delete{
    width:100%;
    height:100%;
    position:relative;
    z-index:2;
  }
  .move{
    position: relative;
  }
  .deleteIcon{
    width: 2rem;
    height:100%;
    position: absolute;
    right:0;
    top:0;
    background:url(./../../assets/main/4.png) no-repeat;
    background-size: contain;
  }
</style>

然后哪個(gè)頁(yè)面需要,哪個(gè)頁(yè)面引入就好。比如myCollect頁(yè)面需要,那么如下:

<template>
  <section class="myCollect">
 <section>
      <div v-for="(item,index) in collectionList">
        <left-slider :index="index" @deleteItem="deleteItem">
          <Financial :item="item" :index="index"></Financial>
        </left-slider>
      </div>
    </section>
  </section>
</template>
<script>
  import api from './../../fetch/api';
  import { mapGetters } from 'vuex';

  import Financial from './../common/financial.vue';
  import LeftSlider from './../common/leftSlider.vue';

  export default {
    name: 'MyCollect',
    props: {
      item: Object,
      index: Number
    },
    components: {
      Financial,
      LeftSlider
    },
    data() {
      return {

      }
    },
    created() {
      this.getCollectionList();
    },
    methods: {

    },
    computed: {
        ...mapGetters([
          'getContextPathSrc',
          'sessionId',
          'token'
        ]),
  },
  methods: {
    // 刪除
    deleteItem: function(index) {
      api.commonApi('后臺(tái)接口,請(qǐng)求數(shù)據(jù)')//此處api是封裝的axios,詳情看文章:vue2+vuex+axios即可
          .then(res => {
        console.debug("REQ_ADD_STORE.res.data.result -> " + res.data.result);
      this.collectionList.splice(index, 1);
    });
    }
  },
  mounted() {

  }
  }
</script>

感謝各位的閱讀,以上就是“vue2怎么實(shí)現(xiàn)左滑刪除功能”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)vue2怎么實(shí)現(xiàn)左滑刪除功能這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI