swipe.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Swipe 2.0
  3. *
  4. * Brad Birdsall
  5. * Copyright 2013, MIT License
  6. *
  7. */
  8. function Swipe(container, options) {
  9. "use strict";
  10. // utilities
  11. var noop = function() {}; // simple no operation function
  12. var offloadFn = function(fn) { setTimeout(fn || noop, 0) }; // offload a functions execution
  13. // check browser capabilities
  14. var browser = {
  15. addEventListener: !!window.addEventListener,
  16. touch: ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
  17. transitions: (function(temp) {
  18. var props = ['transitionProperty', 'WebkitTransition', 'MozTransition', 'OTransition', 'msTransition'];
  19. for ( var i in props ) if (temp.style[ props[i] ] !== undefined) return true;
  20. return false;
  21. })(document.createElement('swipe'))
  22. };
  23. // quit if no root element
  24. if (!container) return;
  25. var element = container.children[0];
  26. var slides, slidePos, width, length;
  27. options = options || {};
  28. var index = parseInt(options.startSlide, 10) || 0;
  29. var speed = options.speed || 300;
  30. options.continuous = options.continuous !== undefined ? options.continuous : true;
  31. function setup() {
  32. // cache slides
  33. slides = element.children;
  34. length = slides.length;
  35. // set continuous to false if only one slide
  36. if (slides.length < 2) options.continuous = false;
  37. //special case if two slides
  38. if (browser.transitions && options.continuous && slides.length < 3) {
  39. element.appendChild(slides[0].cloneNode(true));
  40. element.appendChild(element.children[1].cloneNode(true));
  41. slides = element.children;
  42. }
  43. // create an array to store current positions of each slide
  44. slidePos = new Array(slides.length);
  45. // determine width of each slide
  46. width = container.getBoundingClientRect().width || container.offsetWidth;
  47. element.style.width = (slides.length * width) + 'px';
  48. // stack elements
  49. var pos = slides.length;
  50. while(pos--) {
  51. var slide = slides[pos];
  52. slide.style.width = width + 'px';
  53. slide.setAttribute('data-index', pos);
  54. if (browser.transitions) {
  55. slide.style.left = (pos * -width) + 'px';
  56. move(pos, index > pos ? -width : (index < pos ? width : 0), 0);
  57. }
  58. }
  59. // reposition elements before and after index
  60. if (options.continuous && browser.transitions) {
  61. move(circle(index-1), -width, 0);
  62. move(circle(index+1), width, 0);
  63. }
  64. if (!browser.transitions) element.style.left = (index * -width) + 'px';
  65. container.style.visibility = 'visible';
  66. }
  67. function prev() {
  68. if (options.continuous) slide(index-1);
  69. else if (index) slide(index-1);
  70. }
  71. function next() {
  72. if (options.continuous) slide(index+1);
  73. else if (index < slides.length - 1) slide(index+1);
  74. }
  75. function circle(index) {
  76. // a simple positive modulo using slides.length
  77. return (slides.length + (index % slides.length)) % slides.length;
  78. }
  79. function slide(to, slideSpeed) {
  80. // do nothing if already on requested slide
  81. if (index == to) return;
  82. if (browser.transitions) {
  83. var direction = Math.abs(index-to) / (index-to); // 1: backward, -1: forward
  84. // get the actual position of the slide
  85. if (options.continuous) {
  86. var natural_direction = direction;
  87. direction = -slidePos[circle(to)] / width;
  88. // if going forward but to < index, use to = slides.length + to
  89. // if going backward but to > index, use to = -slides.length + to
  90. if (direction !== natural_direction) to = -direction * slides.length + to;
  91. }
  92. var diff = Math.abs(index-to) - 1;
  93. // move all the slides between index and to in the right direction
  94. while (diff--) move( circle((to > index ? to : index) - diff - 1), width * direction, 0);
  95. to = circle(to);
  96. move(index, width * direction, slideSpeed || speed);
  97. move(to, 0, slideSpeed || speed);
  98. if (options.continuous) move(circle(to - direction), -(width * direction), 0); // we need to get the next in place
  99. } else {
  100. to = circle(to);
  101. animate(index * -width, to * -width, slideSpeed || speed);
  102. //no fallback for a circular continuous if the browser does not accept transitions
  103. }
  104. index = to;
  105. offloadFn(options.callback && options.callback((length == 2 && index > 1 ? index - 2 : index), slides[index]));
  106. }
  107. function move(index, dist, speed) {
  108. translate(index, dist, speed);
  109. slidePos[index] = dist;
  110. }
  111. function translate(index, dist, speed) {
  112. var slide = slides[index];
  113. var style = slide && slide.style;
  114. if (!style) return;
  115. style.webkitTransitionDuration =
  116. style.MozTransitionDuration =
  117. style.msTransitionDuration =
  118. style.OTransitionDuration =
  119. style.transitionDuration = speed + 'ms';
  120. style.webkitTransform = 'translate(' + dist + 'px,0)' + 'translateZ(0)';
  121. style.msTransform =
  122. style.MozTransform =
  123. style.OTransform = 'translateX(' + dist + 'px)';
  124. }
  125. function animate(from, to, speed) {
  126. // if not an animation, just reposition
  127. if (!speed) {
  128. element.style.left = to + 'px';
  129. return;
  130. }
  131. var start = +new Date;
  132. var timer = setInterval(function() {
  133. var timeElap = +new Date - start;
  134. if (timeElap > speed) {
  135. element.style.left = to + 'px';
  136. if (delay) begin();
  137. options.transitionEnd && options.transitionEnd.call(event, (length == 2 && index > 1 ? index - 2 : index), slides[index]);
  138. clearInterval(timer);
  139. return;
  140. }
  141. element.style.left = (( (to - from) * (Math.floor((timeElap / speed) * 100) / 100) ) + from) + 'px';
  142. }, 4);
  143. }
  144. // setup auto slideshow
  145. var delay = options.auto || 0;
  146. var interval;
  147. function begin() {
  148. interval = setTimeout(next, delay);
  149. }
  150. function stop() {
  151. delay = 0;
  152. clearTimeout(interval);
  153. }
  154. // setup initial vars
  155. var start = {};
  156. var delta = {};
  157. var isScrolling;
  158. // setup event capturing
  159. var events = {
  160. handleEvent: function(event) {
  161. switch (event.type) {
  162. case 'touchstart': this.start(event); break;
  163. case 'touchmove': this.move(event); break;
  164. case 'touchend': offloadFn(this.end(event)); break;
  165. case 'webkitTransitionEnd':
  166. case 'msTransitionEnd':
  167. case 'oTransitionEnd':
  168. case 'otransitionend':
  169. case 'transitionend': offloadFn(this.transitionEnd(event)); break;
  170. case 'resize': offloadFn(setup); break;
  171. }
  172. if (options.stopPropagation) event.stopPropagation();
  173. },
  174. start: function(event) {
  175. var touches = event.touches[0];
  176. // measure start values
  177. start = {
  178. // get initial touch coords
  179. x: touches.pageX,
  180. y: touches.pageY,
  181. // store time to determine touch duration
  182. time: +new Date
  183. };
  184. // used for testing first move event
  185. isScrolling = undefined;
  186. // reset delta and end measurements
  187. delta = {};
  188. // attach touchmove and touchend listeners
  189. element.addEventListener('touchmove', this, false);
  190. element.addEventListener('touchend', this, false);
  191. },
  192. move: function(event) {
  193. // ensure swiping with one touch and not pinching
  194. if ( event.touches.length > 1 || event.scale && event.scale !== 1) return
  195. if (options.disableScroll) event.preventDefault();
  196. var touches = event.touches[0];
  197. // measure change in x and y
  198. delta = {
  199. x: touches.pageX - start.x,
  200. y: touches.pageY - start.y
  201. }
  202. // determine if scrolling test has run - one time test
  203. if ( typeof isScrolling == 'undefined') {
  204. isScrolling = !!( isScrolling || Math.abs(delta.x) < Math.abs(delta.y) );
  205. }
  206. // if user is not trying to scroll vertically
  207. if (!isScrolling) {
  208. // prevent native scrolling
  209. event.preventDefault();
  210. // stop slideshow
  211. stop();
  212. // increase resistance if first or last slide
  213. if (options.continuous) { // we don't add resistance at the end
  214. translate(circle(index-1), delta.x + slidePos[circle(index-1)], 0);
  215. translate(index, delta.x + slidePos[index], 0);
  216. translate(circle(index+1), delta.x + slidePos[circle(index+1)], 0);
  217. } else {
  218. delta.x =
  219. delta.x /
  220. ( (!index && delta.x > 0 // if first slide and sliding left
  221. || index == slides.length - 1 // or if last slide and sliding right
  222. && delta.x < 0 // and if sliding at all
  223. ) ?
  224. ( Math.abs(delta.x) / width + 1 ) // determine resistance level
  225. : 1 ); // no resistance if false
  226. // translate 1:1
  227. translate(index-1, delta.x + slidePos[index-1], 0);
  228. translate(index, delta.x + slidePos[index], 0);
  229. translate(index+1, delta.x + slidePos[index+1], 0);
  230. }
  231. }
  232. },
  233. end: function(event) {
  234. // measure duration
  235. var duration = +new Date - start.time;
  236. // determine if slide attempt triggers next/prev slide
  237. var isValidSlide =
  238. Number(duration) < 250 // if slide duration is less than 250ms
  239. && Math.abs(delta.x) > 20 // and if slide amt is greater than 20px
  240. || Math.abs(delta.x) > width/2; // or if slide amt is greater than half the width
  241. // determine if slide attempt is past start and end
  242. var isPastBounds =
  243. !index && delta.x > 0 // if first slide and slide amt is greater than 0
  244. || index == slides.length - 1 && delta.x < 0; // or if last slide and slide amt is less than 0
  245. if (options.continuous) isPastBounds = false;
  246. // determine direction of swipe (true:right, false:left)
  247. var direction = delta.x < 0;
  248. // if not scrolling vertically
  249. if (!isScrolling) {
  250. if (isValidSlide && !isPastBounds) {
  251. if (direction) {
  252. if (options.continuous) { // we need to get the next in this direction in place
  253. move(circle(index-1), -width, 0);
  254. move(circle(index+2), width, 0);
  255. } else {
  256. move(index-1, -width, 0);
  257. }
  258. move(index, slidePos[index]-width, speed);
  259. move(circle(index+1), slidePos[circle(index+1)]-width, speed);
  260. index = circle(index+1);
  261. } else {
  262. if (options.continuous) { // we need to get the next in this direction in place
  263. move(circle(index+1), width, 0);
  264. move(circle(index-2), -width, 0);
  265. } else {
  266. move(index+1, width, 0);
  267. }
  268. move(index, slidePos[index]+width, speed);
  269. move(circle(index-1), slidePos[circle(index-1)]+width, speed);
  270. index = circle(index-1);
  271. }
  272. options.callback && options.callback((length == 2 && index > 1 ? index - 2 : index), slides[index]);
  273. } else {
  274. if (options.continuous) {
  275. move(circle(index-1), -width, speed);
  276. move(index, 0, speed);
  277. move(circle(index+1), width, speed);
  278. } else {
  279. move(index-1, -width, speed);
  280. move(index, 0, speed);
  281. move(index+1, width, speed);
  282. }
  283. }
  284. }
  285. // kill touchmove and touchend event listeners until touchstart called again
  286. element.removeEventListener('touchmove', events, false)
  287. element.removeEventListener('touchend', events, false)
  288. },
  289. transitionEnd: function(event) {
  290. if (parseInt(event.target.getAttribute('data-index'), 10) == index) {
  291. if (delay) begin();
  292. options.transitionEnd && options.transitionEnd.call(event, (length == 2 && index > 1 ? index - 2 : index), slides[index]);
  293. }
  294. }
  295. }
  296. // trigger setup
  297. setup();
  298. // start auto slideshow if applicable
  299. if (delay) begin();
  300. // add event listeners
  301. if (browser.addEventListener) {
  302. // set touchstart event on element
  303. if (browser.touch) element.addEventListener('touchstart', events, false);
  304. if (browser.transitions) {
  305. element.addEventListener('webkitTransitionEnd', events, false);
  306. element.addEventListener('msTransitionEnd', events, false);
  307. element.addEventListener('oTransitionEnd', events, false);
  308. element.addEventListener('otransitionend', events, false);
  309. element.addEventListener('transitionend', events, false);
  310. }
  311. // set resize event on window
  312. window.addEventListener('resize', events, false);
  313. } else {
  314. window.onresize = function () { setup() }; // to play nice with old IE
  315. }
  316. // expose the Swipe API
  317. return {
  318. setup: function() {
  319. setup();
  320. },
  321. slide: function(to, speed) {
  322. // cancel slideshow
  323. stop();
  324. slide(to, speed);
  325. },
  326. prev: function() {
  327. // cancel slideshow
  328. stop();
  329. prev();
  330. },
  331. next: function() {
  332. // cancel slideshow
  333. stop();
  334. next();
  335. },
  336. stop: function() {
  337. // cancel slideshow
  338. stop();
  339. },
  340. getPos: function() {
  341. // return current index position
  342. return index;
  343. },
  344. getNumSlides: function() {
  345. // return total number of slides
  346. return length;
  347. },
  348. kill: function() {
  349. // cancel slideshow
  350. stop();
  351. // reset element
  352. element.style.width = '';
  353. element.style.left = '';
  354. // reset slides
  355. var pos = slides.length;
  356. while(pos--) {
  357. var slide = slides[pos];
  358. slide.style.width = '';
  359. slide.style.left = '';
  360. if (browser.transitions) translate(pos, 0, 0);
  361. }
  362. // removed event listeners
  363. if (browser.addEventListener) {
  364. // remove current event listeners
  365. element.removeEventListener('touchstart', events, false);
  366. element.removeEventListener('webkitTransitionEnd', events, false);
  367. element.removeEventListener('msTransitionEnd', events, false);
  368. element.removeEventListener('oTransitionEnd', events, false);
  369. element.removeEventListener('otransitionend', events, false);
  370. element.removeEventListener('transitionend', events, false);
  371. window.removeEventListener('resize', events, false);
  372. }
  373. else {
  374. window.onresize = null;
  375. }
  376. }
  377. }
  378. }
  379. if ( window.jQuery || window.Zepto ) {
  380. (function($) {
  381. $.fn.Swipe = function(params) {
  382. return this.each(function() {
  383. $(this).data('Swipe', new Swipe($(this)[0], params));
  384. });
  385. }
  386. })( window.jQuery || window.Zepto )
  387. }
  388. module.exports = function(container, opts){
  389. return new Swipe(container, opts);
  390. };