vue实现 移动端touch--点击、双指与拖拽指令

前言

用vue做移动端开发过程中,需要手势操作。因为vue-touch目前不支持vue2.0,所以自己写了几个手势。

实现功能

  • 点击
  • 双指缩放图片
  • 移动

指令代码

myTouch.js >>
export default(Vue) => {
    Vue.directive('touch', {
        bind: function (el, binding, vnode) {
            let type = binding.arg; // 传入点击的类型
            let coordinate = {} // 记录坐标点的对象
            let timeOutTap;
            let timeOutLong;
            let scaleSize; // 缩放尺寸
            let displacement = {}; //移动的位移
            // 勾股定理计算距离
            function getDistance(bg, end){
                return Math.sqrt(Math.pow((end.x - bg.x),2 ) + Math.pow((end.y - bg.y),2 ));
            }
            // 点击开始的时候
            el.addEventListener('touchstart', function(e){
                // 获取第一个手指点击的坐标
                let x = e.touches[0].pageX;
                let y = e.touches[0].pageY; 
                // 如果点击的时间很长,那么点击的类型就是长按 --- longTouch
                timeOutLong = setTimeout(() => {
                    timeOutLong = 0;
                    if(type === 'longTouch'){
                         binding.value.func(binding.value.param)
                    }
                }, 2000)
                timeOutTap = setTimeout(() => {
                    timeOutTap = 0;
                    if(type === 'tap' && e.touches.length === 1){
                         binding.value.func(binding.value.param)
                    }
                }, 200)
                // 如果是两个手指,而且类型是缩放 --- scaleTocuh
                if(e.touches.length > 1 && type === 'scaleTouch'){
                    // 记录双指的间距长度
                    coordinate.start =  getDistance({
                        x: e.touches[0].pageX, 
                        y: e.touches[0].pageY,
                    },{
                        x: e.touches[1].pageX, 
                        y: e.touches[1].pageY,
                    })

                }
                // 如果是移动的话,还要记录下来最开始的位置,只能一个手指位移
                if(type === 'slideTouch' && e.touches.length == 1){
                    // debugger
                    displacement.start = {
                        x: e.touches[0].pageX,
                        y: e.touches[0].pageY,
                    }
                }
            }, false)
            el.addEventListener('touchmove', function(e){
                clearTimeout(timeOutTap)
                clearTimeout(timeOutLong)
                timeOutTap = 0; timeOutLong = 0;
                // 如果是缩放类型
                if(type == 'scaleTouch' && e.touches.length === 2){
                    // 计算移动过程中的两个手指的距离
                    coordinate.stop = getDistance({
                        x: e.touches[0].pageX, 
                        y: e.touches[0].pageY,
                    },{
                        x: e.touches[1].pageX, 
                        y: e.touches[1].pageY,
                    })
                    // 设置缩放尺寸
                    scaleSize = (coordinate.stop / coordinate.start)  - 1;
                    // 这里设置图片不能无限放大与缩小
                    // 这里设置放大缩小速度慢一点,所以 /4一下
                    binding.value.func(scaleSize / 2, false)
                }
                // 如果是移动类型
                if(type == 'slideTouch' && e.touches.length === 1 ){
                    displacement.end = {
                        x: e.changedTouches[0].pageX,
                        y: e.changedTouches[0].pageY,
                    }
                    binding.value.func({x:displacement.end.x - displacement.start.x, y: displacement.end.y - displacement.start.y, is_endMove : false})
                }
            }, false)
            el.addEventListener('touchend', function(e){
                if(type === 'scaleTouch'){
                    binding.value.func(0, true)
                }
                if(type === 'slideTouch'){
                    binding.value.func({x:0, y: 0, is_endMove : true})
                }
            }, false)
        }
    })
}

使用方法

html:
 <img :src="picUrl" class="modal_content"
            v-touch:scaleTouch = "{func: scalePic, param: ''}"
            v-touch:slideTouch = "{func: movePic, param: ''}"
/>

vue:
main.js >>
import myTouch from './myTouch'
myTouch(Vue)

app.vue >>
     data:function () {
      return{
          baseLeft : 0,
          baseTop: 0,
          bodyWidth: document.body.clientWidth,
          ele: null, // 不能在这里设置, dom还没有生成
      }
  },

放大功能实现

