非原创,摘选来源:http://www.jb51.net/article/136221.htm。

废话不多说,相当实用,先记录。

Html代码:

<div class="container">
<div class="page-title">滑动组件</div>
<ul>
<li class="list-item " v-for="(item,index) in list " data-type="0">
<div class="list-box" @touchstart.capture="touchStart" @touchend.capture="touchEnd" @click="skip">
<img class="list-img" :src="item.imgUrl" alt="">
<div class="list-content">
<p class="title">{{item.title}}</p>
<p class="tips">{{item.tips}}</p>
<p class="time">{{item.time}}</p>
</div>
</div>
<div class="delete" @click="deleteItem" :data-index="index">删除</div>
</li>
</ul>
</div>

注意:这里的数据全是本地 mock 的~

Css样式代码:

.page-title{
text-align: center;
font-size: 17px;
padding: 10px 15px;
position: relative;
}
.page-title:after{
content: " ";
position: absolute;
left:;
bottom:;
right:;
height: 1px;
border-bottom: 1px solid #ccc;
color: #ccc;
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
z-index:;
}
.list-item{
position: relative;
height: 1.6rem;
-webkit-transition: all 0.2s;
transition: all 0.2s;
}
.list-item[data-type="0"]{
transform: translate3d(0,0,0);
}
.list-item[data-type="1"]{
transform: translate3d(-2rem,0,0);
}
.list-item:after{
content: " ";
position: absolute;
left: 0.2rem;
bottom:;
right:;
height: 1px;
border-bottom: 1px solid #ccc;
color: #ccc;
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
z-index:;
}
.list-box{
padding: 0.2rem;
background: #fff;
display: flex;
align-items: center;
-webkit-box-sizing: border-box;
box-sizing: border-box;
justify-content: flex-end;
position: absolute;
top:;
right:;
bottom:;
left:;
font-size:;
}
.list-item .list-img{
display: block;
width: 1rem;
height: 1rem;
}
.list-item .list-content{
padding: 0.1rem 0 0.1rem 0.2rem;
position: relative;
flex:;
flex-direction: column;
align-items: flex-start;
justify-content: center;
overflow: hidden;
}
.list-item .title{
display: block;
color: #333;
overflow: hidden;
font-size: 15px;
font-weight: bold;
text-overflow: ellipsis;
white-space: nowrap;
}
.list-item .tips{
display: block;
overflow: hidden;
font-size: 12px;
color: #999;
line-height: 20px;
text-overflow: ellipsis;
white-space: nowrap;
}
.list-item .time{
display: block;
font-size: 12px;
position: absolute;
right:;
top: 0.1rem;
color: #666;
}
.list-item .delete{
width: 2rem;
height: 1.6rem;
background: #ff4949;
font-size: 17px;
color: #fff;
text-align: center;
line-height: 1.6rem;
position: absolute;
top:;
right: -2rem;
}

这是核心的样式代码,还有部分样式重置代码放在了 App.vue 里,通过计算根节点 html 的字体大小的脚本放在了 index.html 中~

js代码:

export default{
name: 'index',
data () {
return {
list : [
  {
    title : 'Chrome更新了' ,
    imgUrl : './static/images/Chrome.png' ,
    tips : '不再支持Flash视频播放' ,
    time : '上午 8:30'
  },
  {
    title : '电影新资讯' ,
    imgUrl : './static/images/Sina.png' ,
    tips : '电影《红海行动》上映以来票房暴涨,很多国人表示对国产电影有了新的改观' ,
    time : '上午 12:00'
  },
  {
    title : '聚焦两会·共筑中国梦' ,
    imgUrl : './static/images/video.png' ,
    tips : '两会故事' ,
    time : '下午 17:45'
  },
  {
    title : '微信团队' ,
    imgUrl : './static/images/Wechat.png' ,
    tips : '您的帐号有异常登录,如非本人操作,请及时修改密码' ,
    time : '昨天'
  }
  ],
startX : 0 ,
endX : 0 ,
}
},
methods : {
//跳转
skip(){
if( this.checkSlide() ){
  this.restSlide();
}else{
  alert('You click the slide!')
}
},
//滑动开始
touchStart(e){
  // 记录初始位置
  this.startX = e.touches[0].clientX;
},
//滑动结束
touchEnd(e){
// 当前滑动的父级元素
  let parentElement = e.currentTarget.parentElement;
  // 记录结束位置
  this.endX = e.changedTouches[0].clientX;
// 左滑
  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('.list-item');
  for( let i = 0 ; i < listItems.length ; i++){
    if( listItems[i].dataset.type == 1 ) {
      return true;
  }
  }
  return false;
},
//复位滑动状态
restSlide(){
  let listItems = document.querySelectorAll('.list-item');
// 复位
  for( let i = 0 ; i < listItems.length ; i++){
    listItems[i].dataset.type = 0;
  }
},
//删除
deleteItem(e){
  // 当前索引
  let index = e.currentTarget.dataset.index;
  // 复位
  this.restSlide();
  // 删除
  this.list.splice(index,1);
  }
}
}

