UNPKG

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