本文转载自blogcss
转载请注明出处html
移动端,滑动是很常见的需求。不少同窗都用过swiper.js,本文从原理出发,实践出一个类swiper的滑动小插件ice-skating。git
小插件的例子:github
在写代码的过程当中产生的一些思考:web
滑动就是用transform: translate(x,y)
或者transform: translate3d(x,y,z)
去控制元素的移动,在松手的时候断定元素最后的位置,元素的样式应用transform: translate3d(endx , endy, 0)
和transition-duration: time
来达到一个动画恢复的效果。标准浏览器提供transitionend
事件监听动画结束,在结束时将动画时间归零。算法
Note: 这里不讨论非标准浏览器的实现,对于不支持transform
和transition
的浏览器,可使用position: absolute
配合left
和top
进行移动,而后用基于时间的动画的算法来模拟动画效果。浏览器
举例一个基本的结构:app
//example <div class="ice-container"> <div class="ice-wrapper" id="myIceId"> <div class="ice-slide">Slide 1</div> <div class="ice-slide">Slide 2</div> <div class="ice-slide">Slide 3</div> </div> </div>
transform: translate3d(x,y,z)
就是应用在className为ice-slide
的元素上。这里不展现css代码,能够在ice-skating的example
文件中里查看完整的css。css代码并非惟一的,简单说只要实现下图的结构就能够。框架
从图中能够直观的看出,移动的是绿色的元素。className为ice-slide
的元素的宽乘于当前索引(offsetWidth * index),就是每次稳定时的偏移量。例如最开始transform: translate3d(offsetWidth * 0, 0, 0)
,切换到slide2后,transform: translate3d(offsetWidth * 1, 0, 0)
,大体就是这样的过程。ide
源码位于ice-skating的dist/iceSkating.js
。我给插件起名叫ice-skating
,但愿它像在冰面同样顺畅^_^
之前咱们会将代码包裹在一个简单的匿名函数里,如今须要加一些额外的代码来兼容各类模块标准。
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (factory((global))); }(this, (function (exports) { 'use strict'; })));
用两个对象来存储信息
var mainStore = Object.create(null); var state = Object.create(null);
Object.create(null)
建立的对象不会带有Object.prototype
上的方法,由于咱们不须要它们,例如toString
、valueOf
、hasOwnProperty
之类的。
function iceSkating(option){ if (!(this instanceof iceSkating)) return new iceSkating(option); } iceSkating.prototype = { }
if (!(this instanceof iceSkating)) return new iceSkating(option);
不少库和框架都有这句,简单说就是不用new生成也能够生成实例。
对于触摸事件,在移动端,咱们会用touchEvent
,在pc端,咱们则用mouseEvent
。因此咱们须要检测支持什么事件。
iceSkating.prototype = { support: { touch: (function(){ return !!(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch); })() }
支持touch则认为是移动端,不然为pc端
var events = ic.support.touch ? ['touchstart', 'touchmove', 'touchend']:['mousedown','mousemove','mouseup'];
pc端和移动端这3个函数是通用的。
var touchStart = function(e){}; var touchMove = function(e){}; var touchEnd = function(e){};
var ic = this; var initEvent = function(){ var events = ic.support.touch ? ['touchstart', 'touchmove', 'touchend']: ['mousedown','mousemove','mouseup']; var transitionEndEvents = ['webkitTransitionEnd', 'transitionend', 'oTransitionEnd', 'MSTransitionEnd', 'msTransitionEnd']; for (var i = 0; i < transitionEndEvents.length; i++) { ic.addEvent(container, transitionEndEvents[i], transitionDurationEndFn, false); } ic.addEvent(container, events[0], touchStart, false); //默认阻止容器元素的click事件 if(ic.store.preventClicks) ic.addEvent(container, 'click', ic.preventClicks, false); if(!isInit){ ic.addEvent(document, events[1], touchMove, false); ic.addEvent(document, events[2], touchEnd, false); isInit = true; } };
touchStart
和transitionDurationEndFn
函数每一个实例的容器都会绑定,可是全部实例共用touchMove
和touchEnd
函数,它们只绑定在document
,而且只会绑定一次。使用事件委托有两个好处:
touchMove
和touchEnd
也绑定在容器元素上,当鼠标移出容器元素时,咱们会“失去控制”。在document
上意味着能够“掌控全局”。不会把封装的函数的代码都一一列出来,但会说明它的做用。
会在触碰的第一时间调用,基本都在初始化state的信息
var touchStart = function(e){ //mouse事件会提供which值, e.which为3时表示按下鼠标右键,鼠标右键会触发mouseup,但右键不容许移动滑块 if (!ic.support.touch && 'which' in e && e.which === 3) return; //获取起始坐标。TouchEvent使用e.targetTouches[0].pageX,MouseEvent使用e.pageX。 state.startX = e.type === 'touchstart' ? e.targetTouches[0].pageX : e.pageX; state.startY = e.type === 'touchstart' ? e.targetTouches[0].pageY : e.pageY; //时间戳 state.startTime = e.timeStamp; //绑定事件的元素 state.currentTarget = e.currentTarget; state.id = e.currentTarget.id; //触发事件的元素 state.target = e.target; //获取当前滑块的参数信息 state.currStore = mainStore[e.currentTarget.id]; //state的touchStart 、touchMove、touchEnd表明是否进入该函数 state.touchEnd = state.touchMove = false; state.touchStart = true; //表示滑块移动的距离 state.diffX = state.diffY = 0; //动画运行时的坐标与动画运行前的坐标差值 state.animatingX = state.animatingY = 0; };
在移动滑块时,可能滑块正在动画中,这是须要考虑一种特殊状况。滑块的移动应该依据如今的位置计算。
如何知道动画运行中的信息呢,可使用window.getComputedStyle(element, [pseudoElt])
,它返回的样式是一个实时的CSSStyleDeclaration
对象。用它取transform
的值会返回一个 2D 变换矩阵,像这样matrix(1, 0, 0, 1, -414.001, 0)
,最后两位就是x,y值。
简单封装一下,就能够取得当前动画translate的x,y值了。
var getTranslate = function(el){ var curStyle = window.getComputedStyle(el); var curTransform = curStyle.transform || curStyle.webkitTransform; var x,y; x = y = 0; curTransform = curTransform.split(', '); if (curTransform.length === 6) { x = parseInt(curTransform[4], 10); y = parseInt(curTransform[5], 10); } return {'x': x,'y': y}; };
移动时会持续调用,若是只是点击操做,不会触发touchMove。
var touchMove = function(e){ // 1. 若是当前触发touchMove的元素和触发touchStart的元素不一致,不容许滑动。 // 2. 执行touchMove时,需保证touchStart已执行,且touchEnd未执行。 if(e.target !== state.target || state.touchEnd || !state.touchStart) return; state.touchMove = true; //取得当前坐标 var currentX = e.type === 'touchmove' ? e.targetTouches[0].pageX : e.pageX; var currentY = e.type === 'touchmove' ? e.targetTouches[0].pageY : e.pageY; var currStore = state.currStore; //触摸时若是动画正在运行 if(currStore.animating){ // 取得当前元素translate的信息 var animationTranslate = getTranslate(state.currentTarget); //计算动画的偏移量,currStore.translateX和currStore.translateY表示的是滑块最近一次稳定时的translate值 state.animatingX = animationTranslate.x - currStore.translateX; state.animatingY = animationTranslate.y - currStore.translateY; currStore.animating = false; //移除动画时间 removeTransitionDuration(currStore.container); } //若是轮播进行中,将定时器清除 if(currStore.autoPlayID !== null){ clearTimeout(currStore.autoPlayID); currStore.autoPlayID = null; } //判断移动方向是水平仍是垂直 if(currStore.direction === 'x'){ //currStore.touchRatio是移动系数 state.diffX = Math.round((currentX - state.startX) * currStore.touchRatio); //移动元素 translate(currStore.container, state.animatingX + state.diffX + state.currStore.translateX, 0, 0); }else{ state.diffY = Math.round((currentY - state.startY) * state.currStore.touchRatio); translate(currStore.container, 0, state.animatingY + state.diffY + state.currStore.translateY, 0); } };
若是支持translate3d,会优先使用它,translate3d会提供硬件加速。有兴趣能够看看这篇blog两张图解释CSS动画的性能
var translate = function(ele, x, y, z){ if (ic.support.transforms3d){ transform(ele, 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)'); } else { transform(ele, 'translate(' + x + 'px, ' + y + 'px)'); } };
在触摸结束时调用。
var touchEnd = function(e){ state.touchEnd = true; if(!state.touchStart) return; var fastClick ; var currStore = state.currStore; //若是整个触摸过程时间小于fastClickTime,会认为这次操做是点击。但默认是屏蔽了容器的click事件的,因此提供一个clickCallback参数,会在点击操做时调用。 if(fastClick = (e.timeStamp - state.startTime) < currStore.fastClickTime && !state.touchMove && typeof currStore.clickCallback === 'function'){ currStore.clickCallback(); } if(!state.touchMove) return; //若是移动距离没达到切换页的临界值,则让它恢复到最近的一次稳定状态 if(fastClick || (Math.abs(state.diffX) < currStore.limitDisX && Math.abs(state.diffY) < currStore.limitDisY)){ //在transitionend事件绑定的函数中断定是否重启轮播,可是若是transform先后两次的值同样时,不会触发transitionend事件,因此在这里断定是否重启轮播 if(state.diffX === 0 && state.diffY === 0 && currStore.autoPlay) autoPlay(currStore); //恢复到最近的一次稳定状态 recover(currStore, currStore.translateX, currStore.translateY, 0); }else{ //位移知足切换 if(state.diffX > 0 || state.diffY > 0) { //切换到上一个滑块 moveTo(currStore, currStore.index - 1); }else{ //切换到下一个滑块 moveTo(currStore, currStore.index + 1); } } };
动画执行完成后调用
var transitionDurationEndFn = function(){ //将动画状态设置为false ic.store.animating = false; //执行自定义的iceEndCallBack函数 if(typeof ic.store.iceEndCallBack === 'function') ic.store.iceEndCallBack(); //将动画时间归零 transitionDuration(container, 0); //清空state if(ic.store.id === state.id) state = Object.create(null); };
至此,一个完整的滑动过程结束。
第一时间想到的是使用setInterval
或者递归setTimeout
实现轮播,但这样作并不优雅。
事件循环(EventLoop)中setTimeout
或setInterval
会放入macrotask
队列中,里面的函数会放入microtask
,当这个macrotask
执行结束后全部可用的 microtask
将会在同一个事件循环中执行。
咱们极端的假设setInterval
设定为200ms,动画时间设为1000ms。每隔200ms, macrotask
队列中就会插入setInterval
,但咱们的动画此时没有完成,因此用setInterval
或者递归setTimeout
的轮播在这种状况下是有问题的。
最佳思路是在每次动画结束后再将轮播开启。
动画结束执行的函数: var transitionDurationEndFn = function(){ ... //检测是否开启轮播 if(ic.store.autoPlay) autoPlay(ic.store); };
轮播函数也至关简单
var autoPlay = function(store){ store.autoPlayID = setTimeout(function(){ //当前滑块的索引 var index = store.index; ++index; //到最后一个了,重置为0 if(index === store.childLength){ index = 0; } //移动 moveTo(store, index); },store.autoplayDelay); };
本文记录了我思考的过程,代码应该还有不少地方值得完善。