Vue project implements left swipe delete function (complete code)

Vue project implements left swipe delete function (complete code)

Achieve results

insert image description here

The code is as follows

html

<template>
  <div>
    <div class="biggestBox">
      <ul>
        <!-- data-type=0 Hide the delete button data-type=1 Show the delete button -->
        <li class="li_vessel" v-for="(item,index) in lists " data-type="0" :key="index">
          <!-- "touchstart" is triggered when the finger touches the screen "touchend" is triggered when the finger leaves the screen "capture" is used for event capture -->
          <div @touchstart.capture="touchStart" @touchend.capture="touchEnd" @click="oneself">
            <div class="contant">
              <img class="image" :src="item.imgUrl" alt />
              <div class="rightBox">
                <div>{{item.title}}</div>
                <div>{{item.subheading}}</div>
                <div>{{item.faddish}}</div>
                <div>{{item.price}}</div>
              </div>
            </div>
          </div>
          <div class="removeBtn" @click="remove" :data-index="index">Delete</div>
        </li>
      </ul>
    </div>
  </div>
</template>

js

export default {
  name: "index",
  data() {
    return {
      lists: [
        {
          title: "Title 1",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 1",
          faddish: "hot item",
          price: "¥12.00",
        },
        {
          title: "Title 2",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 2",
          faddish: "hot item",
          price: "¥58.00",
        },
        {
          title: "Title 3",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 3",
          faddish: "hot item",
          price: "¥99.99",
        },
        {
          title: "Title 4",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 4",
          faddish: "hot item",
          price: "¥88.32",
        },
        {
          title: "Title 5",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 5",
          faddish: "hot item",
          price: "¥9999.99",
        },
      ],
      startX: 0, //slide start endX: 0, //slide end};
  },
  methods: {
    // When the delete button appears when swiping left, click the product information area to cancel the deletion oneself() {
      if (this.checkSlide()) {
        this.restSlide();
      } else {
        // Click on the product information to pop up the alert box ("hello Word!");
      }
    },
    //Slide start touchStart(e) {
      //Record the initial position this.startX = e.touches[0].clientX;
    },
    //Slide end touchEnd(e) {
      // The parent element of the current slide let parentElement = e.currentTarget.parentElement;
      // Record end position this.endX = e.changedTouches[0].clientX;
      // Delete if the left slide distance is greater than 30 if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
        this.restSlide();
        parentElement.dataset.type = 1;
      }
      // Swipe right if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
        this.restSlide();
        parentElement.dataset.type = 0;
      }
      this.startX = 0;
      this.endX = 0;
    },
    // Check if there is a slider in the sliding state checkSlide() {
      let listItems = document.querySelectorAll(".li_vessel");
      for (let i = 0; i < listItems.length; i++) {
        if (listItems[i].dataset.type == 1) {
          return true;
        }
      }
      return false;
    },
    //Reset sliding state restSlide() {
      let listItems = document.querySelectorAll(".li_vessel");
      // Reset for (let i = 0; i < listItems.length; i++) {
        listItems[i].dataset.type = 0;
      }
    },
    //Delete data information remove(e) {
      // Current index value let index = e.currentTarget.dataset.index;
      // Reset this.restSlide();
      // Delete a data in the array lists this.lists.splice(index, 1);
    },
  },
};

CSS

<style>
* {
  /* Remove default inner and outer margins*/
  margin: 0;
  padding: 0;
}
body {
  background: rgb(246, 245, 250);
}
.biggestBox {
  overflow: hidden; /*The exceeding part is hidden*/
}
ul {
  /* Eliminate ul default style */
  list-style: none;
  padding: 0;
  margin: 0;
}

