utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { getWindow } from 'ssr-window';
  2. function deleteProps(obj) {
  3. const object = obj;
  4. Object.keys(object).forEach(key => {
  5. try {
  6. object[key] = null;
  7. } catch (e) {// no getter for object
  8. }
  9. try {
  10. delete object[key];
  11. } catch (e) {// something got wrong
  12. }
  13. });
  14. }
  15. function nextTick(callback, delay = 0) {
  16. return setTimeout(callback, delay);
  17. }
  18. function now() {
  19. return Date.now();
  20. }
  21. function getComputedStyle(el) {
  22. const window = getWindow();
  23. let style;
  24. if (window.getComputedStyle) {
  25. style = window.getComputedStyle(el, null);
  26. }
  27. if (!style && el.currentStyle) {
  28. style = el.currentStyle;
  29. }
  30. if (!style) {
  31. style = el.style;
  32. }
  33. return style;
  34. }
  35. function getTranslate(el, axis = 'x') {
  36. const window = getWindow();
  37. let matrix;
  38. let curTransform;
  39. let transformMatrix;
  40. const curStyle = getComputedStyle(el, null);
  41. if (window.WebKitCSSMatrix) {
  42. curTransform = curStyle.transform || curStyle.webkitTransform;
  43. if (curTransform.split(',').length > 6) {
  44. curTransform = curTransform.split(', ').map(a => a.replace(',', '.')).join(', ');
  45. } // Some old versions of Webkit choke when 'none' is passed; pass
  46. // empty string instead in this case
  47. transformMatrix = new window.WebKitCSSMatrix(curTransform === 'none' ? '' : curTransform);
  48. } else {
  49. transformMatrix = curStyle.MozTransform || curStyle.OTransform || curStyle.MsTransform || curStyle.msTransform || curStyle.transform || curStyle.getPropertyValue('transform').replace('translate(', 'matrix(1, 0, 0, 1,');
  50. matrix = transformMatrix.toString().split(',');
  51. }
  52. if (axis === 'x') {
  53. // Latest Chrome and webkits Fix
  54. if (window.WebKitCSSMatrix) curTransform = transformMatrix.m41; // Crazy IE10 Matrix
  55. else if (matrix.length === 16) curTransform = parseFloat(matrix[12]); // Normal Browsers
  56. else curTransform = parseFloat(matrix[4]);
  57. }
  58. if (axis === 'y') {
  59. // Latest Chrome and webkits Fix
  60. if (window.WebKitCSSMatrix) curTransform = transformMatrix.m42; // Crazy IE10 Matrix
  61. else if (matrix.length === 16) curTransform = parseFloat(matrix[13]); // Normal Browsers
  62. else curTransform = parseFloat(matrix[5]);
  63. }
  64. return curTransform || 0;
  65. }
  66. function isObject(o) {
  67. return typeof o === 'object' && o !== null && o.constructor && Object.prototype.toString.call(o).slice(8, -1) === 'Object';
  68. }
  69. function isNode(node) {
  70. // eslint-disable-next-line
  71. if (typeof window !== 'undefined' && typeof window.HTMLElement !== 'undefined') {
  72. return node instanceof HTMLElement;
  73. }
  74. return node && (node.nodeType === 1 || node.nodeType === 11);
  75. }
  76. function extend(...args) {
  77. const to = Object(args[0]);
  78. const noExtend = ['__proto__', 'constructor', 'prototype'];
  79. for (let i = 1; i < args.length; i += 1) {
  80. const nextSource = args[i];
  81. if (nextSource !== undefined && nextSource !== null && !isNode(nextSource)) {
  82. const keysArray = Object.keys(Object(nextSource)).filter(key => noExtend.indexOf(key) < 0);
  83. for (let nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex += 1) {
  84. const nextKey = keysArray[nextIndex];
  85. const desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
  86. if (desc !== undefined && desc.enumerable) {
  87. if (isObject(to[nextKey]) && isObject(nextSource[nextKey])) {
  88. if (nextSource[nextKey].__swiper__) {
  89. to[nextKey] = nextSource[nextKey];
  90. } else {
  91. extend(to[nextKey], nextSource[nextKey]);
  92. }
  93. } else if (!isObject(to[nextKey]) && isObject(nextSource[nextKey])) {
  94. to[nextKey] = {};
  95. if (nextSource[nextKey].__swiper__) {
  96. to[nextKey] = nextSource[nextKey];
  97. } else {
  98. extend(to[nextKey], nextSource[nextKey]);
  99. }
  100. } else {
  101. to[nextKey] = nextSource[nextKey];
  102. }
  103. }
  104. }
  105. }
  106. }
  107. return to;
  108. }
  109. function setCSSProperty(el, varName, varValue) {
  110. el.style.setProperty(varName, varValue);
  111. }
  112. function animateCSSModeScroll({
  113. swiper,
  114. targetPosition,
  115. side
  116. }) {
  117. const window = getWindow();
  118. const startPosition = -swiper.translate;
  119. let startTime = null;
  120. let time;
  121. const duration = swiper.params.speed;
  122. swiper.wrapperEl.style.scrollSnapType = 'none';
  123. window.cancelAnimationFrame(swiper.cssModeFrameID);
  124. const dir = targetPosition > startPosition ? 'next' : 'prev';
  125. const isOutOfBound = (current, target) => {
  126. return dir === 'next' && current >= target || dir === 'prev' && current <= target;
  127. };
  128. const animate = () => {
  129. time = new Date().getTime();
  130. if (startTime === null) {
  131. startTime = time;
  132. }
  133. const progress = Math.max(Math.min((time - startTime) / duration, 1), 0);
  134. const easeProgress = 0.5 - Math.cos(progress * Math.PI) / 2;
  135. let currentPosition = startPosition + easeProgress * (targetPosition - startPosition);
  136. if (isOutOfBound(currentPosition, targetPosition)) {
  137. currentPosition = targetPosition;
  138. }
  139. swiper.wrapperEl.scrollTo({
  140. [side]: currentPosition
  141. });
  142. if (isOutOfBound(currentPosition, targetPosition)) {
  143. swiper.wrapperEl.style.overflow = 'hidden';
  144. swiper.wrapperEl.style.scrollSnapType = '';
  145. setTimeout(() => {
  146. swiper.wrapperEl.style.overflow = '';
  147. swiper.wrapperEl.scrollTo({
  148. [side]: currentPosition
  149. });
  150. });
  151. window.cancelAnimationFrame(swiper.cssModeFrameID);
  152. return;
  153. }
  154. swiper.cssModeFrameID = window.requestAnimationFrame(animate);
  155. };
  156. animate();
  157. }
  158. export { animateCSSModeScroll, deleteProps, nextTick, now, getTranslate, isObject, extend, getComputedStyle, setCSSProperty };