• wya1
    • 课程
    • 书籍
    • 工具
    • 博客
    • 登录
    • 注册

    小言_互联网的博客

    小言_互联网的博客

    个人资料

    小言_互联网

    • 访问:8055886次
    • 积分:0
    • 等级:1

    文章分类

    1. Java(577)
    2. Python(569)
    3. Linux(200)
    4. 笔记(181)
    5. 人工智能(180)
    6. 数据库(140)
    7. 算法(132)
    8. C++(130)
    9. MySQL(127)
    10. 机器学习(126)

    文章存档

    1. 2023年03月(33)
    2. 2023年02月(699)
    3. 2023年01月(754)
    4. 2022年12月(936)
    5. 2022年11月(1064)
    6. 2022年07月(232)
    7. 2022年06月(171)
    8. 2022年03月(35)
    9. 2021年06月(233)
    10. 2021年05月(1243)
    11. 2021年04月(1215)
    12. 2021年03月(775)
    13. 2021年02月(837)
    14. 2021年01月(1158)
    15. 2020年12月(876)
    16. 2020年11月(289)
    17. 2020年10月(373)
    18. 2020年09月(627)
    19. 2020年08月(143)
    20. 2020年07月(71)
    21. 2020年06月(782)
    22. 2020年05月(887)
    23. 2020年04月(1911)
    24. 2020年03月(253)
    25. 2020年02月(179)
    26. 2019年10月(9)

    阅读排行

    1. 20+个很棒的 Python 脚本的集合(迷你项目)(194610)
    2. 解决报错:org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : [no body](15132)
    3. 安装Photoshop时出现The installation cannot continue as the installer file may be damaged. Download the in(10728)
    4. STM32单片机之温湿度检测系统(DTH11、OLED、LCD1602)(10596)
    5. 误差分析计算公式及其 matlab 代码实现(mse、mape、rmse等)(9723)
    6. 【elementUI】关于el-input限制输入整数、数字范围以及在form表单验证中限制输入正整数(8307)
    7. 手把手教你配置linux下C++开发工具——vim+ycm(YouCompleteMe),支持基于语义的自动补全和第三方库补全(史上最简单、史上最透彻、史上最全的终极解决方案)(7395)
    8. 银河麒麟高级服务器操作系统V10上安装k8s单机集群(6507)
    9. 【Yolo3】一文掌握图像标注、训练、识别(Keras+TensorFlow-gpu)(5722)
    10. 基于STM32L431设计的云端绿化管理系统(ESP8266+阿里云物联网平台)(5412)

    评论排行

    1. 了解Axios及其运用方式(0)
    2. Vue中 引入使用 babel-polyfill 兼容低版本浏览器(0)
    3. 1000亿数据、30W级qps如何架构?来一个天花板案例(0)
    4. STM32开发(16)----CubeMX配置DMA(0)
    5. Hadoop综合案例 - 聊天软件数据(0)
    6. 基于JWT实现用户身份认证(0)
    7. 【企业云端全栈开发实践-2】Spring Boot Controller(0)
    8. 使用手工特征提升模型性能(0)
    9. 《MySql学习》 Select 查询语句慢的非性能原因(0)
    10. 客户端攻击(溯源攻击,获取客户端信息)(0)

    推荐文章

    1. 20+个很棒的 Python 脚本的集合(迷你项目)
    2. 又发现一个ChatGPT国内镜像站,无次数限制也无广告
    3. 解决报错:org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : [no body]
    4. 解决 eslint 的 Parsing error: Unexpected token 错误
    5. Proxy error: Could not proxy request
    6. 五、肺癌检测-数据集训练 training.py model.py
    7. 安装Photoshop时出现The installation cannot continue as the installer file may be damaged. Download the in
    8. STM32单片机之温湿度检测系统(DTH11、OLED、LCD1602)
    9. 误差分析计算公式及其 matlab 代码实现(mse、mape、rmse等)
    10. echarts——实现3D地图+3D柱状图 效果——粗糙代码记录——技能提升

    vue 实现左滑删除功能

    2021-05-22 11:20 553人阅读  评论(0)

    实现效果

    代码如下

    html

    <template>
      <div>
        <div class="biggestBox">
          <ul>
            <!-- data-type=0 隐藏删除按钮 data-type=1 显示删除按钮 -->
            <li class="li_vessel" v-for="(item,index) in lists " data-type="0" :key="index">
              <!-- "touchstart" 当手指触摸屏幕时候触发  "touchend"  当手指从屏幕上离开的时候触发  "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">删除</div>
            </li>
          </ul>
        </div>
      </div>
    </template>
    

    js

    export default {
       
      name: "index",
      data() {
       
        return {
       
          lists: [
            {
       
              title: "标题1",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题1",
              faddish: "爆款",
              price: "¥12.00",
            },
            {
       
              title: "标题2",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题2",
              faddish: "爆款",
              price: "¥58.00",
            },
            {
       
              title: "标题3",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题3",
              faddish: "爆款",
              price: "¥99.99",
            },
            {
       
              title: "标题4",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题4",
              faddish: "爆款",
              price: "¥88.32",
            },
            {
       
              title: "标题5",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题5",
              faddish: "爆款",
              price: "¥9999.99",
            },
          ],
          startX: 0, //滑动开始
          endX: 0, //滑动结束
        };
      },
      methods: {
       
        // 向左滑动出现删除按钮时,点击商品信息区域取消删除
        oneself() {
       
          if (this.checkSlide()) {
       
            this.restSlide();
          } else {
       
            // 点击商品信息弹出弹框
            alert("hello Word!");
          }
        },
        //滑动开始
        touchStart(e) {
       
          // 记录初始位置
          this.startX = e.touches[0].clientX;
        },
        //滑动结束
        touchEnd(e) {
       
          // 当前滑动的父级元素
          let parentElement = e.currentTarget.parentElement;
          // 记录结束位置
          this.endX = e.changedTouches[0].clientX;
          // 左滑大于30距离删除出现
          if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
       
            this.restSlide();
            parentElement.dataset.type = 1;
          }
          // 右滑
          if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
       
            this.restSlide();
            parentElement.dataset.type = 0;
          }
          this.startX = 0;
          this.endX = 0;
        },
        //判断当前是否有滑块处于滑动状态
        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;
        },
        //复位滑动状态
        restSlide() {
       
          let listItems = document.querySelectorAll(".li_vessel");
          // 复位
          for (let i = 0; i < listItems.length; i++) {
       
            listItems[i].dataset.type = 0;
          }
        },
        //删除数据信息
        remove(e) {
       
          // 当前索引值
          let index = e.currentTarget.dataset.index;
          // 复位
          this.restSlide();
          // 删除数组lists中一个数据
          this.lists.splice(index, 1);
        },
      },
    };
    

    css

    <style>
    * {
       
      /* 消除默认内外边距 */
      margin: 0;
      padding: 0;
    }
    body {
       
      background: rgb(246, 245, 250);
    }
    .biggestBox {
       
      overflow: hidden; /*超出部分隐藏*/
    }
    ul {
       
      /* 消除 ul 默认样式 */
      list-style: none;
      padding: 0;
      margin: 0;
    }
    
    .li_vessel {
       
      /* 全部样式 0.2秒 缓动*/
      transition: all 0.2s;
    }
    /* =0隐藏 */
    .li_vessel[data-type="0"] {
       
      transform: translate3d(0, 0, 0);
    }
    /* =1显示 */
    .li_vessel[data-type="1"] {
       
      /* -64px 设置的越大可以左滑的距离越远,最好与下面删除按钮的宽度以及定位的距离设置一样的值*/
      transform: translate3d(-64px, 0, 0);
    }
    /* 删除按钮 */
    .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;
    }
    /* 左边的图片样式 */
    .contant {
       
      overflow: hidden; /*消除图片带来的浮动*/
      padding: 10px;
      background: #ffffff;
    }
    
    .contant .image {
       
      width: 80px;
      height: 80px;
      border-radius: 4px;
      float: left;
    }
    /* 右边的文字信息样式 */
    .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>
    

    完整代码如下

    <template>
      <div>
        <div class="biggestBox">
          <ul>
            <!-- data-type=0 隐藏删除按钮 data-type=1 显示删除按钮 -->
            <li class="li_vessel" v-for="(item,index) in lists " data-type="0" :key="index">
              <!-- "touchstart" 当手指触摸屏幕时候触发  "touchend"  当手指从屏幕上离开的时候触发  "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">删除</div>
            </li>
          </ul>
        </div>
      </div>
    </template>
    
    <script>
    export default {
       
      name: "index",
      data() {
       
        return {
       
          lists: [
            {
       
              title: "标题1",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题1",
              faddish: "爆款",
              price: "¥12.00",
            },
            {
       
              title: "标题2",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题2",
              faddish: "爆款",
              price: "¥58.00",
            },
            {
       
              title: "标题3",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题3",
              faddish: "爆款",
              price: "¥99.99",
            },
            {
       
              title: "标题4",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题4",
              faddish: "爆款",
              price: "¥88.32",
            },
            {
       
              title: "标题5",
              imgUrl: "https://z3.ax1x.com/2021/05/18/gfwKHg.jpg",
              subheading: "副标题5",
              faddish: "爆款",
              price: "¥9999.99",
            },
          ],
          startX: 0, //滑动开始
          endX: 0, //滑动结束
        };
      },
      methods: {
       
        // 向左滑动出现删除按钮时,点击商品信息区域取消删除
        oneself() {
       
          if (this.checkSlide()) {
       
            this.restSlide();
          } else {
       
            // 点击商品信息弹出弹框
            alert("hello Word!");
          }
        },
        //滑动开始
        touchStart(e) {
       
          // 记录初始位置
          this.startX = e.touches[0].clientX;
        },
        //滑动结束
        touchEnd(e) {
       
          // 当前滑动的父级元素
          let parentElement = e.currentTarget.parentElement;
          // 记录结束位置
          this.endX = e.changedTouches[0].clientX;
          // 左滑大于30距离删除出现
          if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
       
            this.restSlide();
            parentElement.dataset.type = 1;
          }
          // 右滑
          if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
       
            this.restSlide();
            parentElement.dataset.type = 0;
          }
          this.startX = 0;
          this.endX = 0;
        },
        //判断当前是否有滑块处于滑动状态
        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;
        },
        //复位滑动状态
        restSlide() {
       
          let listItems = document.querySelectorAll(".li_vessel");
          // 复位
          for (let i = 0; i < listItems.length; i++) {
       
            listItems[i].dataset.type = 0;
          }
        },
        //删除数据信息
        remove(e) {
       
          // 当前索引值
          let index = e.currentTarget.dataset.index;
          // 复位
          this.restSlide();
          // 删除数组lists中一个数据
          this.lists.splice(index, 1);
        },
      },
    };
    </script>
    
    <style>
    * {
       
      /* 消除默认内外边距 */
      margin: 0;
      padding: 0;
    }
    body {
       
      background: rgb(246, 245, 250);
    }
    .biggestBox {
       
      overflow: hidden; /*超出部分隐藏*/
    }
    ul {
       
      /* 消除 ul 默认样式 */
      list-style: none;
      padding: 0;
      margin: 0;
    }
    
    .li_vessel {
       
      /* 全部样式 0.2秒 缓动*/
      transition: all 0.2s;
    }
    /* =0隐藏 */
    .li_vessel[data-type="0"] {
       
      transform: translate3d(0, 0, 0);
    }
    /* =1显示 */
    .li_vessel[data-type="1"] {
       
      /* -64px 设置的越大可以左滑的距离越远,最好与下面删除按钮的宽度以及定位的距离设置一样的值*/
      transform: translate3d(-64px, 0, 0);
    }
    /* 删除按钮 */
    .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;
    }
    /* 左边的图片样式 */
    .contant {
       
      overflow: hidden; /*消除图片带来的浮动*/
      padding: 10px;
      background: #ffffff;
    }
    
    .contant .image {
       
      width: 80px;
      height: 80px;
      border-radius: 4px;
      float: left;
    }
    /* 右边的文字信息样式 */
    .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>
    

    转载:https://blog.csdn.net/Shids_/article/details/116994548
    查看评论
    * 以上用户言论只代表其个人观点,不代表本网站的观点或立场

    Copyright © 2016 wya1 wya1.com All Rights Reserved 鄂ICP备15022242号-2