touch拖动事件缓冲及光滑效果 原 发表于 2017-11-28 | 分类于 前端 , 代码块 | 浏览 次 html1234<div class="container"> <div class="wrapper"> </div></div> main123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126/** * options * 1. container */let config = { parent:'.container', ele:'.wrapper', scrollX:true, scrollY:true}class touchmove{ constructor(options){ this.options = $.extend({},true,config,options) this.target = document.querySelector(this.options.ele) this.init() }}touchmove.prototype = { init(){ var _this = this, // 开始的偏移 startY = 0, startX = 0, // 保存的偏移 translateY = 0, translateX = 0, // 手指坐标 startFingerX = 0, startFingerY = 0, maxHeight = this.target.offsetHeight - (document.documentElement.offsetHeight - this.target.offsetTop), maxWidth = this.target.scrollWidth - (document.querySelector(this.options.parent).scrollWidth), lastTime = 0; //按下的时间 _this.target.addEventListener('touchstart',function (e) { e.stopPropagation(); if(_this.options.scrollY){ startFingerY = e.changedTouches[0].clientY; _this.target.style.WebkitTransition = _this.target.style.transition = ''; startY = translateY; } if(_this.options.scrollX){ startFingerX = e.changedTouches[0].clientX; _this.target.style.WebkitTransition = _this.target.style.transition = ''; startX = translateX; } lastTime = Date.now(); // console.log(lastTime); }); _this.target.addEventListener('touchmove',function (e) { e.stopPropagation(); if(_this.options.scrollY){ var nowFinger = e.changedTouches[0].clientY, distance = nowFinger - startFingerY; translateY = distance + startY; // 弹性 if(translateY > 0 ){ //拖动系数. 拉力的感觉 translateY *= 0.4; }else if( translateY < -maxHeight){ //最后. translateY = distance*0.4 + startY; } } if(_this.options.scrollX){ var nowFinger = e.changedTouches[0].clientX, distance = nowFinger - startFingerX; translateX = distance + startX; // 弹性 if(translateX > 0 ){ //拖动系数. 拉力的感觉 translateX *= 0.4; }else if( translateX < -maxWidth){ //最后. translateX = distance*0.4 + startX; } } _this.shift(translateX,translateY); e.preventDefault() }) _this.target.addEventListener('touchend',function (e) { /* 惯性原理: 产生的速度 = 移动距离 / 移动时间 距离 = 松开的坐标 - 上次的坐标 (距离差) 时间 = 松开的时间 - 按下的时间 (时间差) */ if(_this.options.scrollY){ var nowFinger = e.changedTouches[0].clientY, distance = nowFinger - startFingerY, //距离差 timeDis = Date.now() - lastTime, //时间差 speed = (distance / timeDis)*100; // console.log(speed); //很小 *倍数 // 惯性 translateY += speed; // 边界 ,弹回去 if(translateY > 0){ translateY = 0; }else if(translateY < -maxHeight){ translateY = -maxHeight; } } if(_this.options.scrollX){ var nowFinger = e.changedTouches[0].clientX, distance = nowFinger - startFingerX, //距离差 timeDis = Date.now() - lastTime, //时间差 speed = (distance / timeDis)*100; // console.log(speed); //很小 *倍数 // 惯性 translateX += speed; // 边界 ,弹回去 if(translateX > 0){ translateX = 0; }else if(translateX < -maxWidth){ translateX = -maxWidth; } } // 添加贝塞尔曲线 _this.target.style.WebkitTransition = _this.target.style.transition = 'transform 500ms cubic-bezier(0.1, 0.57, 0.1, 1)'; _this.shift(translateX,translateY); }) }, shift(x,y){ this.target.style.WebkitTransform = this.target.style.transform = 'translate('+x+'px,'+y+'px)'; }}