Vue 中 使用touchstart ,touchmove,touchend事件

  1. <template>
  2. <div id="SlideBar" class="box">
  3. <div class="item" ref="slide" :style="slideStyle" @touchstart="start($event)" @touchmove="move($event)" @touchend="end($event)">
  4. <img src="../../../static/lun.1.jpg" alt="">
  5. <div class="right">
  6. </div>
  7. </div>
  8. <div class="btn" ref="btn">
  9. <button>编辑</button>
  10. <button style="background:#387ef5;color:#fff">收藏</button>
  11. </div>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'SlideBar',
  17. props: {
  18. },
  19. data (){
  20. return {
  21. flag: false,
  22. startX: 0,
  23. endX: 0,
  24. slideStyle: {
  25. left: 0,
  26. transition: 'none'
  27. }
  28. }
  29. },
  30. methods: {
  31. start (e){ //记录开始滑动屏幕的X轴的位置
  32. this.flag = true;
  33. this.startX = e.touches[0].clientX;
  34. this.endX = this.$refs.slide.offsetLeft;
  35. this.slideStyle.transition = 'none';
  36. },
  37. move (e){
  38. if(this.flag){
  39. // 处理鼠标移动的逻辑
  40. var moveX = this.endX + (e.touches[0].clientX - this.startX); //计算滑动的距离
  41. if(Math.abs(moveX) >= this.$refs.btn.offsetWidth && moveX < 0){ //判断滑动的距离是否大于class:btn的宽度
  42. moveX = (Math.abs(moveX) - this.$refs.btn.offsetWidth) * 0.1; // 0.3阻力系数
  43. this.slideStyle.left = - this.$refs.btn.offsetWidth - moveX + 'px';
  44. }else if(moveX >= 0){ //滑动距离是否大于等于0
  45. this.slideStyle.left = 0 + 'px'; //大于等于0让class:item等于0
  46. }else{
  47. this.slideStyle.left = moveX + 'px'; //小于0让class:item等于滑动的距离
  48. }
  49. }
  50. },
  51. end (e){
  52. if(this.flag){
  53. this.flag = false;
  54. // endX = slide.offsetLeft;
  55. var moveX = e.changedTouches[0].clientX - this.startX; //计算滑动的距离
  56. this.slideStyle.transition = 'left .3s';
  57. var btnWidth = this.$refs.btn.offsetWidth; //class:btn的宽度
  58. if(moveX < 0){
  59. if(Math.abs(moveX) >= btnWidth / 2 || Math.abs(this.$refs.slide.offsetLeft) >= this.$refs.btn.offsetWidth){ //是否大于class:btn宽度的一半
  60. this.slideStyle.left = - btnWidth + 'px'; //左滑超过class:btn宽度的一半就滑回去
  61. }else if(Math.abs(moveX) < btnWidth / 2){ //小于class:btn宽度的一半
  62. this.slideStyle.left = 0 + 'px'; //左滑没有超过class:btn宽度的一半回原位
  63. }
  64. }else if(moveX > 0 && this.endX != 0){
  65. if(Math.abs(moveX) >= btnWidth / 2){
  66. this.slideStyle.left = 0 + 'px'; //右滑超过class:btn宽度的一半就滑回去
  67. }else if(Math.abs(moveX) < btnWidth / 2){
  68. this.slideStyle.left = - btnWidth + 'px'; //右滑没有超过class:btn宽度的一半回原位
  69. }
  70. }
  71. }
  72. }
  73. },
  74. mounted (){
  75. var _this = this;
  76. // 使用js的现代事件监听transition过渡结束
  77. this.$refs.slide.addEventListener('transitionend',function(){
  78. _this.endX = this.offsetLeft;
  79. })
  80. }
  81. }
  82. </script>
  83. <style>
  84. .box{
  85. position:relative;
  86. border-bottom:0.026667rem solid #666666;
  87. }
  88. .btn{
  89. height:100%;
  90. position:absolute;
  91. right:0;
  92. top:0;
  93. background:red;
  94. display:flex;
  95. }
  96. button{
  97. width:1.6rem;
  98. height:100%;
  99. background:#f8f8f8;
  100. border:none;
  101. }
  102. .item{
  103. padding:0.266667rem;
  104. display:flex;
  105. position:relative;
  106. background:#fff;
  107. z-index: 2;
  108. box-shadow: 0.026667rem 0 0.053333rem #ddd;
  109. }
  110. .item img{
  111. width:2.133333rem;
  112. height:2.133333rem;
  113. margin-right:0.4rem;
  114. border-radius: 0.133333rem;
  115. }
  116. .item .title{
  117. font-size:0.48rem;
  118. float: left;
  119. }
  120. .item .text{
  121. font-size:0.426667rem;
  122. color:#888;
  123. float: left;
  124. margin: 0 1.33rem;
  125. }
  126. .item .price{
  127. color:#888;
  128. float: left;
  129. margin: 0 1.33rem;
  130. }
  131. </style>

效果图:
在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,67人围观)

还没有评论,来说两句吧...

相关阅读