swipe.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Swipe 1.0
  3. *
  4. * Brad Birdsall, Prime
  5. * Copyright 2011, Licensed GPL & MIT
  6. *
  7. */
  8. window.Swipe = function(element, options) {
  9. // return immediately if element doesn't exist
  10. if (!element) return null;
  11. var _this = this;
  12. // retreive options
  13. this.options = options || {};
  14. this.index = this.options.startSlide || 0;
  15. this.speed = this.options.speed || 300;
  16. this.callback = this.options.callback || function() {};
  17. this.delay = this.options.auto || 0;
  18. // reference dom elements
  19. this.container = element;
  20. this.element = this.container.children[0]; // the slide pane
  21. // static css
  22. this.container.style.overflow = 'hidden';
  23. this.element.style.listStyle = 'none';
  24. // trigger slider initialization
  25. this.setup();
  26. // begin auto slideshow
  27. this.begin();
  28. // add event listeners
  29. if (this.element.addEventListener) {
  30. this.element.addEventListener('touchstart', this, false);
  31. this.element.addEventListener('touchmove', this, false);
  32. this.element.addEventListener('touchend', this, false);
  33. this.element.addEventListener('webkitTransitionEnd', this, false);
  34. this.element.addEventListener('msTransitionEnd', this, false);
  35. this.element.addEventListener('oTransitionEnd', this, false);
  36. this.element.addEventListener('transitionend', this, false);
  37. window.addEventListener('resize', this, false);
  38. }
  39. };
  40. Swipe.prototype = {
  41. setup: function() {
  42. // get and measure amt of slides
  43. this.slides = this.element.children;
  44. this.length = this.slides.length;
  45. // return immediately if their are less than two slides
  46. if (this.length < 2) return null;
  47. // determine width of each slide
  48. this.width = this.container.getBoundingClientRect().width;
  49. // return immediately if measurement fails
  50. if (!this.width) return null;
  51. // hide slider element but keep positioning during setup
  52. this.container.style.visibility = 'hidden';
  53. // dynamic css
  54. this.element.style.width = (this.slides.length * this.width) + 'px';
  55. var index = this.slides.length;
  56. while (index--) {
  57. var el = this.slides[index];
  58. el.style.width = this.width + 'px';
  59. el.style.display = 'table-cell';
  60. el.style.verticalAlign = 'top';
  61. }
  62. // set start position and force translate to remove initial flickering
  63. this.slide(this.index, 0);
  64. // show slider element
  65. this.container.style.visibility = 'visible';
  66. },
  67. slide: function(index, duration) {
  68. var style = this.element.style;
  69. // fallback to default speed
  70. if (duration == undefined) {
  71. duration = this.speed;
  72. }
  73. // set duration speed (0 represents 1-to-1 scrolling)
  74. style.webkitTransitionDuration = style.MozTransitionDuration = style.msTransitionDuration = style.OTransitionDuration = style.transitionDuration = duration + 'ms';
  75. // translate to given index position
  76. style.MozTransform = style.webkitTransform = 'translate3d(' + -(index * this.width) + 'px,0,0)';
  77. style.msTransform = style.OTransform = 'translateX(' + -(index * this.width) + 'px)';
  78. // set new index to allow for expression arguments
  79. this.index = index;
  80. },
  81. getPos: function() {
  82. // return current index position
  83. return this.index;
  84. },
  85. prev: function(delay) {
  86. // cancel next scheduled automatic transition, if any
  87. this.delay = delay || 0;
  88. clearTimeout(this.interval);
  89. // if not at first slide
  90. if (this.index) this.slide(this.index-1, this.speed);
  91. },
  92. next: function(delay) {
  93. // cancel next scheduled automatic transition, if any
  94. this.delay = delay || 0;
  95. clearTimeout(this.interval);
  96. if (this.index < this.length - 1) this.slide(this.index+1, this.speed); // if not last slide
  97. else this.slide(0, this.speed); //if last slide return to start
  98. },
  99. begin: function() {
  100. var _this = this;
  101. this.interval = (this.delay)
  102. ? setTimeout(function() {
  103. _this.next(_this.delay);
  104. }, this.delay)
  105. : 0;
  106. },
  107. stop: function() {
  108. this.delay = 0;
  109. clearTimeout(this.interval);
  110. },
  111. resume: function() {
  112. this.delay = this.options.auto || 0;
  113. this.begin();
  114. },
  115. handleEvent: function(e) {
  116. switch (e.type) {
  117. case 'touchstart': this.onTouchStart(e); break;
  118. case 'touchmove': this.onTouchMove(e); break;
  119. case 'touchend': this.onTouchEnd(e); break;
  120. case 'webkitTransitionEnd':
  121. case 'msTransitionEnd':
  122. case 'oTransitionEnd':
  123. case 'transitionend': this.transitionEnd(e); break;
  124. case 'resize': this.setup(); break;
  125. }
  126. },
  127. transitionEnd: function(e) {
  128. if (this.delay) this.begin();
  129. this.callback(e, this.index, this.slides[this.index]);
  130. },
  131. onTouchStart: function(e) {
  132. this.start = {
  133. // get touch coordinates for delta calculations in onTouchMove
  134. pageX: e.touches[0].pageX,
  135. pageY: e.touches[0].pageY,
  136. // set initial timestamp of touch sequence
  137. time: Number( new Date() )
  138. };
  139. // used for testing first onTouchMove event
  140. this.isScrolling = undefined;
  141. // reset deltaX
  142. this.deltaX = 0;
  143. // set transition time to 0 for 1-to-1 touch movement
  144. this.element.style.MozTransitionDuration = this.element.style.webkitTransitionDuration = 0;
  145. },
  146. onTouchMove: function(e) {
  147. // ensure swiping with one touch and not pinching
  148. if(e.touches.length > 1 || e.scale && e.scale !== 1) return;
  149. //this.deltaX = e.touches[0].pageX - this.start.pageX;
  150. this.deltaY = e.touches[0].pageY - this.start.pageY;
  151. // determine if scrolling test has run - one time test
  152. if ( typeof this.isScrolling == 'undefined') {
  153. //this.isScrolling = !!( this.isScrolling || Math.abs(this.deltaX) < Math.abs(e.touches[0].pageY - this.start.pageY) );
  154. this.isScrolling = !!( this.isScrolling || Math.abs(this.deltaY) < Math.abs(e.touches[0].pageX - this.start.pageX) );
  155. }
  156. // if user is not trying to scroll vertically
  157. if (!this.isScrolling) {
  158. // prevent native scrolling
  159. e.preventDefault();
  160. // cancel slideshow
  161. clearTimeout(this.interval);
  162. // increase resistance if first or last slide
  163. /* this.deltaX =
  164. this.deltaX /
  165. ( (!this.index && this.deltaX > 0 // if first slide and sliding left
  166. || this.index == this.length - 1 // or if last slide and sliding right
  167. && this.deltaX < 0 // and if sliding at all
  168. ) ?
  169. ( Math.abs(this.deltaX) / this.width + 1 ) // determine resistance level
  170. : 1 ); // no resistance if false
  171. */
  172. this.deltaY =
  173. this.deltaY /
  174. ( (!this.index && this.deltaY > 0 // if first slide and sliding left
  175. || this.index == this.length - 1 // or if last slide and sliding right
  176. && this.deltaY < 0 // and if sliding at all
  177. ) ?
  178. ( Math.abs(this.deltaY) / this.width + 1 ) // determine resistance level
  179. : 1 );
  180. // translate immediately 1-to-1
  181. // this.element.style.MozTransform = this.element.style.webkitTransform = 'translate3d(' + (this.deltaX - this.index * this.width) + 'px,0,0)';
  182. this.element.style.MozTransform = this.element.style.webkitTransform = 'translate3d(0,' + (this.deltaY - this.index * this.height) + 'px,0)';
  183. }
  184. },
  185. onTouchEnd: function(e) {
  186. // determine if slide attempt triggers next/prev slide
  187. var isValidSlide =
  188. Number(new Date()) - this.start.time < 250 // if slide duration is less than 250ms
  189. && Math.abs(this.deltaX) > 20 // and if slide amt is greater than 20px
  190. || Math.abs(this.deltaX) > this.width/2, // or if slide amt is greater than half the width
  191. // determine if slide attempt is past start and end
  192. isPastBounds =
  193. !this.index && this.deltaX > 0 // if first slide and slide amt is greater than 0
  194. || this.index == this.length - 1 && this.deltaX < 0; // or if last slide and slide amt is less than 0
  195. // if not scrolling vertically
  196. if (!this.isScrolling) {
  197. // call slide function with slide end value based on isValidSlide and isPastBounds tests
  198. this.slide( this.index + ( isValidSlide && !isPastBounds ? (this.deltaX < 0 ? 1 : -1) : 0 ), this.speed );
  199. }
  200. }
  201. };