scalePic: function(param, is_endScale){
      this.ele =document.getElementsByClassName('modal_content')[0]; // 这个应该在图片显示的时候设置
      let nodeStyle = this.ele.style.transform;
      let changeSize = nodeStyle ?  parseFloat(nodeStyle.slice(nodeStyle.indexOf('scale')+6)) : 0;
      if(is_endScale){
        // 缩放图片结束,要重新计算定位
        this.setMaxdisp(changeSize,parseInt(this.ele.style.marginLeft), parseInt(this.ele.style.marginTop), 'scale')
        return 
      }
      if(nodeStyle){
        // 如果存在的话,就说明已经设置了,scale已经改变了
        let currScaleSize = changeSize + param; 
        currScaleSize > 3 ? currScaleSize = 3 : null
        currScaleSize < 1 ? currScaleSize = 1 : null
        this.ele.style.transform = 'translate(-50%, -50%) scale('+currScaleSize+','+currScaleSize+')';
      }else{ // 如果不存在,就说明是第一次设置
          let scale = param + 1 
          this.ele.style.marginLeft =  '0px';
          this.ele.style.marginTop  = '0px';
          this.ele.style.transform = 'translate(-50%, -50%) scale('+scale+','+scale+')';
      }
    }

移动功能

movePic: function(param){
     if(param.is_endMove){ // 每次移动松开手指的时候要下次移动的基础坐标
        this.baseLeft = parseInt(this.ele.style.marginLeft.slice(0, -2));
        this.baseTop = parseInt(this.ele.style.marginTop.slice(0, -2));
      }else{
        let nodeStyle = this.ele.style.transform 
        if(nodeStyle){ // 有这个就表示应该是移动
          let currScaleSize = parseFloat(nodeStyle.slice(nodeStyle.indexOf('scale')+6))
          this.setMaxdisp(currScaleSize,this.baseLeft+ param.x, this.baseTop + param.y, 'move')
        }
      }
    }

计算最大位移

setMaxdisp:function(changeSize, changeX, changeY, type){
      // 计算最大位移
      // naturalWidth : 是图片原始的宽度,通过比例可以计算出当前图片在页面的高度
      let picHeight =  this.bodyWidth  / (this.ele.naturalWidth / this.ele.naturalHeight); 
      let maxTop = ( picHeight * changeSize - window.innerHeight) /2 
      let maxLeft = this.bodyWidth / 2 * (changeSize - 1) 
      if(changeX >= maxLeft){
        this.ele.style.marginLeft = maxLeft + 'px'
      }else if(changeX < -maxLeft){
        this.ele.style.marginLeft = -maxLeft + 'px'
      }else if(type==='move'){
        this.ele.style.marginLeft =changeX +'px'; 
      }
      // 如果图片当前尺寸大于屏幕尺寸,可以移动
      if(maxTop > 0){
        if(changeY >= maxTop){
          this.ele.style.marginTop = maxTop + 'px';
        }else if(changeY < -maxTop){
          this.ele.style.marginTop = -maxTop + 'px'
        }else if(type==='move'){
          this.ele.style.marginTop = changeY+'px';
        }
      }else if(type==='move'){
        this.ele.style.marginTop = 0 +'px'; 
      }
    }

遇到的问题

双指与单指容易混淆

在双指放大缩小的时候,两个手指松开时间不同,页面就会在两个手指松开的时候判断为点击事件。 这个还没有解决

计算图片位移程度

图片不能随意的位移。应该是在一定范围内去位移的,所以写了一个计算最大位移的函数,图片位移距离应该是图片刚好能看到的程度。通过图片原始的宽高,以及页面当前的宽度比例可以计算出当前图片在页面的高度。然后计算出最大位移

每次移动的基础位置

图片移动或者缩小懂应该是在一个基础数据上去增加减少,如果没有设置这个基础数据,会导致每次都是从1的比例来移动以及缩小,所以每次移动以及放大缩小图片之后,都要记录下来改变之后图片的参数,下次改变图片的时候都是在这个参数基础上去改变的。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一种流行的JavaScript框架,用于构建现代Web应用程序,如移动应用程序和单页应用程序。使用Vue.js开发移动应用程序时,经常需要使用各种UI组件。其中,el-table是一个常用的表格组件,可以方便地展示数据。 移动端的el-table组件需要考虑到手机屏幕的大小和触摸操作的交互设计。因此,我们需要手写实现一套适用于移动端的el-table组件。 首先,我们需要设计一个表格结构,包括表头、表体和表尾。在移动端,由于屏幕较小,通常需要使用滚动条来滚动表体。因此,我们需要给表格设置固定高度,并设置overflow: auto属性。 其次,我们需要实现表格的数据绑定和分页功能。通常情况下,移动端的数据量较小,因此我们可以一次性将所有数据加载到前端进行分页展示。我们可以使用分页器组件来实现分页,并通过计算分页数据来动态更新表格数据。 最后,我们需要考虑到表格的交互设计。在移动端,我们通常使用左右滑动来进行某些操作。例如,我们可以使用左滑删除删除某一行数据,右滑编辑某一行数据。此外,我们还可以添加筛选功能和搜索功能,方便用户快速查找需要的数据。 总之,Vue.js手写实现移动端el-table组件需要考虑到表格结构、数据绑定、分页功能和交互设计等方面。通过仔细设计和实现,我们可以创建出易用性、高效性和美观性的el-table组件,满足用户需要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值