mui.zoom.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. (function($, window) {
  2. var CLASS_ZOOM = $.className('zoom');
  3. var CLASS_ZOOM_SCROLLER = $.className('zoom-scroller');
  4. var SELECTOR_ZOOM = '.' + CLASS_ZOOM;
  5. var SELECTOR_ZOOM_SCROLLER = '.' + CLASS_ZOOM_SCROLLER;
  6. var EVENT_PINCH_START = 'pinchstart';
  7. var EVENT_PINCH = 'pinch';
  8. var EVENT_PINCH_END = 'pinchend';
  9. if ('ongesturestart' in window) {
  10. EVENT_PINCH_START = 'gesturestart';
  11. EVENT_PINCH = 'gesturechange';
  12. EVENT_PINCH_END = 'gestureend';
  13. }
  14. $.Zoom = function(element, options) {
  15. var zoom = this;
  16. zoom.options = $.extend($.Zoom.defaults, options);
  17. zoom.wrapper = zoom.element = element;
  18. zoom.scroller = element.querySelector(SELECTOR_ZOOM_SCROLLER);
  19. zoom.scrollerStyle = zoom.scroller && zoom.scroller.style;
  20. zoom.zoomer = element.querySelector(SELECTOR_ZOOM);
  21. zoom.zoomerStyle = zoom.zoomer && zoom.zoomer.style;
  22. zoom.init = function() {
  23. //自动启用
  24. $.options.gestureConfig.pinch = true;
  25. $.options.gestureConfig.doubletap = true;
  26. zoom.initEvents();
  27. };
  28. zoom.initEvents = function(detach) {
  29. var action = detach ? 'removeEventListener' : 'addEventListener';
  30. var target = zoom.scroller;
  31. target[action](EVENT_PINCH_START, zoom.onPinchstart);
  32. target[action](EVENT_PINCH, zoom.onPinch);
  33. target[action](EVENT_PINCH_END, zoom.onPinchend);
  34. target[action]('touchstart', zoom.onTouchstart);
  35. target[action]('touchmove', zoom.onTouchMove);
  36. target[action]('touchcancel', zoom.onTouchEnd);
  37. target[action]('touchend', zoom.onTouchEnd);
  38. target[action]('drag', function(e) {
  39. if (imageIsMoved || isGesturing) {
  40. e.stopPropagation();
  41. }
  42. });
  43. target[action]('doubletap', function(e) {
  44. zoom.toggleZoom(e.detail.center);
  45. });
  46. };
  47. zoom.transition = function(style, time) {
  48. time = time || 0;
  49. style['webkitTransitionDuration'] = time + 'ms';
  50. return zoom;
  51. };
  52. zoom.translate = function(style, x, y) {
  53. x = x || 0;
  54. y = y || 0;
  55. style['webkitTransform'] = 'translate3d(' + x + 'px,' + y + 'px,0px)';
  56. return zoom;
  57. };
  58. zoom.scale = function(style, scale) {
  59. scale = scale || 1;
  60. style['webkitTransform'] = 'translate3d(0,0,0) scale(' + scale + ')';
  61. return zoom;
  62. };
  63. zoom.scrollerTransition = function(time) {
  64. return zoom.transition(zoom.scrollerStyle, time);
  65. };
  66. zoom.scrollerTransform = function(x, y) {
  67. return zoom.translate(zoom.scrollerStyle, x, y);
  68. };
  69. zoom.zoomerTransition = function(time) {
  70. return zoom.transition(zoom.zoomerStyle, time);
  71. };
  72. zoom.zoomerTransform = function(scale) {
  73. return zoom.scale(zoom.zoomerStyle, scale);
  74. };
  75. // Gestures
  76. var scale = 1,
  77. currentScale = 1,
  78. isScaling = false,
  79. isGesturing = false;
  80. zoom.onPinchstart = function(e) {
  81. isGesturing = true;
  82. };
  83. zoom.onPinch = function(e) {
  84. if (!isScaling) {
  85. zoom.zoomerTransition(0);
  86. isScaling = true;
  87. }
  88. scale = (e.detail ? e.detail.scale : e.scale) * currentScale;
  89. if (scale > zoom.options.maxZoom) {
  90. scale = zoom.options.maxZoom - 1 + Math.pow((scale - zoom.options.maxZoom + 1), 0.5);
  91. }
  92. if (scale < zoom.options.minZoom) {
  93. scale = zoom.options.minZoom + 1 - Math.pow((zoom.options.minZoom - scale + 1), 0.5);
  94. }
  95. zoom.zoomerTransform(scale);
  96. };
  97. zoom.onPinchend = function(e) {
  98. scale = Math.max(Math.min(scale, zoom.options.maxZoom), zoom.options.minZoom);
  99. zoom.zoomerTransition(zoom.options.speed).zoomerTransform(scale);
  100. currentScale = scale;
  101. isScaling = false;
  102. };
  103. zoom.setZoom = function(newScale) {
  104. scale = currentScale = newScale;
  105. zoom.scrollerTransition(zoom.options.speed).scrollerTransform(0, 0);
  106. zoom.zoomerTransition(zoom.options.speed).zoomerTransform(scale);
  107. };
  108. zoom.toggleZoom = function(position, speed) {
  109. if (typeof position === 'number') {
  110. speed = position;
  111. position = undefined;
  112. }
  113. speed = typeof speed === 'undefined' ? zoom.options.speed : speed;
  114. if (scale && scale !== 1) {
  115. scale = currentScale = 1;
  116. zoom.scrollerTransition(speed).scrollerTransform(0, 0);
  117. } else {
  118. scale = currentScale = zoom.options.maxZoom;
  119. if (position) {
  120. var offset = $.offset(zoom.zoomer);
  121. var top = offset.top;
  122. var left = offset.left;
  123. var offsetX = (position.x - left) * scale;
  124. var offsetY = (position.y - top) * scale;
  125. this._cal();
  126. if (offsetX >= imageMaxX && offsetX <= (imageMaxX + wrapperWidth)) { //center
  127. offsetX = imageMaxX - offsetX + wrapperWidth / 2;
  128. } else if (offsetX < imageMaxX) { //left
  129. offsetX = imageMaxX - offsetX + wrapperWidth / 2;
  130. } else if (offsetX > (imageMaxX + wrapperWidth)) { //right
  131. offsetX = imageMaxX + wrapperWidth - offsetX - wrapperWidth / 2;
  132. }
  133. if (offsetY >= imageMaxY && offsetY <= (imageMaxY + wrapperHeight)) { //middle
  134. offsetY = imageMaxY - offsetY + wrapperHeight / 2;
  135. } else if (offsetY < imageMaxY) { //top
  136. offsetY = imageMaxY - offsetY + wrapperHeight / 2;
  137. } else if (offsetY > (imageMaxY + wrapperHeight)) { //bottom
  138. offsetY = imageMaxY + wrapperHeight - offsetY - wrapperHeight / 2;
  139. }
  140. offsetX = Math.min(Math.max(offsetX, imageMinX), imageMaxX);
  141. offsetY = Math.min(Math.max(offsetY, imageMinY), imageMaxY);
  142. zoom.scrollerTransition(speed).scrollerTransform(offsetX, offsetY);
  143. } else {
  144. zoom.scrollerTransition(speed).scrollerTransform(0, 0);
  145. }
  146. }
  147. zoom.zoomerTransition(speed).zoomerTransform(scale);
  148. };
  149. zoom._cal = function() {
  150. wrapperWidth = zoom.wrapper.offsetWidth;
  151. wrapperHeight = zoom.wrapper.offsetHeight;
  152. imageWidth = zoom.zoomer.offsetWidth;
  153. imageHeight = zoom.zoomer.offsetHeight;
  154. var scaledWidth = imageWidth * scale;
  155. var scaledHeight = imageHeight * scale;
  156. imageMinX = Math.min((wrapperWidth / 2 - scaledWidth / 2), 0);
  157. imageMaxX = -imageMinX;
  158. imageMinY = Math.min((wrapperHeight / 2 - scaledHeight / 2), 0);
  159. imageMaxY = -imageMinY;
  160. };
  161. var wrapperWidth, wrapperHeight, imageIsTouched, imageIsMoved, imageCurrentX, imageCurrentY, imageMinX, imageMinY, imageMaxX, imageMaxY, imageWidth, imageHeight, imageTouchesStart = {},
  162. imageTouchesCurrent = {},
  163. imageStartX, imageStartY, velocityPrevPositionX, velocityPrevTime, velocityX, velocityPrevPositionY, velocityY;
  164. zoom.onTouchstart = function(e) {
  165. e.preventDefault();
  166. imageIsTouched = true;
  167. imageTouchesStart.x = e.type === 'touchstart' ? e.targetTouches[0].pageX : e.pageX;
  168. imageTouchesStart.y = e.type === 'touchstart' ? e.targetTouches[0].pageY : e.pageY;
  169. };
  170. zoom.onTouchMove = function(e) {
  171. e.preventDefault();
  172. if (!imageIsTouched) return;
  173. if (!imageIsMoved) {
  174. wrapperWidth = zoom.wrapper.offsetWidth;
  175. wrapperHeight = zoom.wrapper.offsetHeight;
  176. imageWidth = zoom.zoomer.offsetWidth;
  177. imageHeight = zoom.zoomer.offsetHeight;
  178. var translate = $.parseTranslateMatrix($.getStyles(zoom.scroller, 'webkitTransform'));
  179. imageStartX = translate.x || 0;
  180. imageStartY = translate.y || 0;
  181. zoom.scrollerTransition(0);
  182. }
  183. var scaledWidth = imageWidth * scale;
  184. var scaledHeight = imageHeight * scale;
  185. if (scaledWidth < wrapperWidth && scaledHeight < wrapperHeight) return;
  186. imageMinX = Math.min((wrapperWidth / 2 - scaledWidth / 2), 0);
  187. imageMaxX = -imageMinX;
  188. imageMinY = Math.min((wrapperHeight / 2 - scaledHeight / 2), 0);
  189. imageMaxY = -imageMinY;
  190. imageTouchesCurrent.x = e.type === 'touchmove' ? e.targetTouches[0].pageX : e.pageX;
  191. imageTouchesCurrent.y = e.type === 'touchmove' ? e.targetTouches[0].pageY : e.pageY;
  192. if (!imageIsMoved && !isScaling) {
  193. // if (Math.abs(imageTouchesCurrent.y - imageTouchesStart.y) < Math.abs(imageTouchesCurrent.x - imageTouchesStart.x)) {
  194. //TODO 此处需要优化,当遇到长图,需要上下滚动时,下列判断会导致滚动不流畅
  195. if (
  196. (Math.floor(imageMinX) === Math.floor(imageStartX) && imageTouchesCurrent.x < imageTouchesStart.x) ||
  197. (Math.floor(imageMaxX) === Math.floor(imageStartX) && imageTouchesCurrent.x > imageTouchesStart.x)
  198. ) {
  199. imageIsTouched = false;
  200. return;
  201. }
  202. // }
  203. }
  204. imageIsMoved = true;
  205. imageCurrentX = imageTouchesCurrent.x - imageTouchesStart.x + imageStartX;
  206. imageCurrentY = imageTouchesCurrent.y - imageTouchesStart.y + imageStartY;
  207. if (imageCurrentX < imageMinX) {
  208. imageCurrentX = imageMinX + 1 - Math.pow((imageMinX - imageCurrentX + 1), 0.8);
  209. }
  210. if (imageCurrentX > imageMaxX) {
  211. imageCurrentX = imageMaxX - 1 + Math.pow((imageCurrentX - imageMaxX + 1), 0.8);
  212. }
  213. if (imageCurrentY < imageMinY) {
  214. imageCurrentY = imageMinY + 1 - Math.pow((imageMinY - imageCurrentY + 1), 0.8);
  215. }
  216. if (imageCurrentY > imageMaxY) {
  217. imageCurrentY = imageMaxY - 1 + Math.pow((imageCurrentY - imageMaxY + 1), 0.8);
  218. }
  219. //Velocity
  220. if (!velocityPrevPositionX) velocityPrevPositionX = imageTouchesCurrent.x;
  221. if (!velocityPrevPositionY) velocityPrevPositionY = imageTouchesCurrent.y;
  222. if (!velocityPrevTime) velocityPrevTime = $.now();
  223. velocityX = (imageTouchesCurrent.x - velocityPrevPositionX) / ($.now() - velocityPrevTime) / 2;
  224. velocityY = (imageTouchesCurrent.y - velocityPrevPositionY) / ($.now() - velocityPrevTime) / 2;
  225. if (Math.abs(imageTouchesCurrent.x - velocityPrevPositionX) < 2) velocityX = 0;
  226. if (Math.abs(imageTouchesCurrent.y - velocityPrevPositionY) < 2) velocityY = 0;
  227. velocityPrevPositionX = imageTouchesCurrent.x;
  228. velocityPrevPositionY = imageTouchesCurrent.y;
  229. velocityPrevTime = $.now();
  230. zoom.scrollerTransform(imageCurrentX, imageCurrentY);
  231. };
  232. zoom.onTouchEnd = function(e) {
  233. if (!e.touches.length) {
  234. isGesturing = false;
  235. }
  236. if (!imageIsTouched || !imageIsMoved) {
  237. imageIsTouched = false;
  238. imageIsMoved = false;
  239. return;
  240. }
  241. imageIsTouched = false;
  242. imageIsMoved = false;
  243. var momentumDurationX = 300;
  244. var momentumDurationY = 300;
  245. var momentumDistanceX = velocityX * momentumDurationX;
  246. var newPositionX = imageCurrentX + momentumDistanceX;
  247. var momentumDistanceY = velocityY * momentumDurationY;
  248. var newPositionY = imageCurrentY + momentumDistanceY;
  249. if (velocityX !== 0) momentumDurationX = Math.abs((newPositionX - imageCurrentX) / velocityX);
  250. if (velocityY !== 0) momentumDurationY = Math.abs((newPositionY - imageCurrentY) / velocityY);
  251. var momentumDuration = Math.max(momentumDurationX, momentumDurationY);
  252. imageCurrentX = newPositionX;
  253. imageCurrentY = newPositionY;
  254. var scaledWidth = imageWidth * scale;
  255. var scaledHeight = imageHeight * scale;
  256. imageMinX = Math.min((wrapperWidth / 2 - scaledWidth / 2), 0);
  257. imageMaxX = -imageMinX;
  258. imageMinY = Math.min((wrapperHeight / 2 - scaledHeight / 2), 0);
  259. imageMaxY = -imageMinY;
  260. imageCurrentX = Math.max(Math.min(imageCurrentX, imageMaxX), imageMinX);
  261. imageCurrentY = Math.max(Math.min(imageCurrentY, imageMaxY), imageMinY);
  262. zoom.scrollerTransition(momentumDuration).scrollerTransform(imageCurrentX, imageCurrentY);
  263. };
  264. zoom.destory = function() {
  265. zoom.initEvents(true); //detach
  266. delete $.data[zoom.wrapper.getAttribute('data-zoomer')];
  267. zoom.wrapper.setAttribute('data-zoomer', '');
  268. }
  269. zoom.init();
  270. return zoom;
  271. };
  272. $.Zoom.defaults = {
  273. speed: 300,
  274. maxZoom: 3,
  275. minZoom: 1,
  276. };
  277. $.fn.zoom = function(options) {
  278. var zoomApis = [];
  279. this.each(function() {
  280. var zoomApi = null;
  281. var self = this;
  282. var id = self.getAttribute('data-zoomer');
  283. if (!id) {
  284. id = ++$.uuid;
  285. $.data[id] = zoomApi = new $.Zoom(self, options);
  286. self.setAttribute('data-zoomer', id);
  287. } else {
  288. zoomApi = $.data[id];
  289. }
  290. zoomApis.push(zoomApi);
  291. });
  292. return zoomApis.length === 1 ? zoomApis[0] : zoomApis;
  293. };
  294. })(mui, window);