utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. function deleteProps(obj) {
  2. const object = obj;
  3. Object.keys(object).forEach(key => {
  4. try {
  5. object[key] = null;
  6. } catch (e) { // no getter for object
  7. }
  8. try {
  9. delete object[key];
  10. } catch (e) { // something got wrong
  11. }
  12. });
  13. }
  14. function getTranslate(el, axis = 'x') {
  15. let curTransform;
  16. if (axis === 'x') {
  17. curTransform = el.translate;
  18. }
  19. if (axis === 'y') {
  20. curTransform = el.translate;
  21. }
  22. return curTransform || 0;
  23. }
  24. function isObject(o) {
  25. return typeof o === 'object' && o !== null && o.constructor && Object.prototype.toString.call(o).slice(8, -1) ===
  26. 'Object';
  27. }
  28. function now() {
  29. return Date.now();
  30. }
  31. function nextTick(callback, delay = 0) {
  32. return setTimeout(callback, delay);
  33. }
  34. function extend(...args) {
  35. const to = Object(args[0]);
  36. const noExtend = ['__proto__', 'constructor', 'prototype'];
  37. for (let i = 1; i < args.length; i += 1) {
  38. const nextSource = args[i];
  39. if (nextSource !== undefined && nextSource !== null) {
  40. const keysArray = Object.keys(Object(nextSource)).filter(key => noExtend.indexOf(key) < 0);
  41. for (let nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex += 1) {
  42. const nextKey = keysArray[nextIndex];
  43. const desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
  44. if (desc !== undefined && desc.enumerable) {
  45. if (isObject(to[nextKey]) && isObject(nextSource[nextKey])) {
  46. if (nextSource[nextKey].__swiper__) {
  47. to[nextKey] = nextSource[nextKey];
  48. } else {
  49. extend(to[nextKey], nextSource[nextKey]);
  50. }
  51. } else if (!isObject(to[nextKey]) && isObject(nextSource[nextKey])) {
  52. to[nextKey] = {};
  53. if (nextSource[nextKey].__swiper__) {
  54. to[nextKey] = nextSource[nextKey];
  55. } else {
  56. extend(to[nextKey], nextSource[nextKey]);
  57. }
  58. } else {
  59. to[nextKey] = nextSource[nextKey];
  60. }
  61. }
  62. }
  63. }
  64. }
  65. return to;
  66. }
  67. function setCSSProperty(el, varName, varValue) {
  68. el.style.setProperty(varName, varValue);
  69. }
  70. function animateCSSModeScroll({
  71. swiper,
  72. targetPosition,
  73. side
  74. }) {
  75. const window = getWindow();
  76. const startPosition = -swiper.translate;
  77. let startTime = null;
  78. let time;
  79. const duration = swiper.params.speed;
  80. swiper.wrapperEl.style.scrollSnapType = 'none';
  81. window.cancelAnimationFrame(swiper.cssModeFrameID);
  82. const dir = targetPosition > startPosition ? 'next' : 'prev';
  83. const isOutOfBound = (current, target) => {
  84. return dir === 'next' && current >= target || dir === 'prev' && current <= target;
  85. };
  86. const animate = () => {
  87. time = new Date().getTime();
  88. if (startTime === null) {
  89. startTime = time;
  90. }
  91. const progress = Math.max(Math.min((time - startTime) / duration, 1), 0);
  92. const easeProgress = 0.5 - Math.cos(progress * Math.PI) / 2;
  93. let currentPosition = startPosition + easeProgress * (targetPosition - startPosition);
  94. if (isOutOfBound(currentPosition, targetPosition)) {
  95. currentPosition = targetPosition;
  96. }
  97. swiper.wrapperEl.scrollTo({
  98. [side]: currentPosition
  99. });
  100. if (isOutOfBound(currentPosition, targetPosition)) {
  101. swiper.wrapperEl.style.overflow = 'hidden';
  102. swiper.wrapperEl.style.scrollSnapType = '';
  103. setTimeout(() => {
  104. swiper.wrapperEl.style.overflow = '';
  105. swiper.wrapperEl.scrollTo({
  106. [side]: currentPosition
  107. });
  108. });
  109. window.cancelAnimationFrame(swiper.cssModeFrameID);
  110. return;
  111. }
  112. swiper.cssModeFrameID = window.requestAnimationFrame(animate);
  113. };
  114. animate();
  115. }
  116. export {
  117. deleteProps,
  118. extend,
  119. nextTick,
  120. now,
  121. setCSSProperty,
  122. animateCSSModeScroll,
  123. getTranslate,
  124. isObject
  125. };