.li_vessel {
  /* 0.2 second easing for all styles*/
  transition: all 0.2s;
}
/* =0 to hide */
.li_vessel[data-type="0"] {
  transform: translate3d(0, 0, 0);
}
/* =1 display */
.li_vessel[data-type="1"] {
  /* The larger the -64px setting, the farther you can swipe left. It is best to set the same value as the width and positioning distance of the delete button below*/
  transform: translate3d(-64px, 0, 0);
}
/* Delete button */
.li_vessel .removeBtn {
  width: 64px;
  height: 103px;
  background: #ff4949;
  font-size: 16px;
  color: #fff;
  text-align: center;
  line-height: 22px;
  position: absolute;
  top: 0px;
  right: -64px;
  line-height: 103px;
  text-align: center;
  border-radius: 2px;
}
/* Left picture style*/
.contant {
  overflow: hidden; /*Eliminate the floating caused by the image*/
  padding: 10px;
  background: #ffffff;
}

.contant .image {
  width: 80px;
  height: 80px;
  border-radius: 4px;
  float: left;
}
/* Text information style on the right*/
.rightBox {
  overflow: hidden;
  padding-left: 8px;
}
.rightBox div:first-child {
  font-weight: bold;
}
.rightBox div:nth-child(2) {
  margin-top: 4px;
  font-size: 14px;
}
.rightBox div:nth-child(3) {
  width: 24px;
  background: rgb(219, 91, 113);
  color: white;
  font-size: 12px;
  text-align: center;
  padding: 2px 4px 2px 4px;
  margin-left: auto;
}
.rightBox div:last-child {
  color: red;
  font-size: 14px;
  font-weight: bold;
}
</style>

The complete code is as follows

<template>
  <div>
    <div class="biggestBox">
      <ul>
        <!-- data-type=0 Hide the delete button data-type=1 Show the delete button -->
        <li class="li_vessel" v-for="(item,index) in lists " data-type="0" :key="index">
          <!-- "touchstart" is triggered when the finger touches the screen "touchend" is triggered when the finger leaves the screen "capture" is used for event capture -->
          <div @touchstart.capture="touchStart" @touchend.capture="touchEnd" @click="oneself">
            <div class="contant">
              <img class="image" :src="item.imgUrl" alt />
              <div class="rightBox">
                <div>{{item.title}}</div>
                <div>{{item.subheading}}</div>
                <div>{{item.faddish}}</div>
                <div>{{item.price}}</div>
              </div>
            </div>
          </div>
          <div class="removeBtn" @click="remove" :data-index="index">Delete</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: "index",
  data() {
    return {
      lists: [
        {
          title: "Title 1",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 1",
          faddish: "hot item",
          price: "¥12.00",
        },
        {
          title: "Title 2",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 2",
          faddish: "hot item",
          price: "¥58.00",
        },
        {
          title: "Title 3",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 3",
          faddish: "hot item",
          price: "¥99.99",
        },
        {
          title: "Title 4",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 4",
          faddish: "hot item",
          price: "¥88.32",
        },
        {
          title: "Title 5",
          imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
          subheading: "Subheading 5",
          faddish: "hot item",
          price: "¥9999.99",
        },
      ],
      startX: 0, //slide start endX: 0, //slide end};
  },
  methods: {
    // When the delete button appears when swiping left, click the product information area to cancel the deletion oneself() {
      if (this.checkSlide()) {
        this.restSlide();
      } else {
        // Click on the product information to pop up the alert box ("hello Word!");
      }
    },
    //Slide start touchStart(e) {
      //Record the initial position this.startX = e.touches[0].clientX;
    },
    //Slide end touchEnd(e) {
      // The parent element of the current slide let parentElement = e.currentTarget.parentElement;
      // Record end position this.endX = e.changedTouches[0].clientX;
      // Delete if the left slide distance is greater than 30 if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
        this.restSlide();
        parentElement.dataset.type = 1;
      }
      // Swipe right if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
        this.restSlide();
        parentElement.dataset.type = 0;
      }
      this.startX = 0;
      this.endX = 0;
    },
    // Check if there is a slider in the sliding state checkSlide() {
      let listItems = document.querySelectorAll(".li_vessel");
      for (let i = 0; i < listItems.length; i++) {
        if (listItems[i].dataset.type == 1) {
          return true;
        }
      }
      return false;
    },
    //Reset sliding state restSlide() {
      let listItems = document.querySelectorAll(".li_vessel");
      // Reset for (let i = 0; i < listItems.length; i++) {
        listItems[i].dataset.type = 0;
      }
    },
    //Delete data information remove(e) {
      // Current index value let index = e.currentTarget.dataset.index;
      // Reset this.restSlide();
      // Delete a data in the array lists this.lists.splice(index, 1);
    },
  },
};
</script>

