@twistedandy/better-carousel 1.1.0 → 1.1.1
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/README.md +7 -7
- package/dist/better-carousel.js +198 -190
- package/dist/better-carousel.min.js +1 -1
- package/package.json +1 -1
- package/src/scripts/autoplay.js +17 -23
- package/src/scripts/carousel.js +42 -20
- package/src/scripts/geometry.js +34 -39
- package/src/scripts/input.js +39 -53
- package/src/scripts/layout.js +18 -24
- package/src/scripts/markup.js +37 -9
- package/src/scripts/motion.js +3 -2
- package/src/scripts/navigation.js +15 -24
package/dist/better-carousel.js
CHANGED
|
@@ -54,6 +54,17 @@ function cleanStyle(element) {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Make a function that adds or removes event listeners on a target.
|
|
59
|
+
*
|
|
60
|
+
* @param {EventTarget} target
|
|
61
|
+
* @param {Array<Array>} list [name, handler, options] for each listener.
|
|
62
|
+
* @returns {(on?: boolean) => void} With true, it adds the listeners. Without a value, it removes them.
|
|
63
|
+
*/
|
|
64
|
+
function bind(target, list) {
|
|
65
|
+
return (on) => list.forEach(([name, handler, options]) => target[on ? 'addEventListener' : 'removeEventListener'](name, handler, options));
|
|
66
|
+
}
|
|
67
|
+
|
|
57
68
|
/**
|
|
58
69
|
* Make an element.
|
|
59
70
|
*
|
|
@@ -69,6 +80,24 @@ function create(tag, className) {
|
|
|
69
80
|
return element;
|
|
70
81
|
}
|
|
71
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Make a control button of a carousel.
|
|
85
|
+
*
|
|
86
|
+
* @param {string} className
|
|
87
|
+
* @param {HTMLElement} container The container of the slides, which the button controls.
|
|
88
|
+
* @param {string} label The aria-label. An empty label adds no attribute.
|
|
89
|
+
* @param {() => void} click
|
|
90
|
+
* @returns {HTMLButtonElement}
|
|
91
|
+
*/
|
|
92
|
+
function createButton(className, container, label, click) {
|
|
93
|
+
const element = create('button', className);
|
|
94
|
+
element.type = 'button';
|
|
95
|
+
setAttribute(element, 'aria-label', label);
|
|
96
|
+
element.setAttribute('aria-controls', container.id);
|
|
97
|
+
element.addEventListener('click', click);
|
|
98
|
+
return element;
|
|
99
|
+
}
|
|
100
|
+
|
|
72
101
|
/**
|
|
73
102
|
* Find the element of an option, such as "arrows", "dots" or "sync". true gives the root, and false gives null.
|
|
74
103
|
* The library searches for a selector in the root and then in each parent of the root, so that it finds a heading in the section of the carousel.
|
|
@@ -233,10 +262,9 @@ function readSlides(container, selector) {
|
|
|
233
262
|
function copySlides(slides, count, className) {
|
|
234
263
|
const list = [];
|
|
235
264
|
|
|
236
|
-
for (let
|
|
265
|
+
for (let pass = 0; pass < count; pass++) {
|
|
237
266
|
for (const node of slides) {
|
|
238
|
-
const clone = node
|
|
239
|
-
removeIds(clone);
|
|
267
|
+
const clone = copy(node);
|
|
240
268
|
clone.setAttribute('aria-hidden', 'true');
|
|
241
269
|
clone.inert = true;
|
|
242
270
|
clone.style.translate = '';
|
|
@@ -250,11 +278,13 @@ function copySlides(slides, count, className) {
|
|
|
250
278
|
return list;
|
|
251
279
|
}
|
|
252
280
|
|
|
253
|
-
// An id must be unique in the page, so
|
|
254
|
-
function
|
|
255
|
-
|
|
281
|
+
// Copy a node with its content. An id must be unique in the page, so the copy has no ids.
|
|
282
|
+
function copy(node) {
|
|
283
|
+
const result = node.cloneNode(true);
|
|
284
|
+
for (const item of [result, ...result.querySelectorAll('[id]')]) {
|
|
256
285
|
item.removeAttribute('id');
|
|
257
286
|
}
|
|
287
|
+
return result;
|
|
258
288
|
}
|
|
259
289
|
|
|
260
290
|
/**
|
|
@@ -264,9 +294,7 @@ function removeIds(node) {
|
|
|
264
294
|
* @param {HTMLElement} node The slide.
|
|
265
295
|
*/
|
|
266
296
|
function copyContent(clone, node) {
|
|
267
|
-
|
|
268
|
-
removeIds(copy);
|
|
269
|
-
clone.replaceChildren(...copy.childNodes);
|
|
297
|
+
clone.replaceChildren(...copy(node).childNodes);
|
|
270
298
|
}
|
|
271
299
|
|
|
272
300
|
/**
|
|
@@ -366,17 +394,13 @@ function pauser(root, options, change) {
|
|
|
366
394
|
function controller(context, key, options, hooks) {
|
|
367
395
|
const { api, root, container, cls, emit } = context,
|
|
368
396
|
labels = context.options.labels,
|
|
369
|
-
button =
|
|
397
|
+
button = createButton(cls.button + ' ' + cls.playButton, container, '', () => playing ? control.stop() : control.play());
|
|
370
398
|
|
|
371
399
|
let playing = false,
|
|
372
400
|
watch = null,
|
|
373
401
|
// true until the first init
|
|
374
402
|
first = true;
|
|
375
403
|
|
|
376
|
-
button.type = 'button';
|
|
377
|
-
button.setAttribute('aria-controls', container.id);
|
|
378
|
-
button.addEventListener('click', () => playing ? control.stop() : control.play());
|
|
379
|
-
|
|
380
404
|
function update() {
|
|
381
405
|
setAttribute(button, 'aria-label', playing ? labels.pause : labels.play);
|
|
382
406
|
setClass(root, cls.playing, playing);
|
|
@@ -529,8 +553,14 @@ function autoScroll(context) {
|
|
|
529
553
|
pressed = false;
|
|
530
554
|
|
|
531
555
|
const control = controller(context, 'autoScroll', options, {
|
|
532
|
-
play
|
|
556
|
+
play() {
|
|
557
|
+
presses(true);
|
|
558
|
+
wait();
|
|
559
|
+
},
|
|
533
560
|
stop() {
|
|
561
|
+
presses();
|
|
562
|
+
listen();
|
|
563
|
+
pressed = false;
|
|
534
564
|
wait(-1);
|
|
535
565
|
context.halt();
|
|
536
566
|
},
|
|
@@ -576,36 +606,28 @@ function autoScroll(context) {
|
|
|
576
606
|
}
|
|
577
607
|
}
|
|
578
608
|
|
|
579
|
-
//
|
|
580
|
-
|
|
609
|
+
// While the auto scroll plays, a press stops the slides under the pointer. A release without a drag starts them again after the delay.
|
|
610
|
+
// A drag is an interaction.
|
|
611
|
+
const listen = bind(window, [['pointerup', release], ['pointercancel', release]]),
|
|
612
|
+
presses = bind(context.viewport, [['pointerdown', press]]);
|
|
581
613
|
|
|
582
614
|
function press(event) {
|
|
583
|
-
if (!event.button &&
|
|
615
|
+
if (!event.button && !pressed) {
|
|
584
616
|
pressed = true;
|
|
585
617
|
wait(-1);
|
|
586
618
|
context.halt();
|
|
587
|
-
listen(
|
|
619
|
+
listen(true);
|
|
588
620
|
}
|
|
589
621
|
}
|
|
590
622
|
|
|
591
623
|
function release() {
|
|
592
624
|
pressed = false;
|
|
593
|
-
listen(
|
|
594
|
-
if (
|
|
625
|
+
listen();
|
|
626
|
+
if (!context.dragging()) {
|
|
595
627
|
wait();
|
|
596
628
|
}
|
|
597
629
|
}
|
|
598
630
|
|
|
599
|
-
const { viewport } = context,
|
|
600
|
-
remove = control.remove;
|
|
601
|
-
|
|
602
|
-
viewport.addEventListener('pointerdown', press);
|
|
603
|
-
control.remove = () => {
|
|
604
|
-
viewport.removeEventListener('pointerdown', press);
|
|
605
|
-
listen('removeEventListener');
|
|
606
|
-
remove();
|
|
607
|
-
};
|
|
608
|
-
|
|
609
631
|
return control;
|
|
610
632
|
}
|
|
611
633
|
|
|
@@ -702,6 +724,9 @@ function normalize(location, length) {
|
|
|
702
724
|
*/
|
|
703
725
|
|
|
704
726
|
|
|
727
|
+
// Short names of the Math functions, which the minified file can make shorter
|
|
728
|
+
const { abs, floor, round } = Math;
|
|
729
|
+
|
|
705
730
|
/**
|
|
706
731
|
* Keep a value between two limits.
|
|
707
732
|
*
|
|
@@ -820,6 +845,29 @@ function groups(list, view, count) {
|
|
|
820
845
|
return result;
|
|
821
846
|
}
|
|
822
847
|
|
|
848
|
+
/**
|
|
849
|
+
* Find the item with the lowest score.
|
|
850
|
+
*
|
|
851
|
+
* @param {Array} list
|
|
852
|
+
* @param {(item: *, index: number) => number} score Infinity excludes an item.
|
|
853
|
+
* @param {number} [margin] A score must be lower than the best score by more than this value. Then the lower index wins a tie.
|
|
854
|
+
* @returns {number} The index, or -1 if all items are excluded.
|
|
855
|
+
*/
|
|
856
|
+
function lowest(list, score, margin = 0) {
|
|
857
|
+
let best = -1,
|
|
858
|
+
gap = Infinity;
|
|
859
|
+
|
|
860
|
+
list.forEach((item, index) => {
|
|
861
|
+
const value = score(item, index);
|
|
862
|
+
if (value < gap - margin) {
|
|
863
|
+
gap = value;
|
|
864
|
+
best = index;
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
return best;
|
|
869
|
+
}
|
|
870
|
+
|
|
823
871
|
/**
|
|
824
872
|
* Get the distance between two locations. In loop mode, use the shortest distance modulo the period.
|
|
825
873
|
*
|
|
@@ -830,7 +878,7 @@ function groups(list, view, count) {
|
|
|
830
878
|
*/
|
|
831
879
|
function distance(a, b, period) {
|
|
832
880
|
const value = a - b;
|
|
833
|
-
return period ? value -
|
|
881
|
+
return period ? value - round(value / period) * period : value;
|
|
834
882
|
}
|
|
835
883
|
|
|
836
884
|
/**
|
|
@@ -864,8 +912,8 @@ function snaps({ slides, view, align, slidesPerSnap, containScroll, period = 0 }
|
|
|
864
912
|
}
|
|
865
913
|
|
|
866
914
|
if (period || containScroll !== 'keepSnaps') {
|
|
867
|
-
list = list.filter((location, index) => !index ||
|
|
868
|
-
if (period && list.length > 1 &&
|
|
915
|
+
list = list.filter((location, index) => !index || abs(distance(location, list[index - 1], period)) > 1);
|
|
916
|
+
if (period && list.length > 1 && abs(distance(list[0], list[list.length - 1], period)) <= 1) {
|
|
869
917
|
list.pop();
|
|
870
918
|
}
|
|
871
919
|
}
|
|
@@ -911,18 +959,7 @@ function snaps({ slides, view, align, slidesPerSnap, containScroll, period = 0 }
|
|
|
911
959
|
* @returns {number} The slide index, or -1 if the range is empty.
|
|
912
960
|
*/
|
|
913
961
|
function activeSlide(slides, location, view, align, period, from = 0, to = slides.length) {
|
|
914
|
-
|
|
915
|
-
gap = Infinity;
|
|
916
|
-
|
|
917
|
-
slides.forEach((slide, index) => {
|
|
918
|
-
const value = Math.abs(distance(slide.start + location, alignOffset(align, view, slide.end - slide.start, index), period));
|
|
919
|
-
if (index >= from && index < to && !slide.hidden && value < gap - 0.5) {
|
|
920
|
-
gap = value;
|
|
921
|
-
best = index;
|
|
922
|
-
}
|
|
923
|
-
});
|
|
924
|
-
|
|
925
|
-
return best;
|
|
962
|
+
return lowest(slides, (slide, index) => index >= from && index < to && !slide.hidden ? abs(distance(slide.start + location, alignOffset(align, view, slide.end - slide.start, index), period)) : Infinity, 0.5);
|
|
926
963
|
}
|
|
927
964
|
|
|
928
965
|
/**
|
|
@@ -956,18 +993,7 @@ function snapOfSlide(list, slides, index, view, align, period) {
|
|
|
956
993
|
* @returns {number} The snap index.
|
|
957
994
|
*/
|
|
958
995
|
function nearestSnap(list, location, period) {
|
|
959
|
-
|
|
960
|
-
gap = Infinity;
|
|
961
|
-
|
|
962
|
-
list.forEach((snap, index) => {
|
|
963
|
-
const value = Math.abs(distance(snap.location, location, period));
|
|
964
|
-
if (value < gap) {
|
|
965
|
-
gap = value;
|
|
966
|
-
best = index;
|
|
967
|
-
}
|
|
968
|
-
});
|
|
969
|
-
|
|
970
|
-
return best;
|
|
996
|
+
return Math.max(0, lowest(list, (snap) => abs(distance(snap.location, location, period))));
|
|
971
997
|
}
|
|
972
998
|
|
|
973
999
|
/**
|
|
@@ -989,7 +1015,7 @@ function targetLocation(target, location, period, direction = 0) {
|
|
|
989
1015
|
}
|
|
990
1016
|
|
|
991
1017
|
if (direction < 0) {
|
|
992
|
-
return target + (
|
|
1018
|
+
return target + (floor((location + 0.5 - target) / period) + 1) * period;
|
|
993
1019
|
}
|
|
994
1020
|
|
|
995
1021
|
return location + distance(target, location, period);
|
|
@@ -1005,7 +1031,7 @@ function targetLocation(target, location, period, direction = 0) {
|
|
|
1005
1031
|
function progress(location, { min, max, period }) {
|
|
1006
1032
|
if (period) {
|
|
1007
1033
|
const value = (max - location) / period;
|
|
1008
|
-
return value -
|
|
1034
|
+
return value - floor(value);
|
|
1009
1035
|
}
|
|
1010
1036
|
return max > min ? clamp((max - location) / (max - min), 0, 1) : 0;
|
|
1011
1037
|
}
|
|
@@ -1111,16 +1137,7 @@ function compute(layout, rects, area, count, options) {
|
|
|
1111
1137
|
* @returns {number} The snap index.
|
|
1112
1138
|
*/
|
|
1113
1139
|
function physicalSnap(geo, index, location) {
|
|
1114
|
-
|
|
1115
|
-
gap = Infinity;
|
|
1116
|
-
|
|
1117
|
-
geo.snaps.forEach((snap, item) => {
|
|
1118
|
-
const value = Math.abs(distance(snap.location, location, geo.period));
|
|
1119
|
-
if (geo.map[item] === index && value < gap) {
|
|
1120
|
-
gap = value;
|
|
1121
|
-
best = item;
|
|
1122
|
-
}
|
|
1123
|
-
});
|
|
1140
|
+
const best = lowest(geo.snaps, (snap, item) => geo.map[item] === index ? abs(distance(snap.location, location, geo.period)) : Infinity);
|
|
1124
1141
|
|
|
1125
1142
|
return best < 0 ? clamp(index, 0, geo.snaps.length - 1) : best;
|
|
1126
1143
|
}
|
|
@@ -1131,6 +1148,7 @@ function physicalSnap(geo, index, location) {
|
|
|
1131
1148
|
* The reads run in the ResizeObserver callback, when the layout is clean.
|
|
1132
1149
|
*/
|
|
1133
1150
|
|
|
1151
|
+
|
|
1134
1152
|
/**
|
|
1135
1153
|
* Read the axis, the alignment, the direction and the gap from the computed style of the container.
|
|
1136
1154
|
*
|
|
@@ -1194,13 +1212,13 @@ function readRects(viewport, nodes, layout, written) {
|
|
|
1194
1212
|
width = viewport.offsetWidth,
|
|
1195
1213
|
height = viewport.offsetHeight,
|
|
1196
1214
|
// offsetWidth is a whole number: a difference of less than 1 px is not a transform
|
|
1197
|
-
transformed = width && height && (
|
|
1215
|
+
transformed = width && height && (abs(box.width - width) >= 1 || abs(box.height - height) >= 1),
|
|
1198
1216
|
// A scale s and a rotation by an angle a give a box of s(w cos a + h sin a) by s(w sin a + h cos a). The main axis gets s cos a.
|
|
1199
|
-
scale = !transformed ? 1 :
|
|
1217
|
+
scale = !transformed ? 1 : abs(width - height) > 1 ? (width * box.width - height * box.height) / (width * width - height * height) : box.width / width,
|
|
1200
1218
|
view = !transformed ? (x ? box.width : box.height) : x ? width : height,
|
|
1201
1219
|
style = getComputedStyle(viewport),
|
|
1202
|
-
|
|
1203
|
-
pad = { start: parseFloat(style['padding' +
|
|
1220
|
+
side = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'],
|
|
1221
|
+
pad = { start: parseFloat(style['padding' + side[0]]) || 0, end: parseFloat(style['padding' + side[1]]) || 0 };
|
|
1204
1222
|
|
|
1205
1223
|
const slides = nodes.map((node, index) => {
|
|
1206
1224
|
const rect = node.getBoundingClientRect(),
|
|
@@ -1211,35 +1229,27 @@ function readRects(viewport, nodes, layout, written) {
|
|
|
1211
1229
|
return { start, end: start + size, cross: (x ? rect.height : rect.width) / scale, hidden: !rect.width && !rect.height };
|
|
1212
1230
|
});
|
|
1213
1231
|
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
} else {
|
|
1220
|
-
edge = slide.end;
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
|
|
1224
|
-
let first = Math.max(0, slides.findIndex((slide) => !slide.hidden)),
|
|
1225
|
-
last = first;
|
|
1232
|
+
// A hidden slide goes to the end of the visible slide before it. The first and the last visible slides give the margins of the loop gap.
|
|
1233
|
+
let edge = slides.find((slide) => !slide.hidden)?.start || 0,
|
|
1234
|
+
first = -1,
|
|
1235
|
+
last = -1,
|
|
1236
|
+
gap = layout.gap;
|
|
1226
1237
|
|
|
1227
1238
|
slides.forEach((slide, index) => {
|
|
1228
1239
|
if (slide.hidden) {
|
|
1240
|
+
slide.start = slide.end = edge;
|
|
1229
1241
|
return;
|
|
1230
1242
|
}
|
|
1231
|
-
|
|
1243
|
+
edge = slide.end;
|
|
1244
|
+
if (first < 0 || slide.start < slides[first].start) {
|
|
1232
1245
|
first = index;
|
|
1233
1246
|
}
|
|
1234
|
-
if (slide.end > slides[last].end) {
|
|
1247
|
+
if (last < 0 || slide.end > slides[last].end) {
|
|
1235
1248
|
last = index;
|
|
1236
1249
|
}
|
|
1237
1250
|
});
|
|
1238
1251
|
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
if (slides.length) {
|
|
1242
|
-
const side = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'];
|
|
1252
|
+
if (first > -1) {
|
|
1243
1253
|
gap += (parseFloat(getComputedStyle(nodes[first])['margin' + side[0]]) || 0) + (parseFloat(getComputedStyle(nodes[last])['margin' + side[1]]) || 0);
|
|
1244
1254
|
}
|
|
1245
1255
|
|
|
@@ -1279,9 +1289,9 @@ function visibleArea(viewport, layout, view, pad, size) {
|
|
|
1279
1289
|
}
|
|
1280
1290
|
|
|
1281
1291
|
const box = viewport.getBoundingClientRect(),
|
|
1282
|
-
before = Math.max(0,
|
|
1292
|
+
before = Math.max(0, round(layout.axis === 'x' ? (layout.rtl ? size - box.right : box.left) : box.top)) + pad.start;
|
|
1283
1293
|
|
|
1284
|
-
return { start: -before, size: before + Math.max(view + pad.end,
|
|
1294
|
+
return { start: -before, size: before + Math.max(view + pad.end, round(size - before)) };
|
|
1285
1295
|
}
|
|
1286
1296
|
|
|
1287
1297
|
// One ResizeObserver for all carousels. Each callback first reads the layout of all carousels, then writes. In this way, the reads of one carousel do not
|
|
@@ -1415,10 +1425,10 @@ function bezier([x1, y1, x2, y2]) {
|
|
|
1415
1425
|
for (let i = 0; i < 6; i++) {
|
|
1416
1426
|
const error = axis(x1, x2, t) - x,
|
|
1417
1427
|
slope = 3 * (1 + 3 * x1 - 3 * x2) * t * t + 2 * (3 * x2 - 6 * x1) * t + 3 * x1;
|
|
1418
|
-
if (
|
|
1428
|
+
if (abs(error) < 1e-5) {
|
|
1419
1429
|
return axis(y1, y2, t);
|
|
1420
1430
|
}
|
|
1421
|
-
if (
|
|
1431
|
+
if (abs(slope) < 1e-6) {
|
|
1422
1432
|
break;
|
|
1423
1433
|
}
|
|
1424
1434
|
t -= error / slope;
|
|
@@ -1879,12 +1889,11 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1879
1889
|
|
|
1880
1890
|
const api = {
|
|
1881
1891
|
goToSnap: (index, instant) => go(physical(index), instant),
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
goToPrev: (instant) => geo ? api.canGoToPrev() && step(selected, -1, instant) : go(selected - 1, instant),
|
|
1892
|
+
goToNext: (instant) => advance(1, instant),
|
|
1893
|
+
goToPrev: (instant) => advance(-1, instant),
|
|
1885
1894
|
goToSlide: (index, instant) => go(physical(api.snapOfSlide(index)), instant, index),
|
|
1886
|
-
canGoToNext: () =>
|
|
1887
|
-
canGoToPrev: () =>
|
|
1895
|
+
canGoToNext: () => can(1),
|
|
1896
|
+
canGoToPrev: () => can(-1),
|
|
1888
1897
|
selectedSnap: () => logical(selected),
|
|
1889
1898
|
previousSnap: () => logical(previous),
|
|
1890
1899
|
selectedSlide,
|
|
@@ -2127,11 +2136,11 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2127
2136
|
* @param {MutationRecord[]} records
|
|
2128
2137
|
*/
|
|
2129
2138
|
function onCopy(records) {
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2139
|
+
slides.forEach((node, index) => {
|
|
2140
|
+
if (records.some(({ type, target }) => node.contains(target) && (type !== 'attributes' || target !== node))) {
|
|
2141
|
+
for (let item = index; item < clones.length; item += slides.length) {
|
|
2142
|
+
copyContent(clones[item], node);
|
|
2143
|
+
}
|
|
2135
2144
|
}
|
|
2136
2145
|
});
|
|
2137
2146
|
}
|
|
@@ -2228,7 +2237,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2228
2237
|
values = [layout.axis, layout.rtl, typeof layout.align === 'string' && layout.align, rects.view, rects.gap, area.start, area.size, ...rects.slides.flatMap((slide) => [slide.start, slide.end, slide.cross])];
|
|
2229
2238
|
|
|
2230
2239
|
// A change of less than 0.5 px is not a change. During a motion, the rects have small errors, because the browser and bezier() round the easing differently.
|
|
2231
|
-
if (ready && values.length === signature.length && values.every((value, index) => value === signature[index] ||
|
|
2240
|
+
if (ready && values.length === signature.length && values.every((value, index) => value === signature[index] || abs(value - signature[index]) < 0.5)) {
|
|
2232
2241
|
return false;
|
|
2233
2242
|
}
|
|
2234
2243
|
|
|
@@ -2249,8 +2258,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2249
2258
|
|
|
2250
2259
|
// Before the first measure pass, selected is the logical snap of the startSnap option, of goToSnap() or of goToNext().
|
|
2251
2260
|
// In loop mode, a step before the first snap goes to the last snap.
|
|
2252
|
-
|
|
2253
|
-
selected = keep >= 0 ? physical(api.snapOfSlide(keep)) : Math.min(total - 1, ready ? selected : physical(geo.period ? (selected % count + count) % count : Math.max(selected, 0)));
|
|
2261
|
+
selected = keep >= 0 ? physical(api.snapOfSlide(keep)) : Math.min(total - 1, ready ? selected : physical(snapIndex(selected, geo.logical.length, geo.period)));
|
|
2254
2262
|
previous = Math.min(previous, total - 1);
|
|
2255
2263
|
requested = keep;
|
|
2256
2264
|
if (!dragging) {
|
|
@@ -2357,7 +2365,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2357
2365
|
*/
|
|
2358
2366
|
function screen(value) {
|
|
2359
2367
|
const scale = devicePixelRatio || 1;
|
|
2360
|
-
return
|
|
2368
|
+
return round(value * scale) / scale * geo.sign;
|
|
2361
2369
|
}
|
|
2362
2370
|
|
|
2363
2371
|
/**
|
|
@@ -2401,7 +2409,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2401
2409
|
root.style.setProperty('--better-progress', progress(at, geo));
|
|
2402
2410
|
} else if (options.progress) {
|
|
2403
2411
|
const value = snapPosition(geo.snaps, at, geo.period),
|
|
2404
|
-
snap =
|
|
2412
|
+
snap = floor(value);
|
|
2405
2413
|
whole.progress = progress(at, geo);
|
|
2406
2414
|
whole.snap = logical(snap) + value - snap;
|
|
2407
2415
|
options.progress(whole, api);
|
|
@@ -2454,6 +2462,30 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2454
2462
|
return translate(screen(value)) || '0px';
|
|
2455
2463
|
}
|
|
2456
2464
|
|
|
2465
|
+
/**
|
|
2466
|
+
* Find if the carousel can go one snap in a direction.
|
|
2467
|
+
*
|
|
2468
|
+
* @param {number} direction 1 or -1.
|
|
2469
|
+
* @returns {boolean}
|
|
2470
|
+
*/
|
|
2471
|
+
function can(direction) {
|
|
2472
|
+
return !!geo && !geo.fit && (!!geo.period || (direction > 0 ? selected < geo.snaps.length - 1 : selected > 0));
|
|
2473
|
+
}
|
|
2474
|
+
|
|
2475
|
+
/**
|
|
2476
|
+
* Go one snap in a direction, if the carousel can. Before the first measure pass, keep the step in the selected snap.
|
|
2477
|
+
*
|
|
2478
|
+
* @param {number} direction 1 or -1.
|
|
2479
|
+
* @param {boolean} [instant]
|
|
2480
|
+
*/
|
|
2481
|
+
function advance(direction, instant) {
|
|
2482
|
+
if (!geo) {
|
|
2483
|
+
go(selected + direction, instant);
|
|
2484
|
+
} else if (can(direction)) {
|
|
2485
|
+
step(selected, direction, instant);
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2457
2489
|
/**
|
|
2458
2490
|
* Go to the snap after a snap in a direction. In loop mode, the first snap follows the last snap.
|
|
2459
2491
|
*
|
|
@@ -2485,7 +2517,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2485
2517
|
}
|
|
2486
2518
|
|
|
2487
2519
|
const target = targetLocation(geo.snaps[index].location, current(), geo.period, direction),
|
|
2488
|
-
span =
|
|
2520
|
+
span = abs(index - selected),
|
|
2489
2521
|
count = geo.period ? Math.min(span, geo.snaps.length - span) : span;
|
|
2490
2522
|
|
|
2491
2523
|
move(index, target, instant ? 0 : snapDuration(options.duration, count), EASE);
|
|
@@ -2502,7 +2534,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2502
2534
|
function move(index, target, duration, curve) {
|
|
2503
2535
|
const from = halt(),
|
|
2504
2536
|
picked = select(index),
|
|
2505
|
-
jump = !duration || geo.fit ||
|
|
2537
|
+
jump = !duration || geo.fit || abs(target - from) < 0.5;
|
|
2506
2538
|
|
|
2507
2539
|
location = target;
|
|
2508
2540
|
|
|
@@ -2542,10 +2574,10 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2542
2574
|
// Start in the first period, so that the location does not grow after each pause
|
|
2543
2575
|
location = normalize(from, geo.period);
|
|
2544
2576
|
motion.loop(location, direction, geo.period / speed, geo, nodes);
|
|
2545
|
-
} else if (
|
|
2577
|
+
} else if (abs(to - from) > 1) {
|
|
2546
2578
|
free = true;
|
|
2547
2579
|
location = to;
|
|
2548
|
-
motion.run(from, to,
|
|
2580
|
+
motion.run(from, to, abs(to - from) / speed, [0, 0, 1, 1], events.has('scroll'));
|
|
2549
2581
|
}
|
|
2550
2582
|
}
|
|
2551
2583
|
|
|
@@ -2664,10 +2696,10 @@ const MOMENTUM = 325;
|
|
|
2664
2696
|
* @returns {number}
|
|
2665
2697
|
*/
|
|
2666
2698
|
function releaseDuration(path, velocity, curve, fallback) {
|
|
2667
|
-
if (Math.sign(path) !== Math.sign(velocity) ||
|
|
2699
|
+
if (Math.sign(path) !== Math.sign(velocity) || abs(velocity) < 0.05) {
|
|
2668
2700
|
return fallback;
|
|
2669
2701
|
}
|
|
2670
|
-
return clamp(
|
|
2702
|
+
return clamp(abs(path) * curve[1] / curve[0] / abs(velocity), 200, 1200);
|
|
2671
2703
|
}
|
|
2672
2704
|
|
|
2673
2705
|
/**
|
|
@@ -2732,9 +2764,9 @@ function releaseTarget({ location, from, velocity, current, snaps, min, max, per
|
|
|
2732
2764
|
|
|
2733
2765
|
let direction = 0;
|
|
2734
2766
|
|
|
2735
|
-
if (
|
|
2767
|
+
if (abs(velocity) > 0.3) {
|
|
2736
2768
|
direction = velocity < 0 ? 1 : -1;
|
|
2737
|
-
} else if (
|
|
2769
|
+
} else if (abs(moved) > 20) {
|
|
2738
2770
|
direction = moved < 0 ? 1 : -1;
|
|
2739
2771
|
}
|
|
2740
2772
|
|
|
@@ -2769,7 +2801,7 @@ function drag(context) {
|
|
|
2769
2801
|
// The selected snap at the drag start
|
|
2770
2802
|
snap = 0;
|
|
2771
2803
|
|
|
2772
|
-
const listen = (
|
|
2804
|
+
const listen = bind(window, [['pointermove', handle], ['pointerup', handle], ['pointercancel', handle]]);
|
|
2773
2805
|
|
|
2774
2806
|
function down(event) {
|
|
2775
2807
|
block = false;
|
|
@@ -2793,7 +2825,7 @@ function drag(context) {
|
|
|
2793
2825
|
axis = { x: geo.axis === 'x', sign: geo.sign, scale: geo.scale };
|
|
2794
2826
|
start = [event.clientX, event.clientY];
|
|
2795
2827
|
samples = [];
|
|
2796
|
-
listen(
|
|
2828
|
+
listen(true);
|
|
2797
2829
|
}
|
|
2798
2830
|
|
|
2799
2831
|
function handle(event) {
|
|
@@ -2816,10 +2848,10 @@ function drag(context) {
|
|
|
2816
2848
|
|
|
2817
2849
|
if (!context.dragging()) {
|
|
2818
2850
|
// A touch move across the axis scrolls the page
|
|
2819
|
-
if (event.pointerType !== 'mouse' &&
|
|
2851
|
+
if (event.pointerType !== 'mouse' && abs(cross) > abs(main)) {
|
|
2820
2852
|
return end();
|
|
2821
2853
|
}
|
|
2822
|
-
if (
|
|
2854
|
+
if (abs(main) < options.dragThreshold) {
|
|
2823
2855
|
return;
|
|
2824
2856
|
}
|
|
2825
2857
|
// Stop the motion, and start the drag at the location where the carousel is now
|
|
@@ -2849,7 +2881,7 @@ function drag(context) {
|
|
|
2849
2881
|
* @param {boolean} [up] true for a pointerup. A pointercancel releases with no velocity.
|
|
2850
2882
|
*/
|
|
2851
2883
|
function end(up) {
|
|
2852
|
-
listen(
|
|
2884
|
+
listen();
|
|
2853
2885
|
pointer = null;
|
|
2854
2886
|
|
|
2855
2887
|
if (!context.dragging()) {
|
|
@@ -2880,18 +2912,14 @@ function drag(context) {
|
|
|
2880
2912
|
// Stop a text selection during a drag. A "user-select" rule on the dragging class does the same, but its change paints all layers of the carousel.
|
|
2881
2913
|
const select = (event) => context.dragging() && event.preventDefault();
|
|
2882
2914
|
|
|
2883
|
-
viewport
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
viewport.addEventListener('selectstart', select);
|
|
2915
|
+
const own = bind(viewport, [['pointerdown', down], ['click', click, true], ['dragstart', prevent], ['selectstart', select]]);
|
|
2916
|
+
|
|
2917
|
+
own(true);
|
|
2887
2918
|
|
|
2888
2919
|
return {
|
|
2889
2920
|
remove() {
|
|
2890
|
-
listen(
|
|
2891
|
-
|
|
2892
|
-
viewport.removeEventListener('click', click, true);
|
|
2893
|
-
viewport.removeEventListener('dragstart', prevent);
|
|
2894
|
-
viewport.removeEventListener('selectstart', select);
|
|
2921
|
+
listen();
|
|
2922
|
+
own();
|
|
2895
2923
|
}
|
|
2896
2924
|
};
|
|
2897
2925
|
}
|
|
@@ -2911,7 +2939,7 @@ function scrolls(node, viewport, axis, delta) {
|
|
|
2911
2939
|
for (; node && node !== viewport; node = node.parentElement) {
|
|
2912
2940
|
// Read the style only for an element with more content than space
|
|
2913
2941
|
if (node[size] > node[client] && /auto|scroll/.test(getComputedStyle(node)[overflow])) {
|
|
2914
|
-
const at =
|
|
2942
|
+
const at = abs(node[position]);
|
|
2915
2943
|
if (delta > 0 ? at + node[client] < node[size] - 1 : at > 0) {
|
|
2916
2944
|
return true;
|
|
2917
2945
|
}
|
|
@@ -2952,7 +2980,7 @@ function wheel(context) {
|
|
|
2952
2980
|
|
|
2953
2981
|
function onWheel(event) {
|
|
2954
2982
|
const geo = context.geo(),
|
|
2955
|
-
side = geo.axis === 'x' &&
|
|
2983
|
+
side = geo.axis === 'x' && abs(event.deltaX) > abs(event.deltaY),
|
|
2956
2984
|
delta = side ? event.deltaX * geo.sign : event.deltaY;
|
|
2957
2985
|
|
|
2958
2986
|
if (event.ctrlKey || !delta || context.dragging()) {
|
|
@@ -2961,22 +2989,19 @@ function wheel(context) {
|
|
|
2961
2989
|
|
|
2962
2990
|
const now = event.timeStamp,
|
|
2963
2991
|
gesture = now - time < 200,
|
|
2964
|
-
size =
|
|
2992
|
+
size = abs(delta),
|
|
2965
2993
|
// A delta that does not grow is the inertia of the gesture, not a new push
|
|
2966
2994
|
inertia = gesture && size <= last;
|
|
2967
2995
|
|
|
2996
|
+
time = now;
|
|
2968
2997
|
last = size;
|
|
2969
|
-
|
|
2970
|
-
held = false;
|
|
2971
|
-
}
|
|
2998
|
+
held &&= gesture;
|
|
2972
2999
|
|
|
2973
3000
|
// An element in a slide that can scroll in the wheel direction scrolls, also for the rest of its gesture
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
time = now;
|
|
3001
|
+
inner = (gesture && inner) || scrolls(event.target, viewport, side ? 'x' : 'y', side ? event.deltaX : delta);
|
|
3002
|
+
if (inner) {
|
|
2977
3003
|
return;
|
|
2978
3004
|
}
|
|
2979
|
-
inner = false;
|
|
2980
3005
|
|
|
2981
3006
|
const base = context.location(),
|
|
2982
3007
|
path = delta * (event.deltaMode === 1 ? 40 : event.deltaMode ? geo.view : 1);
|
|
@@ -2986,10 +3011,8 @@ function wheel(context) {
|
|
|
2986
3011
|
target = clamp(target, geo.min, geo.max);
|
|
2987
3012
|
}
|
|
2988
3013
|
|
|
2989
|
-
time = now;
|
|
2990
|
-
|
|
2991
3014
|
// At the end, the page scrolls. The inertia of a gesture that moved the carousel does not scroll the page.
|
|
2992
|
-
if (
|
|
3015
|
+
if (abs(target - base) < 0.5) {
|
|
2993
3016
|
if (held && inertia) {
|
|
2994
3017
|
event.preventDefault();
|
|
2995
3018
|
}
|
|
@@ -3018,23 +3041,23 @@ function wheel(context) {
|
|
|
3018
3041
|
const geo = context.geo(),
|
|
3019
3042
|
at = context.current(),
|
|
3020
3043
|
index = nearestSnap(geo.snaps, at, geo.period),
|
|
3021
|
-
away =
|
|
3044
|
+
away = abs(targetLocation(geo.snaps[index].location, at, geo.period) - at) > 10;
|
|
3022
3045
|
|
|
3023
3046
|
context.step(index, index === snap && away ? direction : 0);
|
|
3024
3047
|
}
|
|
3025
3048
|
|
|
3026
3049
|
// A wheel listener that is not passive makes the browser wait for the main thread before it scrolls the page.
|
|
3027
3050
|
// Add it only where the wheel moves the carousel.
|
|
3028
|
-
|
|
3029
|
-
const geo = context.geo();
|
|
3030
|
-
viewport[!geo.fit && (geo.axis === 'y' || options.wheel === 'both') ? 'addEventListener' : 'removeEventListener']('wheel', onWheel, { passive: false });
|
|
3031
|
-
}
|
|
3051
|
+
const listen = bind(viewport, [['wheel', onWheel, { passive: false }]]);
|
|
3032
3052
|
|
|
3033
|
-
['init', 'reinit', 'resize'].forEach((name) => context.on(name,
|
|
3053
|
+
['init', 'reinit', 'resize'].forEach((name) => context.on(name, () => {
|
|
3054
|
+
const geo = context.geo();
|
|
3055
|
+
listen(!geo.fit && (geo.axis === 'y' || options.wheel === 'both'));
|
|
3056
|
+
}));
|
|
3034
3057
|
|
|
3035
3058
|
return {
|
|
3036
3059
|
remove() {
|
|
3037
|
-
|
|
3060
|
+
listen();
|
|
3038
3061
|
clearTimeout(timer);
|
|
3039
3062
|
}
|
|
3040
3063
|
};
|
|
@@ -3067,13 +3090,11 @@ function keys(context) {
|
|
|
3067
3090
|
}
|
|
3068
3091
|
}
|
|
3069
3092
|
|
|
3070
|
-
root
|
|
3093
|
+
const listen = bind(root, [['keydown', onKey]]);
|
|
3071
3094
|
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
}
|
|
3076
|
-
};
|
|
3095
|
+
listen(true);
|
|
3096
|
+
|
|
3097
|
+
return { remove: listen };
|
|
3077
3098
|
}
|
|
3078
3099
|
|
|
3079
3100
|
/**
|
|
@@ -3107,15 +3128,11 @@ function focus(context) {
|
|
|
3107
3128
|
context.emit('slidefocus');
|
|
3108
3129
|
}
|
|
3109
3130
|
|
|
3110
|
-
container
|
|
3111
|
-
container.addEventListener('focusin', onFocus);
|
|
3131
|
+
const listen = bind(container, [['pointerdown', press], ['focusin', onFocus]]);
|
|
3112
3132
|
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
container.removeEventListener('focusin', onFocus);
|
|
3117
|
-
}
|
|
3118
|
-
};
|
|
3133
|
+
listen(true);
|
|
3134
|
+
|
|
3135
|
+
return { remove: listen };
|
|
3119
3136
|
}
|
|
3120
3137
|
|
|
3121
3138
|
/*
|
|
@@ -3158,14 +3175,7 @@ function nav(context) {
|
|
|
3158
3175
|
}
|
|
3159
3176
|
|
|
3160
3177
|
if (arrows) {
|
|
3161
|
-
|
|
3162
|
-
const button = create('button', cls.button + ' ' + cls[key + 'Button']);
|
|
3163
|
-
button.type = 'button';
|
|
3164
|
-
button.setAttribute('aria-label', labels[key]);
|
|
3165
|
-
button.setAttribute('aria-controls', context.container.id);
|
|
3166
|
-
button.addEventListener('click', () => api[method]());
|
|
3167
|
-
buttons.push(button);
|
|
3168
|
-
});
|
|
3178
|
+
buttons.push(createButton(cls.button + ' ' + cls.prevButton, context.container, labels.prev, () => api.goToPrev()), createButton(cls.button + ' ' + cls.nextButton, context.container, labels.next, () => api.goToNext()));
|
|
3169
3179
|
}
|
|
3170
3180
|
|
|
3171
3181
|
function update() {
|
|
@@ -3173,17 +3183,17 @@ function nav(context) {
|
|
|
3173
3183
|
count = fit ? 0 : api.snapProgress().length,
|
|
3174
3184
|
selected = api.selectedSnap();
|
|
3175
3185
|
|
|
3176
|
-
buttons.forEach((
|
|
3186
|
+
buttons.forEach((item, index) => {
|
|
3177
3187
|
if (fit) {
|
|
3178
|
-
|
|
3188
|
+
item.remove();
|
|
3179
3189
|
} else {
|
|
3180
|
-
if (!
|
|
3190
|
+
if (!item.isConnected) {
|
|
3181
3191
|
// Before the dots, for the tab order
|
|
3182
|
-
list?.parentNode === arrows ? list.before(
|
|
3192
|
+
list?.parentNode === arrows ? list.before(item) : arrows.append(item);
|
|
3183
3193
|
}
|
|
3184
3194
|
const disabled = !(index ? api.canGoToNext() : api.canGoToPrev());
|
|
3185
|
-
if (
|
|
3186
|
-
|
|
3195
|
+
if (item.disabled !== disabled) {
|
|
3196
|
+
item.disabled = disabled;
|
|
3187
3197
|
}
|
|
3188
3198
|
}
|
|
3189
3199
|
});
|
|
@@ -3203,16 +3213,12 @@ function nav(context) {
|
|
|
3203
3213
|
}
|
|
3204
3214
|
|
|
3205
3215
|
while (dots.length < count) {
|
|
3206
|
-
const
|
|
3207
|
-
|
|
3208
|
-
dot.type = 'button';
|
|
3209
|
-
dot.setAttribute('aria-label', labels.dot.replace('%d', index + 1));
|
|
3210
|
-
dot.setAttribute('aria-controls', context.container.id);
|
|
3216
|
+
const index = dots.length,
|
|
3217
|
+
dot = createButton(cls.dot, context.container, labels.dot.replace('%d', index + 1), () => api.goToSnap(index));
|
|
3211
3218
|
// The window of the dots hides the dots out of the zoom: keep them out of the tab order
|
|
3212
3219
|
if (windowed) {
|
|
3213
3220
|
dot.tabIndex = -1;
|
|
3214
3221
|
}
|
|
3215
|
-
dot.addEventListener('click', () => api.goToSnap(index));
|
|
3216
3222
|
dots.push(dot);
|
|
3217
3223
|
track.append(dot);
|
|
3218
3224
|
}
|
|
@@ -3264,7 +3270,7 @@ function nav(context) {
|
|
|
3264
3270
|
|
|
3265
3271
|
return {
|
|
3266
3272
|
remove() {
|
|
3267
|
-
buttons.forEach((
|
|
3273
|
+
buttons.forEach((item) => item.remove());
|
|
3268
3274
|
list?.remove();
|
|
3269
3275
|
}
|
|
3270
3276
|
};
|
|
@@ -3316,12 +3322,14 @@ function sync(context) {
|
|
|
3316
3322
|
|
|
3317
3323
|
context.on('init', connect);
|
|
3318
3324
|
context.on('reinit', connect);
|
|
3319
|
-
container
|
|
3325
|
+
const listen = bind(container, [['click', click]]);
|
|
3326
|
+
|
|
3327
|
+
listen(true);
|
|
3320
3328
|
|
|
3321
3329
|
return {
|
|
3322
3330
|
remove() {
|
|
3323
3331
|
other?.off('select', show);
|
|
3324
|
-
|
|
3332
|
+
listen();
|
|
3325
3333
|
}
|
|
3326
3334
|
};
|
|
3327
3335
|
}
|