center-center 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +24 -158
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import debug from 'debug'
|
|
2
|
-
|
|
3
|
-
const log = debug('center-center')
|
|
4
|
-
|
|
5
1
|
export function getScrollingElement (document) {
|
|
6
2
|
if (Reflect.has(document, 'scrollingElement')) {
|
|
7
3
|
const scrollingElement = Reflect.get(document, 'scrollingElement')
|
|
@@ -64,179 +60,49 @@ export function getRects (container, target) {
|
|
|
64
60
|
export function calculateLeft ({
|
|
65
61
|
viewport: {
|
|
66
62
|
left: viewportL,
|
|
67
|
-
right: viewportR
|
|
68
|
-
width: viewportW
|
|
63
|
+
right: viewportR
|
|
69
64
|
},
|
|
70
65
|
container: {
|
|
66
|
+
width: containerW,
|
|
71
67
|
left: containerL,
|
|
72
|
-
right: containerR
|
|
73
|
-
width: containerW
|
|
68
|
+
right: containerR
|
|
74
69
|
},
|
|
75
70
|
target: {
|
|
76
71
|
width: targetW
|
|
77
72
|
}
|
|
78
73
|
}) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return ((availableW / 2) - (targetW / 2))
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (viewportL > containerL) {
|
|
94
|
-
if (containerR > viewportL) {
|
|
95
|
-
if (viewportR > containerR) {
|
|
96
|
-
log('calculate viewportL to containerR')
|
|
97
|
-
|
|
98
|
-
const availableW = Math.max(targetW, (containerL + containerW) - viewportL)
|
|
99
|
-
|
|
100
|
-
return (containerW - availableW) + ((availableW / 2) - (targetW / 2))
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (containerR > viewportR) {
|
|
104
|
-
log('calculate viewportL to viewportR')
|
|
105
|
-
|
|
106
|
-
const availableW = Math.max(targetW, viewportW)
|
|
107
|
-
|
|
108
|
-
return (viewportL - containerL) + ((availableW / 2) - (targetW / 2))
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (viewportL > containerR) {
|
|
113
|
-
log('pin to right')
|
|
114
|
-
|
|
115
|
-
const availableW = Math.max(targetW, containerW)
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Pin to right
|
|
119
|
-
*/
|
|
120
|
-
return (availableW - targetW)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (containerL > viewportL) {
|
|
125
|
-
if (containerL > viewportR) {
|
|
126
|
-
log('pin to left')
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Pin to left
|
|
130
|
-
*
|
|
131
|
-
* Scroll is above container (container is offscreen)
|
|
132
|
-
*/
|
|
133
|
-
return 0
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (viewportR > containerL) {
|
|
137
|
-
if (viewportR > containerR) {
|
|
138
|
-
log('calculate containerL to containerR')
|
|
139
|
-
|
|
140
|
-
const availableW = Math.max(targetW, (containerR - containerL))
|
|
141
|
-
|
|
142
|
-
return ((availableW / 2) - (targetW / 2))
|
|
143
|
-
} else {
|
|
144
|
-
log('calculate containerL to viewportR')
|
|
145
|
-
|
|
146
|
-
const availableW = Math.max(targetW, (viewportR - containerL))
|
|
147
|
-
|
|
148
|
-
return ((availableW / 2) - (targetW / 2))
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
74
|
+
const l = Math.max(viewportL, containerL)
|
|
75
|
+
const r = Math.min(viewportR, containerR)
|
|
76
|
+
const w = (r - l)
|
|
77
|
+
const x = (l - containerL)
|
|
78
|
+
const boundary = (containerW - targetW)
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
Math.max(0, Math.min(boundary, x + ((w / 2) - (targetW / 2))))
|
|
82
|
+
)
|
|
152
83
|
}
|
|
153
84
|
|
|
154
85
|
export function calculateTop ({
|
|
155
86
|
viewport: {
|
|
156
87
|
top: viewportT,
|
|
157
|
-
bottom: viewportB
|
|
158
|
-
height: viewportH
|
|
88
|
+
bottom: viewportB
|
|
159
89
|
},
|
|
160
90
|
container: {
|
|
91
|
+
height: containerH,
|
|
161
92
|
top: containerT,
|
|
162
|
-
bottom: containerB
|
|
163
|
-
height: containerH
|
|
93
|
+
bottom: containerB
|
|
164
94
|
},
|
|
165
95
|
target: {
|
|
166
96
|
height: targetH
|
|
167
97
|
}
|
|
168
98
|
}) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return ((availableH / 2) - (targetH / 2))
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (viewportT > containerT) {
|
|
184
|
-
if (containerB > viewportT) {
|
|
185
|
-
if (viewportB > containerB) {
|
|
186
|
-
log('calculate viewportT to containerB')
|
|
187
|
-
|
|
188
|
-
const availableH = Math.max(targetH, (containerT + containerH) - viewportT)
|
|
189
|
-
|
|
190
|
-
return (containerH - availableH) + ((availableH / 2) - (targetH / 2))
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (containerB > viewportB) {
|
|
194
|
-
log('calculate viewportT to viewportB') // , viewportT > containerT, containerB > viewportB)
|
|
195
|
-
|
|
196
|
-
const availableH = Math.max(targetH, viewportH)
|
|
197
|
-
|
|
198
|
-
return (viewportT - containerT) + ((availableH / 2) - (targetH / 2))
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (viewportT > containerB) {
|
|
203
|
-
log('pin to bottom')
|
|
204
|
-
|
|
205
|
-
const availableH = Math.max(targetH, containerH)
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Pin to bottom
|
|
209
|
-
*/
|
|
210
|
-
return (availableH - targetH)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (containerT > viewportT) {
|
|
215
|
-
if (containerT > viewportB) {
|
|
216
|
-
log('pin to top')
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Pin to top
|
|
220
|
-
*
|
|
221
|
-
* Scroll is above container (container is offscreen)
|
|
222
|
-
*/
|
|
223
|
-
return 0
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (viewportB > containerT) {
|
|
227
|
-
if (viewportB > containerB) {
|
|
228
|
-
log('calculate containerT to containerB')
|
|
229
|
-
|
|
230
|
-
const availableH = Math.max(targetH, (containerB - containerT))
|
|
231
|
-
|
|
232
|
-
return ((availableH / 2) - (targetH / 2))
|
|
233
|
-
} else {
|
|
234
|
-
log('calculate containerT to viewportB')
|
|
235
|
-
|
|
236
|
-
const availableH = Math.max(targetH, (viewportB - containerT))
|
|
237
|
-
|
|
238
|
-
return ((availableH / 2) - (targetH / 2))
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
99
|
+
const t = Math.max(viewportT, containerT)
|
|
100
|
+
const b = Math.min(viewportB, containerB)
|
|
101
|
+
const h = (b - t)
|
|
102
|
+
const y = (t - containerT)
|
|
103
|
+
const boundary = (containerH - targetH)
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
Math.max(0, Math.min(boundary, y + ((h / 2) - (targetH / 2))))
|
|
107
|
+
)
|
|
242
108
|
}
|