<style>
* {
  /* Remove default inner and outer margins*/
  margin: 0;
  padding: 0;
}
body {
  background: rgb(246, 245, 250);
}
.biggestBox {
  overflow: hidden; /*The exceeding part is hidden*/
}
ul {
  /* Eliminate ul default style */
  list-style: none;
  padding: 0;
  margin: 0;
}

.li_vessel {
  /* 0.2 second easing for all styles*/
  transition: all 0.2s;
}
/* =0 to hide */
.li_vessel[data-type="0"] {
  transform: translate3d(0, 0, 0);
}
/* =1 display */
.li_vessel[data-type="1"] {
  /* The larger the -64px setting, the farther you can swipe left. It is best to set the same value as the width and positioning distance of the delete button below*/
  transform: translate3d(-64px, 0, 0);
}
/* Delete button */
.li_vessel .removeBtn {
  width: 64px;
  height: 103px;
  background: #ff4949;
  font-size: 16px;
  color: #fff;
  text-align: center;
  line-height: 22px;
  position: absolute;
  top: 0px;
  right: -64px;
  line-height: 103px;
  text-align: center;
  border-radius: 2px;
}
/* Left picture style*/
.contant {
  overflow: hidden; /*Eliminate the floating caused by the image*/
  padding: 10px;
  background: #ffffff;
}

.contant .image {
  width: 80px;
  height: 80px;
  border-radius: 4px;
  float: left;
}
/* Text information style on the right*/
.rightBox {
  overflow: hidden;
  padding-left: 8px;
}
.rightBox div:first-child {
  font-weight: bold;
}
.rightBox div:nth-child(2) {
  margin-top: 4px;
  font-size: 14px;
}
.rightBox div:nth-child(3) {
  width: 24px;
  background: rgb(219, 91, 113);
  color: white;
  font-size: 12px;
  text-align: center;
  padding: 2px 4px 2px 4px;
  margin-left: auto;
}
.rightBox div:last-child {
  color: red;
  font-size: 14px;
  font-weight: bold;
}
</style>

The above is the detailed content of Vue's implementation of the left swipe to delete function. For more information about Vue's left swipe to delete, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Vue project WebPack packaging deletes comments and console
  • How to delete the dist directory when using rimraf dev in vue project
  • Detailed explanation of using mockjs in vue-cli project (request data to delete data)
  • The problem and solution of Vue project cannot be deleted

<<:  Mybatis mysql delete in operation can only delete the first data method

>>:  Detailed explanation of CentOS7 online installation of Docker 17.03.2 using Alibaba Cloud Docker Yum source

Recommend

Sample code for achieving small triangle border effect with pure CSS3+DIV

The specific code is as follows: The html code is...

About Nginx gzip configuration

The principle of nginx to achieve resource compre...

10 ways to view compressed file contents in Linux (summary)

Generally speaking, when we view the contents of ...

Database query which object contains which field method statement

The database queries which object contains which ...

32 Typical Column/Grid-Based Websites

If you’re looking for inspiration for columnar web...

How to expand Linux swap memory

Swap memory mainly means that when the physical m...

Why can't my tomcat start?

Table of contents Phenomenon: Port usage: Spellin...

Better-scroll realizes the effect of linking menu and content

1. Basic use <!DOCTYPE html> <html lang=...

Pure js to achieve typewriter effect

This article example shares the specific code of ...

Detailed steps for porting busybox to build a minimal root file system

Busybox: A Swiss Army knife filled with small com...

Implementation of rewrite jump in nginx

1. New and old domain name jump Application scena...

Mysql database recovery actual record by time point

Introduction: MySQL database recovery by time poi...

How to decompress multiple files using the unzip command in Linux

Solution to the problem that there is no unzip co...

Detailed explanation of the principle and function of Vue list rendering key

Table of contents The principle and function of l...