lemon_sideslip_list

功能描述

实用vue.js扩展的滑动删除插件,是基于vue.js 框架实现手势滑动删除功能

依赖的模块

  1. vue

快速使用

  1. <script type="text/javascript">
  2. (function(doc, win) {
  3. var docEl = doc.documentElement,
  4. resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
  5. recalc = function() {
  6. var clientWidth = docEl.clientWidth;
  7. if(!clientWidth) return;
  8. docEl.style.fontSize = 20 * (clientWidth / 375) + 'px';
  9. };
  10. if(!doc.addEventListener) return;
  11. win.addEventListener(resizeEvt, recalc, false);
  12. doc.addEventListener('DOMContentLoaded', recalc, false);
  13. })(document, window);
  14. </script>
  15. <style>
  16. html,body { margin:0px;}
  17. .app-message-box { width:100%; height:auto; float:left; overflow: hidden}
  18. .app-message-box ul { width:100%; height:auto; float:left;overflow: hidden; list-style:none; padding:0px 5%; margin-bottom:70px;}
  19. .app-message-box ul li { width:100%; height:50px; float:left; border-bottom:1px solid #EFEFEF;}
  20. .app-message-box ul li a {width:100%; height:50px; float:left; line-height: 50px; font-size:16px;}
  21. /* 左侧滑块删除 */
  22. .vue-list-del-data { -webkit-transition: all 0.2s;transition: 0.2s;position: relative;user-select: none;}
  23. .vue-list-del-data[ data-type="0"] { transform: translateX(0px);}
  24. .vue-list-del-data[ data-type="1"] { transform: translateX(-60px);}
  25. .vue-list-del-data div.content { background: #fff;-webkit-box-sizing: border-box;box-sizing: border-box; justify-content: flex-end;position: absolute;left: 0;right: 0;top: 0;bottom: 0;}
  26. .vue-list-del-data div.delete { width:100px;height: 50px;background: #DD4338; font-size: 15px;color: #fff;text-align: center;line-height:50px;position: absolute;top: 0;right: -90px;}
  27. </style>
  28. <div class="app-message-box">
  29. <ul id="message_list" style="margin:0px;">
  30. <li v-cloak class="vue-list-del-data" v-for="(vo,index) in list" data-type="0">
  31. <div class="content" @touchstart.capture="touchStart" @touchend.capture="touchEnd" @touchmove.capture="touchMove" @click="skip(index)">
  32. <a>{{vo.title}}</a>
  33. </div>
  34. <div class="delete" @click="deleteItem" :data-index="index">删除</div>
  35. </li>
  36. </ul>
  37. </div>
  38. <script type="text/javascript" src="./libs/vue.js"></script>
  39. <script type="text/javascript">
  40. var message_list = new Vue({
  41. el: '#message_list',
  42. data: {
  43. list: [{id:1,title:'测试1'},{id:2,title:'测试2'},{id:3,title:'测试3'},{id:4,title:'测试4'}],
  44. startX:0,
  45. startY:0,
  46. endX:0,
  47. endY:0,
  48. moveX:0,
  49. disX:0
  50. },
  51. methods:vue_methods_list_del('.vue-list-del-data',function(el,index){
  52. alert('删除');
  53. },function(el,index){
  54. alert('点击');
  55. })
  56. });
  57. function vue_methods_list_del(dom_str,click_callback,del_callback){
  58. return {
  59. skip:function(index) {
  60. if(this.checkSlide()) {
  61. this.restSlide();
  62. } else {
  63. //alert('----'+this.list[index].user_name);
  64. click_callback(this,index);
  65. }
  66. },
  67. //滑动开始
  68. touchStart:function(e) {
  69. // 记录初始位置
  70. this.startX = e.touches[0].clientX;
  71. this.startY = e.touches[0].clientY;
  72. },
  73. //滑块中
  74. touchMove:function(e){
  75. var e = e || event;
  76. var wd= 58;
  77. var parentElement = e.currentTarget.parentElement;
  78. var moveEndX = e.touches[0].clientX;
  79. var moveEndY = e.touches[0].clientY;
  80. var X = moveEndX - this.startX;
  81. var Y = moveEndY - this.startY;
  82. if(e.touches.length == 1) {
  83. this.moveX = e.touches[0].clientX
  84. this.disX = this.startX - this.moveX;
  85. //$lemon.toast('X:'+X +'||'+ 'Y:'+Y);
  86. if(this.disX < 0 || this.disX == 0) {
  87. parentElement.style.transform = "translateX(0px)";
  88. // 大于0,表示左滑了,此时滑块开始滑动
  89. }else if ( X < -90 && Y<90 && this.disX > 0) {
  90. //具体滑动距离我取的是 手指偏移距离*5。
  91. parentElement.style.transform = "translateX(-" + this.disX*5 + "px)";
  92. // 最大也只能等于删除按钮宽度
  93. if (this.disX*5 >=wd) {
  94. this.restSlide();
  95. parentElement.style.transform = "translateX(-" +wd+ "px)";
  96. parentElement.dataset.type = 1;
  97. }
  98. }else{
  99. parentElement.style.transform = "translateX(0px)";
  100. }
  101. }
  102. },
  103. //滑动结束
  104. touchEnd:function(e) {
  105. // 当前滑动的父级元素
  106. var parentElement = e.currentTarget.parentElement;
  107. // 记录结束位置
  108. this.endX = e.changedTouches[0].clientX;
  109. //$lemon.toast(parentElement.dataset.type);
  110. var e = e || event;
  111. var wd= 98;
  112. if (e.changedTouches.length == 1 && parentElement.dataset.type==1) {
  113. var endX = e.changedTouches[0].clientX;
  114. this.disX = this.startX - endX;
  115. if ((this.disX*5) < (wd/2)) {
  116. this.restSlide();
  117. parentElement.style.transform = "translateX(0px)";
  118. parentElement.dataset.type = 0;
  119. }else{
  120. //大于一半 滑动到最大值
  121. this.restSlide();
  122. parentElement.style.transform = "translateX(-" +wd+ "px)";
  123. parentElement.dataset.type = 1;
  124. }
  125. }
  126. this.startX = 0;
  127. this.startY = 0;
  128. this.endX = 0;
  129. this.endY = 0;
  130. },
  131. //判断当前是否有滑块处于滑动状态
  132. checkSlide:function() {
  133. var listItems = document.querySelectorAll(dom_str);
  134. for(var i = 0; i < listItems.length; i++) {
  135. if(listItems[i].dataset.type == 1) {
  136. return true;
  137. }
  138. }
  139. return false;
  140. },
  141. //复位滑动状态
  142. restSlide:function() {
  143. var listItems = document.querySelectorAll(dom_str);
  144. // 复位
  145. for(var i = 0; i < listItems.length; i++) {
  146. listItems[i].dataset.type = 0;
  147. listItems[i].style.transform = "translateX(0px)";
  148. }
  149. },
  150. //删除
  151. deleteItem:function(e) {
  152. // 当前索引
  153. var index = e.currentTarget.dataset.index;
  154. del_callback(this,index);
  155. // 复位
  156. this.restSlide();
  157. // 删除
  158. this.list.splice(index, 1);
  159. }
  160. };
  161. };
  162. </script>

特别说明

  1. 使用本模块注意会与同vue.js模块冲突,请开发者谨慎选择,本模块为1.0版本,后续会陆续更新。``