综上,亲测可用,叽叽叽叽叽叽~

Vue 仿QQ左滑删除功能(非原创)的更多相关文章

  1. Android开发学习之路-PopupWindow和仿QQ左滑删除

    这周作业,要做一个类似QQ的左滑删除效果的ListView,因为不想给每个item都放一个按钮,所以决定用PopupWindow,这里记录一下 先放一下效果图: 先说明一下这里面的问题: ①没有做到像 ...

  2. tableView左滑删除功能

    实现三个代理方法即可 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtI ...

  3. 基于touch.js 左滑删除功能

    左滑删除功能 完整代码如下: (touch.js) <!DOCTYPE html> <html> <head> <meta charset="UTF ...

  4. 模仿QQ左滑删除

    需求: 1.左滑删除 2.向左滑动距离超过一半的时候让它自动滑开,向右滑动超过一半的时候自动隐藏 3.一次只允许滑开一个item 还有,根本不需要自定义view来实现,谨防入坑 布局: <?xm ...

  5. 微信小程序左滑删除功能

    效果图如下: wxml代码: <view class="container"> <view class="touch-item {{item.isTou ...

  6. JS实现移动端购物车左滑删除功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  7. [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子

    转载:http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除 ...

  8. Android ListView左滑删除、左滑自定义功能

    最近项目需要ListView左滑删除功能,搜集了很多资料发现了一个某一前辈写的库能很简单的实现这个功能,而且有源码,直接拿来使用了. 库名字叫做SwipeMenuListView,下面给大家演示一下使 ...

  9. 仿QQ列表左滑删除

    一直想写个仿QQ通讯列表左滑删除的效果,今天终于忙里偷闲,简单一个. 大概思路是这样的: 通过 ontouchstartontouchmoveontouchend 结合css3的平移. 不多说,直接上 ...

随机推荐

  1. 网页存储倒计时与解决网页cookie保存多个相同key问题

    短信倒计时多用网页临时存储,这可以保证网页在关闭状态也可记时. <p class="test_button" id="getcode">获取验证码& ...

  2. AmqpException: No method found for class java.lang.String

    amqpTemplate发送消息用的String,接收消息用的Message,统一消息类型就可以

  3. SqlSugar入门级教程+实例 (.net core下的)

    官方参考:http://www.codeisbug.com/Doc/8 前言:这应该是目前最好用的ORM框架之一了,而且支持.net core,网上除了官方文档其他参考就少了点,自己整理了一下,大致包 ...

  4. python相关小技巧整理[持续更新]

    1. pdb的非常方便的debug,抛弃print吧~ 参考https://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/ impor ...

  5. System.Web.HttpContext.cs

    ylbtech-System.Web.HttpContext.cs 1.程序集 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken ...

  6. 使用Maven命令行下载依赖库

    这篇文章,不是教大家如何新建maven项目,不是与大家分享Eclipse与Maven整合. 注意:是在命令行下使用Maven下载依赖库. 废话不说,步骤如下: 1.保证电脑上已成功安装了JDK.运行j ...

  7. 3列滚动抽奖 jquery.slotmachine

    效果图: 需引入js文件: <script src="js/jquery-3.2.0.js"></script> <script src=" ...

  8. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

  9. Lock方法是用于数据库的锁机制,

    Lock方法是用于数据库的锁机制,如果在查询或者执行操作的时候使用: lock(true); 复制代码   就会自动在生成的SQL语句最后加上 FOR UPDATE或者FOR UPDATE NOWAI ...

  10. csp-s模拟测试61砖块, 数字,甜圈题解

    题面:https://www.cnblogs.com/Juve/articles/11626350.html 砖块: 直接模拟即可,map统计被覆盖的次数 #include<iostream&g ...