vue移动端实现手指滑动效果

vue移动端实现手指滑动效果
在这里插入图片描述
滑动时候黄色块宽度跟着变化
通过touch点击实现
目前感觉宽度变化有点问题,还在思考中
下面上代码:

<template lang="html">
  <div class="back" ref="back" @touchstart.prevent="touchStart" @touchmove.prevent="touchMove">
    <div class="back-r" 
	@touchstart.prevent="touchStart" @touchmove.prevent="touchMove"
	@touchend="touchEnd"
	ref="right"></div>
  </div>
</template>
 
<script>
export default {
  data() {
  },
  created() {
    this.touch = {}
  },
  methods: {
    touchStart(e) {
      const touch = e.touches[0]
	  
	 //点击时的位子
      this.touch.startX = touch.pageX
      this.touch.startY = touch.pageY
	  console.log('----',this.$refs.right.clientWidth)
    },
//开始点击
    touchMove(e) {
      console.log("move");
      const touch = e.touches[0]
	  //滑动位置和初始位置差
      const deltaX = touch.pageX - this.touch.startX
	  console.log(deltaX)
      const deltaY = touch.pageY - this.touch.startY;
      //打印right宽度
	  console.log('----',this.$refs.right.clientWidth+'px')	
	  const rwidth = this.$refs.right.clientWidth
	  //改变right的宽度
	  this.$refs.right.style.width = (this.$refs.right.clientWidth + Math.floor(deltaX/50)) +'px'
	  //进行判断,当宽度小于0或者大于400
	  if (rwidth<0) {
	  	this.$refs.right.style.width = 0
	  } else if(rwidth>400){
		  this.$refs.right.style.width = 400+'px'
	  } 
	  console.log('----',this.$refs.right.clientWidth+'px')

    },
    touchEnd() {
      console.log("end");
      // console.log(this.percent);
    }
  }
}
</script>
 
<style scoped lang="stylus" rel="stylesheet/stylus">
body{
  margin:0;
  padding: 0;
  overflow: hidden;
  touch-action:none;
}
.back{
	width: 100%
	height: 100px
	border 10px solid #0000FF
	overflow: hidden;
}
  
  
  .back-r{
	  // display: inline-block
	  // vertical-align: top
	  position: relative
	  width: 400px
	  height: 100%
	  overflow: hidden;
	  background-color: yellow
  }
    
</style>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值