@twistedandy/better-carousel 1.1.0 → 1.1.2
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 +14 -12
- package/dist/better-carousel.css +1 -1
- package/dist/better-carousel.js +375 -336
- package/dist/better-carousel.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +1 -8
- package/src/scripts/autoplay.js +22 -24
- package/src/scripts/carousel.js +109 -49
- package/src/scripts/geometry.js +45 -55
- package/src/scripts/input.js +47 -59
- package/src/scripts/layout.js +82 -86
- package/src/scripts/markup.js +50 -19
- package/src/scripts/motion.js +3 -2
- package/src/scripts/navigation.js +24 -42
- package/src/styles/better-carousel.scss +5 -0
- package/src/types/better-carousel.d.ts +4 -4
package/dist/better-carousel.js
CHANGED
|
@@ -34,12 +34,12 @@ function setAttribute(element, name, value) {
|
|
|
34
34
|
* Set an inline style property.
|
|
35
35
|
*
|
|
36
36
|
* @param {HTMLElement} element
|
|
37
|
-
* @param {string} name
|
|
38
|
-
* @param {string} value An empty string removes the property.
|
|
37
|
+
* @param {string} name The CSS name, such as 'touch-action' or '--better-dot'.
|
|
38
|
+
* @param {string|number} value An empty string removes the property.
|
|
39
39
|
*/
|
|
40
40
|
function setStyle(element, name, value) {
|
|
41
|
-
if (element.style
|
|
42
|
-
element.style
|
|
41
|
+
if (element.style.getPropertyValue(name) !== String(value)) {
|
|
42
|
+
element.style.setProperty(name, value);
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -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.
|
|
@@ -99,16 +128,14 @@ const marked = new WeakMap(),
|
|
|
99
128
|
*
|
|
100
129
|
* @param {Element} node
|
|
101
130
|
* @param {string} name
|
|
102
|
-
* @param {string} value
|
|
131
|
+
* @param {string} value An empty value removes the attribute, for example an empty label.
|
|
103
132
|
*/
|
|
104
133
|
function own(node, name, value) {
|
|
105
134
|
const list = marked.get(node) || new Set();
|
|
106
135
|
if (list.has(name) || !node.hasAttribute(name)) {
|
|
107
136
|
list.add(name);
|
|
108
137
|
marked.set(node, list);
|
|
109
|
-
|
|
110
|
-
node.setAttribute(name, value);
|
|
111
|
-
}
|
|
138
|
+
setAttribute(node, name, value);
|
|
112
139
|
}
|
|
113
140
|
}
|
|
114
141
|
|
|
@@ -223,7 +250,8 @@ function readSlides(container, selector) {
|
|
|
223
250
|
}
|
|
224
251
|
|
|
225
252
|
/**
|
|
226
|
-
* Make the clones: full copies of the slides, which the screen reader and the keyboard do not see.
|
|
253
|
+
* Make the clones: full copies of the slides, which the screen reader and the keyboard do not see. A clone is inert, so the pointer
|
|
254
|
+
* does not see it either: the carousel passes a click on a clone to its slide.
|
|
227
255
|
*
|
|
228
256
|
* @param {HTMLElement[]} slides
|
|
229
257
|
* @param {number} count The number of copies of all slides.
|
|
@@ -233,10 +261,9 @@ function readSlides(container, selector) {
|
|
|
233
261
|
function copySlides(slides, count, className) {
|
|
234
262
|
const list = [];
|
|
235
263
|
|
|
236
|
-
for (let
|
|
264
|
+
for (let pass = 0; pass < count; pass++) {
|
|
237
265
|
for (const node of slides) {
|
|
238
|
-
const clone = node
|
|
239
|
-
removeIds(clone);
|
|
266
|
+
const clone = copy(node);
|
|
240
267
|
clone.setAttribute('aria-hidden', 'true');
|
|
241
268
|
clone.inert = true;
|
|
242
269
|
clone.style.translate = '';
|
|
@@ -250,11 +277,17 @@ function copySlides(slides, count, className) {
|
|
|
250
277
|
return list;
|
|
251
278
|
}
|
|
252
279
|
|
|
253
|
-
// An id must be unique in the page, so
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
280
|
+
// Copy a node with its content. An id must be unique in the page, so the copy has no ids. A form field in the copy has no name and is not
|
|
281
|
+
// required: it does not join the radio group of its slide, the form does not send it, and the form check does not stop at it.
|
|
282
|
+
function copy(node) {
|
|
283
|
+
const result = node.cloneNode(true),
|
|
284
|
+
selects = node.querySelectorAll('select');
|
|
285
|
+
for (const item of [result, ...result.querySelectorAll('[id],[name],[required]')]) {
|
|
286
|
+
['id', 'name', 'required'].forEach((name) => item.removeAttribute(name));
|
|
257
287
|
}
|
|
288
|
+
// The copy keeps the values and the checks of the fields, but not the selected option
|
|
289
|
+
result.querySelectorAll('select').forEach((item, index) => item.selectedIndex = selects[index].selectedIndex);
|
|
290
|
+
return result;
|
|
258
291
|
}
|
|
259
292
|
|
|
260
293
|
/**
|
|
@@ -264,9 +297,7 @@ function removeIds(node) {
|
|
|
264
297
|
* @param {HTMLElement} node The slide.
|
|
265
298
|
*/
|
|
266
299
|
function copyContent(clone, node) {
|
|
267
|
-
|
|
268
|
-
removeIds(copy);
|
|
269
|
-
clone.replaceChildren(...copy.childNodes);
|
|
300
|
+
clone.replaceChildren(...copy(node).childNodes);
|
|
270
301
|
}
|
|
271
302
|
|
|
272
303
|
/**
|
|
@@ -366,17 +397,13 @@ function pauser(root, options, change) {
|
|
|
366
397
|
function controller(context, key, options, hooks) {
|
|
367
398
|
const { api, root, container, cls, emit } = context,
|
|
368
399
|
labels = context.options.labels,
|
|
369
|
-
button =
|
|
400
|
+
button = createButton(cls.button + ' ' + cls.playButton, container, '', () => playing ? control.stop() : control.play());
|
|
370
401
|
|
|
371
402
|
let playing = false,
|
|
372
403
|
watch = null,
|
|
373
404
|
// true until the first init
|
|
374
405
|
first = true;
|
|
375
406
|
|
|
376
|
-
button.type = 'button';
|
|
377
|
-
button.setAttribute('aria-controls', container.id);
|
|
378
|
-
button.addEventListener('click', () => playing ? control.stop() : control.play());
|
|
379
|
-
|
|
380
407
|
function update() {
|
|
381
408
|
setAttribute(button, 'aria-label', playing ? labels.pause : labels.play);
|
|
382
409
|
setClass(root, cls.playing, playing);
|
|
@@ -424,13 +451,17 @@ function controller(context, key, options, hooks) {
|
|
|
424
451
|
isPlaying: () => playing,
|
|
425
452
|
paused: () => !playing || watch.paused(),
|
|
426
453
|
remove() {
|
|
454
|
+
// Keep the play state for the setup after a breakpoint change. See reinit() in carousel.js.
|
|
455
|
+
if (context.playing) {
|
|
456
|
+
context.playing[key] = playing;
|
|
457
|
+
}
|
|
427
458
|
control.stop();
|
|
428
459
|
button.remove();
|
|
429
460
|
delete api[key];
|
|
430
461
|
}
|
|
431
462
|
};
|
|
432
463
|
|
|
433
|
-
[['init
|
|
464
|
+
[['init reinit resize', show], ...hooks.events].forEach(([names, handler]) => context.on(names, handler));
|
|
434
465
|
update();
|
|
435
466
|
api[key] = control;
|
|
436
467
|
|
|
@@ -529,8 +560,14 @@ function autoScroll(context) {
|
|
|
529
560
|
pressed = false;
|
|
530
561
|
|
|
531
562
|
const control = controller(context, 'autoScroll', options, {
|
|
532
|
-
play
|
|
563
|
+
play() {
|
|
564
|
+
presses(true);
|
|
565
|
+
wait();
|
|
566
|
+
},
|
|
533
567
|
stop() {
|
|
568
|
+
presses();
|
|
569
|
+
listen();
|
|
570
|
+
pressed = false;
|
|
534
571
|
wait(-1);
|
|
535
572
|
context.halt();
|
|
536
573
|
},
|
|
@@ -576,36 +613,28 @@ function autoScroll(context) {
|
|
|
576
613
|
}
|
|
577
614
|
}
|
|
578
615
|
|
|
579
|
-
//
|
|
580
|
-
|
|
616
|
+
// While the auto scroll plays, a press stops the slides under the pointer. A release without a drag starts them again after the delay.
|
|
617
|
+
// A drag is an interaction.
|
|
618
|
+
const listen = bind(window, [['pointerup', release], ['pointercancel', release]]),
|
|
619
|
+
presses = bind(context.viewport, [['pointerdown', press]]);
|
|
581
620
|
|
|
582
621
|
function press(event) {
|
|
583
|
-
if (!event.button &&
|
|
622
|
+
if (!event.button && !pressed) {
|
|
584
623
|
pressed = true;
|
|
585
624
|
wait(-1);
|
|
586
625
|
context.halt();
|
|
587
|
-
listen(
|
|
626
|
+
listen(true);
|
|
588
627
|
}
|
|
589
628
|
}
|
|
590
629
|
|
|
591
630
|
function release() {
|
|
592
631
|
pressed = false;
|
|
593
|
-
listen(
|
|
594
|
-
if (
|
|
632
|
+
listen();
|
|
633
|
+
if (!context.dragging()) {
|
|
595
634
|
wait();
|
|
596
635
|
}
|
|
597
636
|
}
|
|
598
637
|
|
|
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
638
|
return control;
|
|
610
639
|
}
|
|
611
640
|
|
|
@@ -702,6 +731,9 @@ function normalize(location, length) {
|
|
|
702
731
|
*/
|
|
703
732
|
|
|
704
733
|
|
|
734
|
+
// Short names of the Math functions, which the minified file can make shorter
|
|
735
|
+
const { abs, floor, round } = Math;
|
|
736
|
+
|
|
705
737
|
/**
|
|
706
738
|
* Keep a value between two limits.
|
|
707
739
|
*
|
|
@@ -820,6 +852,29 @@ function groups(list, view, count) {
|
|
|
820
852
|
return result;
|
|
821
853
|
}
|
|
822
854
|
|
|
855
|
+
/**
|
|
856
|
+
* Find the item with the lowest score.
|
|
857
|
+
*
|
|
858
|
+
* @param {Array} list
|
|
859
|
+
* @param {(item: *, index: number) => number} score Infinity excludes an item.
|
|
860
|
+
* @param {number} [margin] A score must be lower than the best score by more than this value. Then the lower index wins a tie.
|
|
861
|
+
* @returns {number} The index, or -1 if all items are excluded.
|
|
862
|
+
*/
|
|
863
|
+
function lowest(list, score, margin = 0) {
|
|
864
|
+
let best = -1,
|
|
865
|
+
gap = Infinity;
|
|
866
|
+
|
|
867
|
+
list.forEach((item, index) => {
|
|
868
|
+
const value = score(item, index);
|
|
869
|
+
if (value < gap - margin) {
|
|
870
|
+
gap = value;
|
|
871
|
+
best = index;
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
return best;
|
|
876
|
+
}
|
|
877
|
+
|
|
823
878
|
/**
|
|
824
879
|
* Get the distance between two locations. In loop mode, use the shortest distance modulo the period.
|
|
825
880
|
*
|
|
@@ -830,7 +885,7 @@ function groups(list, view, count) {
|
|
|
830
885
|
*/
|
|
831
886
|
function distance(a, b, period) {
|
|
832
887
|
const value = a - b;
|
|
833
|
-
return period ? value -
|
|
888
|
+
return period ? value - round(value / period) * period : value;
|
|
834
889
|
}
|
|
835
890
|
|
|
836
891
|
/**
|
|
@@ -864,8 +919,8 @@ function snaps({ slides, view, align, slidesPerSnap, containScroll, period = 0 }
|
|
|
864
919
|
}
|
|
865
920
|
|
|
866
921
|
if (period || containScroll !== 'keepSnaps') {
|
|
867
|
-
list = list.filter((location, index) => !index ||
|
|
868
|
-
if (period && list.length > 1 &&
|
|
922
|
+
list = list.filter((location, index) => !index || abs(distance(location, list[index - 1], period)) > 1);
|
|
923
|
+
if (period && list.length > 1 && abs(distance(list[0], list[list.length - 1], period)) <= 1) {
|
|
869
924
|
list.pop();
|
|
870
925
|
}
|
|
871
926
|
}
|
|
@@ -911,32 +966,17 @@ function snaps({ slides, view, align, slidesPerSnap, containScroll, period = 0 }
|
|
|
911
966
|
* @returns {number} The slide index, or -1 if the range is empty.
|
|
912
967
|
*/
|
|
913
968
|
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;
|
|
969
|
+
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
970
|
}
|
|
927
971
|
|
|
928
972
|
/**
|
|
929
973
|
* Find the snap of a slide: the snap whose active slide it is, or the snap nearest to the raw location of the slide.
|
|
930
974
|
*
|
|
931
|
-
* @param {Array<{location: number, slide: number}
|
|
932
|
-
* @param {Array<{start: number, end: number}>} slides
|
|
975
|
+
* @param {{snaps: Array<{location: number, slide: number}>, slides: Array<{start: number, end: number}>, view: number, align: string|Function, period?: number}} geo
|
|
933
976
|
* @param {number} index The slide index.
|
|
934
|
-
* @param {number} view
|
|
935
|
-
* @param {string|Function} align
|
|
936
|
-
* @param {number} [period]
|
|
937
977
|
* @returns {number} The snap index.
|
|
938
978
|
*/
|
|
939
|
-
function snapOfSlide(list, slides,
|
|
979
|
+
function snapOfSlide({ snaps: list, slides, view, align, period }, index) {
|
|
940
980
|
const found = list.findIndex((snap) => snap.slide === index),
|
|
941
981
|
slide = slides[index];
|
|
942
982
|
|
|
@@ -956,18 +996,7 @@ function snapOfSlide(list, slides, index, view, align, period) {
|
|
|
956
996
|
* @returns {number} The snap index.
|
|
957
997
|
*/
|
|
958
998
|
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;
|
|
999
|
+
return Math.max(0, lowest(list, (snap) => abs(distance(snap.location, location, period))));
|
|
971
1000
|
}
|
|
972
1001
|
|
|
973
1002
|
/**
|
|
@@ -989,7 +1018,7 @@ function targetLocation(target, location, period, direction = 0) {
|
|
|
989
1018
|
}
|
|
990
1019
|
|
|
991
1020
|
if (direction < 0) {
|
|
992
|
-
return target + (
|
|
1021
|
+
return target + (floor((location + 0.5 - target) / period) + 1) * period;
|
|
993
1022
|
}
|
|
994
1023
|
|
|
995
1024
|
return location + distance(target, location, period);
|
|
@@ -1005,7 +1034,7 @@ function targetLocation(target, location, period, direction = 0) {
|
|
|
1005
1034
|
function progress(location, { min, max, period }) {
|
|
1006
1035
|
if (period) {
|
|
1007
1036
|
const value = (max - location) / period;
|
|
1008
|
-
return value -
|
|
1037
|
+
return value - floor(value);
|
|
1009
1038
|
}
|
|
1010
1039
|
return max > min ? clamp((max - location) / (max - min), 0, 1) : 0;
|
|
1011
1040
|
}
|
|
@@ -1013,14 +1042,12 @@ function progress(location, { min, max, period }) {
|
|
|
1013
1042
|
/**
|
|
1014
1043
|
* Find the slides that show at a location.
|
|
1015
1044
|
*
|
|
1016
|
-
* @param {Array<{start: number, end: number}
|
|
1045
|
+
* @param {{slides: Array<{start: number, end: number}>, view: number, period?: number}} geo
|
|
1017
1046
|
* @param {number} location
|
|
1018
|
-
* @param {number} view
|
|
1019
1047
|
* @param {number} threshold The part of a slide that must show, from 0 to 1. With 0, any part is enough.
|
|
1020
|
-
* @param {number} [period]
|
|
1021
1048
|
* @returns {number[]} The slide indexes.
|
|
1022
1049
|
*/
|
|
1023
|
-
function inView(slides,
|
|
1050
|
+
function inView({ slides, view, period }, location, threshold) {
|
|
1024
1051
|
const result = [];
|
|
1025
1052
|
|
|
1026
1053
|
slides.forEach((slide, index) => {
|
|
@@ -1067,26 +1094,27 @@ function snapPosition(list, location, period) {
|
|
|
1067
1094
|
* With clones, a logical snap is a snap whose active slide is an original slide.
|
|
1068
1095
|
*
|
|
1069
1096
|
* @param {object} layout The result of readLayout().
|
|
1070
|
-
* @param {{view: number, slides: Array<{start: number, end: number}>, gap: number}} rects The result of readRects().
|
|
1071
|
-
*
|
|
1097
|
+
* @param {{view: number, slides: Array<{start: number, end: number}>, gap: number, area: {start: number, size: number}}} rects The result of readRects().
|
|
1098
|
+
* The slides after the first count slides are clones.
|
|
1072
1099
|
* @param {number} count The number of original slides.
|
|
1073
1100
|
* @param {object} options The options of the carousel.
|
|
1074
1101
|
* @returns {object} The geometry. If the carousel needs a different number of clones, only { copies }: the number of copies of all slides.
|
|
1075
1102
|
* map gives the logical snap of each snap.
|
|
1076
1103
|
*/
|
|
1077
|
-
function compute(layout, rects,
|
|
1104
|
+
function compute(layout, rects, count, options) {
|
|
1078
1105
|
const content = bounds(rects.slides.slice(0, count)),
|
|
1079
|
-
|
|
1106
|
+
// The auto scroll with the loop never fits, unless no slide shows, for example in a hidden tab
|
|
1107
|
+
fit = content.size <= rects.view + 1 && !(options.autoScroll && options.loop && content.max),
|
|
1080
1108
|
loop = options.loop && !fit && count,
|
|
1081
1109
|
// The loop period: the distance from the first slide start to the same point in the next copy of the slides
|
|
1082
1110
|
period = loop ? bounds(rects.slides).size + rects.gap : 0,
|
|
1083
|
-
copies = loop && options.clones ? cloneCount(content.size + rects.gap, area.size, content.max) : 0;
|
|
1111
|
+
copies = loop && options.clones ? cloneCount(content.size + rects.gap, rects.area.size, content.max) : 0;
|
|
1084
1112
|
|
|
1085
1113
|
if (copies * count !== rects.slides.length - count) {
|
|
1086
1114
|
return { copies };
|
|
1087
1115
|
}
|
|
1088
1116
|
|
|
1089
|
-
const geo = { ...layout, ...rects,
|
|
1117
|
+
const geo = { ...layout, ...rects, fit, count, period, min: 0, max: 0 };
|
|
1090
1118
|
|
|
1091
1119
|
if (fit) {
|
|
1092
1120
|
geo.snaps = [{ location: 0, slide: Math.max(0, activeSlide(rects.slides, 0, rects.view, layout.align)) }];
|
|
@@ -1111,16 +1139,7 @@ function compute(layout, rects, area, count, options) {
|
|
|
1111
1139
|
* @returns {number} The snap index.
|
|
1112
1140
|
*/
|
|
1113
1141
|
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
|
-
});
|
|
1142
|
+
const best = lowest(geo.snaps, (snap, item) => geo.map[item] === index ? abs(distance(snap.location, location, geo.period)) : Infinity);
|
|
1124
1143
|
|
|
1125
1144
|
return best < 0 ? clamp(index, 0, geo.snaps.length - 1) : best;
|
|
1126
1145
|
}
|
|
@@ -1131,6 +1150,7 @@ function physicalSnap(geo, index, location) {
|
|
|
1131
1150
|
* The reads run in the ResizeObserver callback, when the layout is clean.
|
|
1132
1151
|
*/
|
|
1133
1152
|
|
|
1153
|
+
|
|
1134
1154
|
/**
|
|
1135
1155
|
* Read the axis, the alignment, the direction and the gap from the computed style of the container.
|
|
1136
1156
|
*
|
|
@@ -1173,19 +1193,21 @@ function readLayout(container, options) {
|
|
|
1173
1193
|
|
|
1174
1194
|
/**
|
|
1175
1195
|
* Read the slide boxes on the main axis, relative to the inner edge of the viewport padding at location 0. The snaps align to this edge.
|
|
1176
|
-
* The values are in the px of the carousel: in a parent with a CSS scale, such as a modal window that opens with a scale animation,
|
|
1177
|
-
* values do not change
|
|
1196
|
+
* The values are in the px of the carousel: in a parent with a CSS scale or zoom, such as a modal window that opens with a scale animation,
|
|
1197
|
+
* the values do not change. A rotation of a parent is not supported.
|
|
1178
1198
|
* A slide with no box, such as a slide with display: none, is hidden. It gets no snap, and it stays at the end of the slide before it.
|
|
1179
1199
|
*
|
|
1180
1200
|
* @param {HTMLElement} viewport
|
|
1181
1201
|
* @param {HTMLElement[]} nodes All slides, clones included.
|
|
1182
1202
|
* @param {object} layout The result of readLayout().
|
|
1183
1203
|
* @param {{axis: string, container: number, slides: number[]}} written The translate values that the library wrote, in px on the screen.
|
|
1184
|
-
* @
|
|
1204
|
+
* @param {number} [size] The window size on the main axis, for clones: 'fill'.
|
|
1205
|
+
* @returns {{view: number, slides: Array<{start: number, end: number, cross: number, hidden: boolean}>, gap: number, scale: number, area: {start: number, size: number}}}
|
|
1185
1206
|
* view is the viewport size without the padding. gap is the loop gap: the space between the last slide and the next copy of the first slide.
|
|
1186
|
-
* scale is the screen px of one px of the carousel.
|
|
1207
|
+
* scale is the screen px of one px of the carousel. area is the part of the main axis where the loop must show slides: the viewport with its
|
|
1208
|
+
* padding, or with a size, the window, so that the slides fill it when the viewport overflow shows.
|
|
1187
1209
|
*/
|
|
1188
|
-
function readRects(viewport, nodes, layout, written) {
|
|
1210
|
+
function readRects(viewport, nodes, layout, written, size) {
|
|
1189
1211
|
const box = viewport.getBoundingClientRect(),
|
|
1190
1212
|
x = layout.axis === 'x',
|
|
1191
1213
|
sign = layout.sign,
|
|
@@ -1193,14 +1215,12 @@ function readRects(viewport, nodes, layout, written) {
|
|
|
1193
1215
|
shift = known ? written.container : 0,
|
|
1194
1216
|
width = viewport.offsetWidth,
|
|
1195
1217
|
height = viewport.offsetHeight,
|
|
1196
|
-
// offsetWidth is a whole number: a difference of less than 1 px is not a
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
scale = !transformed ? 1 : Math.abs(width - height) > 1 ? (width * box.width - height * box.height) / (width * width - height * height) : box.width / width,
|
|
1200
|
-
view = !transformed ? (x ? box.width : box.height) : x ? width : height,
|
|
1218
|
+
// offsetWidth is a whole number: a difference of less than 1 px is not a scale
|
|
1219
|
+
scale = width && height && (abs(box.width - width) >= 1 || abs(box.height - height) >= 1) ? (x ? box.width / width : box.height / height) : 1,
|
|
1220
|
+
view = (x ? box.width : box.height) / scale,
|
|
1201
1221
|
style = getComputedStyle(viewport),
|
|
1202
|
-
|
|
1203
|
-
pad = { start: parseFloat(style['padding' +
|
|
1222
|
+
side = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'],
|
|
1223
|
+
pad = { start: parseFloat(style['padding' + side[0]]) || 0, end: parseFloat(style['padding' + side[1]]) || 0 };
|
|
1204
1224
|
|
|
1205
1225
|
const slides = nodes.map((node, index) => {
|
|
1206
1226
|
const rect = node.getBoundingClientRect(),
|
|
@@ -1211,44 +1231,47 @@ function readRects(viewport, nodes, layout, written) {
|
|
|
1211
1231
|
return { start, end: start + size, cross: (x ? rect.height : rect.width) / scale, hidden: !rect.width && !rect.height };
|
|
1212
1232
|
});
|
|
1213
1233
|
|
|
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;
|
|
1234
|
+
// 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.
|
|
1235
|
+
let edge = slides.find((slide) => !slide.hidden)?.start || 0,
|
|
1236
|
+
first = -1,
|
|
1237
|
+
last = -1,
|
|
1238
|
+
gap = layout.gap;
|
|
1226
1239
|
|
|
1227
1240
|
slides.forEach((slide, index) => {
|
|
1228
1241
|
if (slide.hidden) {
|
|
1242
|
+
slide.start = slide.end = edge;
|
|
1229
1243
|
return;
|
|
1230
1244
|
}
|
|
1231
|
-
|
|
1245
|
+
edge = slide.end;
|
|
1246
|
+
if (first < 0 || slide.start < slides[first].start) {
|
|
1232
1247
|
first = index;
|
|
1233
1248
|
}
|
|
1234
|
-
if (slide.end > slides[last].end) {
|
|
1249
|
+
if (last < 0 || slide.end > slides[last].end) {
|
|
1235
1250
|
last = index;
|
|
1236
1251
|
}
|
|
1237
1252
|
});
|
|
1238
1253
|
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
if (slides.length) {
|
|
1242
|
-
const side = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'];
|
|
1254
|
+
if (first > -1) {
|
|
1243
1255
|
gap += (parseFloat(getComputedStyle(nodes[first])['margin' + side[0]]) || 0) + (parseFloat(getComputedStyle(nodes[last])['margin' + side[1]]) || 0);
|
|
1244
1256
|
}
|
|
1245
1257
|
|
|
1258
|
+
const inner = view - pad.start - pad.end;
|
|
1259
|
+
|
|
1260
|
+
// The area before and after the inner edge of the viewport padding: the padding, or the window
|
|
1261
|
+
let before = pad.start,
|
|
1262
|
+
after = inner + pad.end;
|
|
1263
|
+
|
|
1264
|
+
if (size) {
|
|
1265
|
+
before += Math.max(0, round(x ? (layout.rtl ? size - box.right : box.left) : box.top));
|
|
1266
|
+
after = Math.max(after, round(size - before));
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1246
1269
|
return {
|
|
1247
|
-
view:
|
|
1270
|
+
view: inner,
|
|
1248
1271
|
slides,
|
|
1249
1272
|
gap,
|
|
1250
1273
|
scale,
|
|
1251
|
-
|
|
1274
|
+
area: { start: -before, size: before + after }
|
|
1252
1275
|
};
|
|
1253
1276
|
}
|
|
1254
1277
|
|
|
@@ -1262,28 +1285,6 @@ function windowSize(axis) {
|
|
|
1262
1285
|
return axis === 'x' ? document.documentElement.clientWidth : innerHeight;
|
|
1263
1286
|
}
|
|
1264
1287
|
|
|
1265
|
-
/**
|
|
1266
|
-
* Get the part of the main axis where the loop must show slides, relative to the inner edge of the viewport padding.
|
|
1267
|
-
* It is the viewport with its padding. With clones: 'fill', it is the window, so that the slides fill it when the viewport overflow shows.
|
|
1268
|
-
*
|
|
1269
|
-
* @param {HTMLElement} viewport
|
|
1270
|
-
* @param {{axis: string, rtl: boolean}} layout
|
|
1271
|
-
* @param {number} view The viewport size without the padding.
|
|
1272
|
-
* @param {{start: number, end: number}} pad The viewport padding.
|
|
1273
|
-
* @param {number} [size] The window size on the main axis, for clones: 'fill'. Without it, the area is the viewport.
|
|
1274
|
-
* @returns {{start: number, size: number}}
|
|
1275
|
-
*/
|
|
1276
|
-
function visibleArea(viewport, layout, view, pad, size) {
|
|
1277
|
-
if (!size) {
|
|
1278
|
-
return { start: -pad.start, size: pad.start + view + pad.end };
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
const box = viewport.getBoundingClientRect(),
|
|
1282
|
-
before = Math.max(0, Math.round(layout.axis === 'x' ? (layout.rtl ? size - box.right : box.left) : box.top)) + pad.start;
|
|
1283
|
-
|
|
1284
|
-
return { start: -before, size: before + Math.max(view + pad.end, Math.round(size - before)) };
|
|
1285
|
-
}
|
|
1286
|
-
|
|
1287
1288
|
// 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
|
|
1288
1289
|
// come after the writes of another carousel, and the browser does one layout for all of them.
|
|
1289
1290
|
const owners = new WeakMap(),
|
|
@@ -1303,14 +1304,13 @@ let resizer;
|
|
|
1303
1304
|
*/
|
|
1304
1305
|
function observeSize(nodes, owner) {
|
|
1305
1306
|
resizer ||= new ResizeObserver((entries) => {
|
|
1306
|
-
const
|
|
1307
|
+
const changed = [...new Set(entries.filter((entry) => {
|
|
1307
1308
|
const { inlineSize, blockSize } = entry.borderBoxSize[0],
|
|
1308
1309
|
size = inlineSize + ',' + blockSize,
|
|
1309
1310
|
same = sizes.get(entry.target) === size;
|
|
1310
1311
|
sizes.set(entry.target, size);
|
|
1311
1312
|
return !same && !owners.get(entry.target)?.skip(entry);
|
|
1312
|
-
}).map((entry) => owners.get(entry.target)));
|
|
1313
|
-
const changed = [...list].filter((item) => item && item.read());
|
|
1313
|
+
}).map((entry) => owners.get(entry.target)))].filter((item) => item?.read());
|
|
1314
1314
|
changed.forEach((item) => item.write());
|
|
1315
1315
|
// A write can change the size of an element that the observer watches, for example the auto height or the clones. If the observer has
|
|
1316
1316
|
// a change to report after its callback, the browser sends the "ResizeObserver loop" error to window.onerror. Stop the watch of these
|
|
@@ -1337,53 +1337,67 @@ function observeSize(nodes, owner) {
|
|
|
1337
1337
|
}
|
|
1338
1338
|
}
|
|
1339
1339
|
|
|
1340
|
-
// The lazy option: one IntersectionObserver for each margin, the callback of each element that waits, and the
|
|
1340
|
+
// The lazy option: one IntersectionObserver for each margin, the callback of each element that waits, and the elements to check: [node, lazy, margin]
|
|
1341
1341
|
const watchers = new Map(),
|
|
1342
|
-
waiters = new WeakMap()
|
|
1343
|
-
boxes = new WeakMap();
|
|
1342
|
+
waiters = new WeakMap();
|
|
1344
1343
|
|
|
1345
|
-
|
|
1346
|
-
* Read the boxes of elements before a carousel writes to the DOM. Then the reads of all carousels cause one layout, not one for each carousel.
|
|
1347
|
-
* waitNear() uses each box one time.
|
|
1348
|
-
*
|
|
1349
|
-
* @param {Element[]} elements
|
|
1350
|
-
*/
|
|
1351
|
-
function readBoxes(elements) {
|
|
1352
|
-
elements.forEach((element) => boxes.set(element, element.getBoundingClientRect()));
|
|
1353
|
-
}
|
|
1344
|
+
let queue = [];
|
|
1354
1345
|
|
|
1355
1346
|
/**
|
|
1356
|
-
* Wait until an element comes near the screen.
|
|
1347
|
+
* Wait until an element comes near the screen. One microtask reads the boxes of all elements that started to wait, after the writes of
|
|
1348
|
+
* the carousels that started without a wait. Then the reads cause one layout, not one for each carousel. The microtask runs before the
|
|
1349
|
+
* first paint, so a carousel near the screen starts before the page shows, and it does not move the page.
|
|
1357
1350
|
*
|
|
1358
1351
|
* @param {Element} node
|
|
1359
1352
|
* @param {true|number} lazy The "lazy" option: true for one screen height, or a distance in px.
|
|
1360
1353
|
* @param {Function} callback
|
|
1361
|
-
* @returns {Function
|
|
1354
|
+
* @returns {Function} A function that stops the wait.
|
|
1362
1355
|
*/
|
|
1363
1356
|
function waitNear(node, lazy, callback) {
|
|
1364
|
-
const
|
|
1365
|
-
rect = boxes.get(node) || node.getBoundingClientRect(),
|
|
1366
|
-
margin = lazy === true ? '100%' : lazy + 'px';
|
|
1367
|
-
|
|
1368
|
-
boxes.delete(node);
|
|
1369
|
-
|
|
1370
|
-
if (rect.height && rect.bottom >= -near && rect.top <= innerHeight + near) {
|
|
1371
|
-
return null;
|
|
1372
|
-
}
|
|
1357
|
+
const margin = lazy === true ? '100%' : lazy + 'px';
|
|
1373
1358
|
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
observer = new IntersectionObserver((entries) => entries.forEach((entry) => entry.isIntersecting && waiters.get(entry.target)?.()), { rootMargin: margin });
|
|
1377
|
-
watchers.set(margin, observer);
|
|
1359
|
+
if (!queue.length) {
|
|
1360
|
+
queueMicrotask(check);
|
|
1378
1361
|
}
|
|
1362
|
+
queue.push([node, lazy, margin]);
|
|
1379
1363
|
waiters.set(node, callback);
|
|
1380
|
-
|
|
1364
|
+
|
|
1381
1365
|
return () => {
|
|
1382
|
-
|
|
1366
|
+
watchers.get(margin)?.unobserve(node);
|
|
1383
1367
|
waiters.delete(node);
|
|
1384
1368
|
};
|
|
1385
1369
|
}
|
|
1386
1370
|
|
|
1371
|
+
// Call the callback of each element near the screen, and watch the other elements. An element whose wait stopped has no callback.
|
|
1372
|
+
function check() {
|
|
1373
|
+
const list = queue,
|
|
1374
|
+
rects = list.map(([node]) => node.getBoundingClientRect());
|
|
1375
|
+
|
|
1376
|
+
queue = [];
|
|
1377
|
+
|
|
1378
|
+
list.forEach(([node, lazy, margin], index) => {
|
|
1379
|
+
const near = lazy === true ? innerHeight : lazy,
|
|
1380
|
+
rect = rects[index],
|
|
1381
|
+
callback = waiters.get(node);
|
|
1382
|
+
|
|
1383
|
+
if (!callback) {
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
if (rect.height && rect.bottom >= -near && rect.top <= innerHeight + near) {
|
|
1388
|
+
callback();
|
|
1389
|
+
return;
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
let observer = watchers.get(margin);
|
|
1393
|
+
if (!observer) {
|
|
1394
|
+
observer = new IntersectionObserver((entries) => entries.forEach((entry) => entry.isIntersecting && waiters.get(entry.target)?.()), { rootMargin: margin });
|
|
1395
|
+
watchers.set(margin, observer);
|
|
1396
|
+
}
|
|
1397
|
+
observer.observe(node);
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1387
1401
|
/*
|
|
1388
1402
|
* Motion: the easing, the duration of a goToSnap() motion, and the motion engine.
|
|
1389
1403
|
*/
|
|
@@ -1415,10 +1429,10 @@ function bezier([x1, y1, x2, y2]) {
|
|
|
1415
1429
|
for (let i = 0; i < 6; i++) {
|
|
1416
1430
|
const error = axis(x1, x2, t) - x,
|
|
1417
1431
|
slope = 3 * (1 + 3 * x1 - 3 * x2) * t * t + 2 * (3 * x2 - 6 * x1) * t + 3 * x1;
|
|
1418
|
-
if (
|
|
1432
|
+
if (abs(error) < 1e-5) {
|
|
1419
1433
|
return axis(y1, y2, t);
|
|
1420
1434
|
}
|
|
1421
|
-
if (
|
|
1435
|
+
if (abs(slope) < 1e-6) {
|
|
1422
1436
|
break;
|
|
1423
1437
|
}
|
|
1424
1438
|
t -= error / slope;
|
|
@@ -1781,6 +1795,9 @@ function breakpoints(base, change) {
|
|
|
1781
1795
|
// The instance of each root, viewport and container
|
|
1782
1796
|
const registry = new WeakMap();
|
|
1783
1797
|
|
|
1798
|
+
// true if the browser supports the pan-up and pan-down values of touch-action
|
|
1799
|
+
let directional;
|
|
1800
|
+
|
|
1784
1801
|
/**
|
|
1785
1802
|
* Get the carousel of a root, a viewport or a container.
|
|
1786
1803
|
*
|
|
@@ -1791,7 +1808,7 @@ const find = (element) => registry.get(element);
|
|
|
1791
1808
|
|
|
1792
1809
|
/**
|
|
1793
1810
|
* Make an event emitter. A handler gets (api, name), as in Embla.
|
|
1794
|
-
* The handlers of the features run before the handlers of the page, and clear() removes them.
|
|
1811
|
+
* The handlers of the features run before the handlers of the page, and clear() removes them. listen() takes event names with spaces between them.
|
|
1795
1812
|
*
|
|
1796
1813
|
* @param {object} api
|
|
1797
1814
|
* @returns {{on: Function, off: Function, listen: Function, clear: Function, emit: Function, has: Function}}
|
|
@@ -1810,7 +1827,7 @@ function emitter(api) {
|
|
|
1810
1827
|
user[name]?.delete(handler);
|
|
1811
1828
|
return api;
|
|
1812
1829
|
},
|
|
1813
|
-
listen: (
|
|
1830
|
+
listen: (names, handler) => names.split(' ').forEach((name) => add(own, name, handler)),
|
|
1814
1831
|
clear: () => Object.values(own).forEach((set) => set.clear()),
|
|
1815
1832
|
emit(name) {
|
|
1816
1833
|
own[name]?.forEach((handler) => handler(api, name));
|
|
@@ -1879,16 +1896,15 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1879
1896
|
|
|
1880
1897
|
const api = {
|
|
1881
1898
|
goToSnap: (index, instant) => go(physical(index), instant),
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
goToPrev: (instant) => geo ? api.canGoToPrev() && step(selected, -1, instant) : go(selected - 1, instant),
|
|
1899
|
+
goToNext: (instant) => advance(1, instant),
|
|
1900
|
+
goToPrev: (instant) => advance(-1, instant),
|
|
1885
1901
|
goToSlide: (index, instant) => go(physical(api.snapOfSlide(index)), instant, index),
|
|
1886
|
-
canGoToNext: () =>
|
|
1887
|
-
canGoToPrev: () =>
|
|
1902
|
+
canGoToNext: () => can(1),
|
|
1903
|
+
canGoToPrev: () => can(-1),
|
|
1888
1904
|
selectedSnap: () => logical(selected),
|
|
1889
1905
|
previousSnap: () => logical(previous),
|
|
1890
1906
|
selectedSlide,
|
|
1891
|
-
snapOfSlide: (index) => geo ? logical(snapOfSlide(geo
|
|
1907
|
+
snapOfSlide: (index) => geo ? logical(snapOfSlide(geo, index)) : index,
|
|
1892
1908
|
snapProgress: () => geo ? geo.logical.map((index) => progress(geo.snaps[index].location, geo)) : [],
|
|
1893
1909
|
progress: () => geo ? progress(current(), geo) : 0,
|
|
1894
1910
|
slideNodes: () => slides.slice(),
|
|
@@ -1912,17 +1928,19 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1912
1928
|
each = { node: null, index: 0, offset: 0, distance: 0, size: 0, visible: 0 },
|
|
1913
1929
|
/**
|
|
1914
1930
|
* What the features share: the API, the events, and the state and the actions of the carousel.
|
|
1915
|
-
* setup() adds root, viewport, container, cls and options. reinit() adds playing:
|
|
1931
|
+
* setup() adds root, viewport, container, cls and options. reinit() adds playing: an object where the autoplay and the auto scroll
|
|
1932
|
+
* keep their play state for the next setup after a breakpoint change.
|
|
1916
1933
|
*/
|
|
1917
1934
|
context = {
|
|
1918
1935
|
api,
|
|
1919
1936
|
emit: events.emit,
|
|
1920
|
-
// Listen to
|
|
1937
|
+
// Listen to events, such as 'init reinit', until the teardown
|
|
1921
1938
|
on: events.listen,
|
|
1939
|
+
// Get the carousel of an element, for the sync option
|
|
1940
|
+
find,
|
|
1922
1941
|
|
|
1923
1942
|
// The state
|
|
1924
1943
|
geo: () => geo,
|
|
1925
|
-
nodes: () => nodes,
|
|
1926
1944
|
selected: () => selected,
|
|
1927
1945
|
dragging: () => dragging,
|
|
1928
1946
|
// The location at rest or at the end of the motion. During a free motion, the location now.
|
|
@@ -1959,8 +1977,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1959
1977
|
api.on = events.on;
|
|
1960
1978
|
api.off = events.off;
|
|
1961
1979
|
|
|
1962
|
-
// The options of the first setup. The lazy option:
|
|
1963
|
-
// a start after the first paint adds the arrows and the dots in view, and this moves the page (CLS).
|
|
1980
|
+
// The options of the first setup. The lazy option: the carousel waits, and waitNear() starts it before the first paint if it is near the screen.
|
|
1964
1981
|
let resolved = resolve(element, defaults, userOptions),
|
|
1965
1982
|
waiting = resolved.lazy ? waitNear(element, resolved.lazy, reinit) : null;
|
|
1966
1983
|
|
|
@@ -2007,6 +2024,8 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2007
2024
|
}
|
|
2008
2025
|
|
|
2009
2026
|
viewport.addEventListener('scroll', onScroll);
|
|
2027
|
+
viewport.addEventListener('click', onClick);
|
|
2028
|
+
viewport.addEventListener('input', onInput);
|
|
2010
2029
|
if (options.loop && options.clones === 'fill') {
|
|
2011
2030
|
addEventListener('resize', onWindow);
|
|
2012
2031
|
}
|
|
@@ -2043,21 +2062,23 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2043
2062
|
dragging = free = false;
|
|
2044
2063
|
setClones(0);
|
|
2045
2064
|
viewport.removeEventListener('scroll', onScroll);
|
|
2065
|
+
viewport.removeEventListener('click', onClick);
|
|
2066
|
+
viewport.removeEventListener('input', onInput);
|
|
2046
2067
|
removeEventListener('resize', onWindow);
|
|
2047
2068
|
|
|
2048
2069
|
for (const node of nodes) {
|
|
2049
2070
|
setStyle(node, 'translate', '');
|
|
2050
|
-
node
|
|
2071
|
+
setStyle(node, '--better-offset', '');
|
|
2051
2072
|
setClass(node, cls.selected, false);
|
|
2052
2073
|
setClass(node, cls.inView, false);
|
|
2053
2074
|
cleanStyle(node);
|
|
2054
2075
|
}
|
|
2055
2076
|
|
|
2056
2077
|
setStyle(container, 'translate', '');
|
|
2057
|
-
setStyle(container, '
|
|
2058
|
-
setStyle(viewport, '
|
|
2078
|
+
setStyle(container, 'will-change', '');
|
|
2079
|
+
setStyle(viewport, 'touch-action', '');
|
|
2059
2080
|
setStyle(viewport, 'height', '');
|
|
2060
|
-
root
|
|
2081
|
+
setStyle(root, '--better-progress', '');
|
|
2061
2082
|
|
|
2062
2083
|
for (const name of ['horizontal', 'vertical', 'fit', 'dragging']) {
|
|
2063
2084
|
setClass(root, cls[name], false);
|
|
@@ -2091,7 +2112,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2091
2112
|
}
|
|
2092
2113
|
const keep = geo ? selectedSlide() : requested;
|
|
2093
2114
|
// After a breakpoint change, the new autoplay and auto scroll keep the play state. New options start them again.
|
|
2094
|
-
context.playing = newOptions ? null : {
|
|
2115
|
+
context.playing = newOptions ? null : {};
|
|
2095
2116
|
teardown(true);
|
|
2096
2117
|
setup();
|
|
2097
2118
|
requested = keep;
|
|
@@ -2122,20 +2143,27 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2122
2143
|
}
|
|
2123
2144
|
|
|
2124
2145
|
/**
|
|
2125
|
-
* Copy a changed slide into its clones
|
|
2146
|
+
* Copy a changed slide into its clones, for example after the page changed a text or an image source. The carousel writes the classes
|
|
2147
|
+
* of the slides, so a change of a slide's own attributes does not count. A style change does not count: a "progress" or an "offset"
|
|
2148
|
+
* callback can write the styles of the elements in the slides on each frame.
|
|
2126
2149
|
*
|
|
2127
|
-
* @param {MutationRecord[]} records
|
|
2150
|
+
* @param {MutationRecord[]} [records] With no records, copy all slides.
|
|
2128
2151
|
*/
|
|
2129
2152
|
function onCopy(records) {
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2153
|
+
slides.forEach((node, index) => {
|
|
2154
|
+
if (!records || records.some(({ type, target }) => node.contains(target) && (type !== 'attributes' || target !== node))) {
|
|
2155
|
+
for (let item = index; item < clones.length; item += slides.length) {
|
|
2156
|
+
copyContent(clones[item], node);
|
|
2157
|
+
}
|
|
2135
2158
|
}
|
|
2136
2159
|
});
|
|
2137
2160
|
}
|
|
2138
2161
|
|
|
2162
|
+
// A form field changes its value or its check with no mutation. A checked radio also changes the radios of its group in other slides, so copy all slides.
|
|
2163
|
+
function onInput() {
|
|
2164
|
+
onCopy();
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2139
2167
|
/**
|
|
2140
2168
|
* Watch the size of the viewport, the container and the slides.
|
|
2141
2169
|
*
|
|
@@ -2217,18 +2245,17 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2217
2245
|
function measure() {
|
|
2218
2246
|
const sign = geo?.sign || 1,
|
|
2219
2247
|
at = motion.current(),
|
|
2220
|
-
layout = readLayout(container, options)
|
|
2221
|
-
// The translate values that show now: the written values, or the values of a motion
|
|
2222
|
-
rects = readRects(viewport, nodes, layout, at === null ? written : { axis: written.axis, container: sign * at, slides: motion.shifts(sign) || written.slides });
|
|
2248
|
+
layout = readLayout(container, options);
|
|
2223
2249
|
|
|
2224
2250
|
// With clones: 'fill', the loop fills the window
|
|
2225
2251
|
lastWindow = options.loop && options.clones === 'fill' ? windowSize(layout.axis) : 0;
|
|
2226
2252
|
|
|
2227
|
-
|
|
2228
|
-
|
|
2253
|
+
// The translate values that show now: the written values, or the values of a motion
|
|
2254
|
+
const rects = readRects(viewport, nodes, layout, at === null ? written : { axis: written.axis, container: sign * at, slides: motion.shifts(sign) || written.slides }, lastWindow),
|
|
2255
|
+
values = [layout.axis, layout.rtl, typeof layout.align === 'string' && layout.align, rects.view, rects.gap, rects.area.start, rects.area.size, ...rects.slides.flatMap((slide) => [slide.start, slide.end, slide.cross])];
|
|
2229
2256
|
|
|
2230
2257
|
// 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] ||
|
|
2258
|
+
if (ready && values.length === signature.length && values.every((value, index) => value === signature[index] || abs(value - signature[index]) < 0.5)) {
|
|
2232
2259
|
return false;
|
|
2233
2260
|
}
|
|
2234
2261
|
|
|
@@ -2236,7 +2263,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2236
2263
|
|
|
2237
2264
|
// After a change of the slides, onMutation() gives the slide to keep
|
|
2238
2265
|
const keep = ready && !changed ? selectedSlide() : requested,
|
|
2239
|
-
next = compute(layout, rects,
|
|
2266
|
+
next = compute(layout, rects, slides.length, options);
|
|
2240
2267
|
|
|
2241
2268
|
if (!next.snaps) {
|
|
2242
2269
|
pending = next.copies;
|
|
@@ -2249,8 +2276,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2249
2276
|
|
|
2250
2277
|
// Before the first measure pass, selected is the logical snap of the startSnap option, of goToSnap() or of goToNext().
|
|
2251
2278
|
// 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)));
|
|
2279
|
+
selected = keep >= 0 ? physical(api.snapOfSlide(keep)) : Math.min(total - 1, ready ? selected : physical(snapIndex(selected, geo.logical.length, geo.period)));
|
|
2254
2280
|
previous = Math.min(previous, total - 1);
|
|
2255
2281
|
requested = keep;
|
|
2256
2282
|
if (!dragging) {
|
|
@@ -2325,8 +2351,13 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2325
2351
|
setClass(root, cls.horizontal, x);
|
|
2326
2352
|
setClass(root, cls.vertical, !x);
|
|
2327
2353
|
setClass(root, cls.fit, geo.fit);
|
|
2328
|
-
|
|
2329
|
-
|
|
2354
|
+
// A touch can scroll the page across the axis. At the first or the last snap of a vertical carousel, it can also scroll the page up
|
|
2355
|
+
// or down. Safari has no pan-up and pan-down, so there the carousel keeps the touch.
|
|
2356
|
+
const first = !can(-1),
|
|
2357
|
+
last = !can(1),
|
|
2358
|
+
edge = !x && (first || last) && (directional ??= CSS.supports('touch-action', 'pan-up')) ? (first && last ? 'pan-y ' : first ? 'pan-up ' : 'pan-down ') : '';
|
|
2359
|
+
setStyle(viewport, 'touch-action', geo.fit ? '' : x ? 'pan-y pinch-zoom' : 'pan-x ' + edge + 'pinch-zoom');
|
|
2360
|
+
setStyle(container, 'will-change', geo.fit ? '' : 'translate');
|
|
2330
2361
|
|
|
2331
2362
|
if (!keep) {
|
|
2332
2363
|
write(geo.fit ? 0 : location);
|
|
@@ -2334,11 +2365,11 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2334
2365
|
|
|
2335
2366
|
// Auto height: the height of the tallest slide in view at the target location
|
|
2336
2367
|
if (options.autoHeight) {
|
|
2337
|
-
setStyle(viewport, 'height', x ? Math.max(...inView(geo
|
|
2368
|
+
setStyle(viewport, 'height', x ? Math.max(...inView(geo, location, 0).map((index) => geo.slides[index].cross)) + 'px' : '');
|
|
2338
2369
|
}
|
|
2339
2370
|
|
|
2340
2371
|
const chosen = selectedSlide(),
|
|
2341
|
-
list = [...new Set(inView(geo
|
|
2372
|
+
list = [...new Set(inView(geo, location, options.inViewThreshold).map(original))].sort((a, b) => a - b);
|
|
2342
2373
|
|
|
2343
2374
|
nodes.forEach((node, index) => setClass(node, cls.selected, original(index) === chosen));
|
|
2344
2375
|
|
|
@@ -2357,7 +2388,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2357
2388
|
*/
|
|
2358
2389
|
function screen(value) {
|
|
2359
2390
|
const scale = devicePixelRatio || 1;
|
|
2360
|
-
return
|
|
2391
|
+
return round(value * scale) / scale * geo.sign;
|
|
2361
2392
|
}
|
|
2362
2393
|
|
|
2363
2394
|
/**
|
|
@@ -2384,9 +2415,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2384
2415
|
written.container = screen(value);
|
|
2385
2416
|
setStyle(container, 'translate', translate(written.container));
|
|
2386
2417
|
place(value, value);
|
|
2387
|
-
|
|
2388
|
-
track(value, written.slides);
|
|
2389
|
-
}
|
|
2418
|
+
track(value, written.slides);
|
|
2390
2419
|
}
|
|
2391
2420
|
|
|
2392
2421
|
/**
|
|
@@ -2401,7 +2430,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2401
2430
|
root.style.setProperty('--better-progress', progress(at, geo));
|
|
2402
2431
|
} else if (options.progress) {
|
|
2403
2432
|
const value = snapPosition(geo.snaps, at, geo.period),
|
|
2404
|
-
snap =
|
|
2433
|
+
snap = floor(value);
|
|
2405
2434
|
whole.progress = progress(at, geo);
|
|
2406
2435
|
whole.snap = logical(snap) + value - snap;
|
|
2407
2436
|
options.progress(whole, api);
|
|
@@ -2454,6 +2483,30 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2454
2483
|
return translate(screen(value)) || '0px';
|
|
2455
2484
|
}
|
|
2456
2485
|
|
|
2486
|
+
/**
|
|
2487
|
+
* Find if the carousel can go one snap in a direction.
|
|
2488
|
+
*
|
|
2489
|
+
* @param {number} direction 1 or -1.
|
|
2490
|
+
* @returns {boolean}
|
|
2491
|
+
*/
|
|
2492
|
+
function can(direction) {
|
|
2493
|
+
return !!geo && !geo.fit && (!!geo.period || (direction > 0 ? selected < geo.snaps.length - 1 : selected > 0));
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2496
|
+
/**
|
|
2497
|
+
* Go one snap in a direction, if the carousel can. Before the first measure pass, keep the step in the selected snap.
|
|
2498
|
+
*
|
|
2499
|
+
* @param {number} direction 1 or -1.
|
|
2500
|
+
* @param {boolean} [instant]
|
|
2501
|
+
*/
|
|
2502
|
+
function advance(direction, instant) {
|
|
2503
|
+
if (!geo) {
|
|
2504
|
+
go(selected + direction, instant);
|
|
2505
|
+
} else if (can(direction)) {
|
|
2506
|
+
step(selected, direction, instant);
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2509
|
+
|
|
2457
2510
|
/**
|
|
2458
2511
|
* Go to the snap after a snap in a direction. In loop mode, the first snap follows the last snap.
|
|
2459
2512
|
*
|
|
@@ -2485,7 +2538,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2485
2538
|
}
|
|
2486
2539
|
|
|
2487
2540
|
const target = targetLocation(geo.snaps[index].location, current(), geo.period, direction),
|
|
2488
|
-
span =
|
|
2541
|
+
span = abs(index - selected),
|
|
2489
2542
|
count = geo.period ? Math.min(span, geo.snaps.length - span) : span;
|
|
2490
2543
|
|
|
2491
2544
|
move(index, target, instant ? 0 : snapDuration(options.duration, count), EASE);
|
|
@@ -2502,7 +2555,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2502
2555
|
function move(index, target, duration, curve) {
|
|
2503
2556
|
const from = halt(),
|
|
2504
2557
|
picked = select(index),
|
|
2505
|
-
jump = !duration || geo.fit ||
|
|
2558
|
+
jump = !duration || geo.fit || abs(target - from) < 0.5;
|
|
2506
2559
|
|
|
2507
2560
|
location = target;
|
|
2508
2561
|
|
|
@@ -2542,10 +2595,10 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2542
2595
|
// Start in the first period, so that the location does not grow after each pause
|
|
2543
2596
|
location = normalize(from, geo.period);
|
|
2544
2597
|
motion.loop(location, direction, geo.period / speed, geo, nodes);
|
|
2545
|
-
} else if (
|
|
2598
|
+
} else if (abs(to - from) > 1) {
|
|
2546
2599
|
free = true;
|
|
2547
2600
|
location = to;
|
|
2548
|
-
motion.run(from, to,
|
|
2601
|
+
motion.run(from, to, abs(to - from) / speed, [0, 0, 1, 1], events.has('scroll'));
|
|
2549
2602
|
}
|
|
2550
2603
|
}
|
|
2551
2604
|
|
|
@@ -2633,6 +2686,27 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2633
2686
|
function onScroll() {
|
|
2634
2687
|
viewport.scrollLeft = viewport.scrollTop = 0;
|
|
2635
2688
|
}
|
|
2689
|
+
|
|
2690
|
+
/**
|
|
2691
|
+
* Pass a click on a clone to the same element in its slide. A clone is inert, so the click goes to the viewport or the container.
|
|
2692
|
+
* A click after a drag is cancelled, and it does not pass.
|
|
2693
|
+
*
|
|
2694
|
+
* @param {MouseEvent} event
|
|
2695
|
+
*/
|
|
2696
|
+
function onClick(event) {
|
|
2697
|
+
if (event.defaultPrevented || !clones.length || (event.target !== viewport && event.target !== container)) {
|
|
2698
|
+
return;
|
|
2699
|
+
}
|
|
2700
|
+
|
|
2701
|
+
// Find the element under the pointer in the clones, and the element at the same place in the slide
|
|
2702
|
+
clones.forEach((node) => node.inert = false);
|
|
2703
|
+
const node = document.elementFromPoint(event.clientX, event.clientY),
|
|
2704
|
+
index = clones.findIndex((clone) => clone.contains(node)),
|
|
2705
|
+
all = (item) => item ? [item, ...item.querySelectorAll('*')] : [];
|
|
2706
|
+
clones.forEach((item) => item.inert = true);
|
|
2707
|
+
|
|
2708
|
+
all(slides[index % slides.length])[all(clones[index]).indexOf(node)]?.dispatchEvent(new MouseEvent('click', event));
|
|
2709
|
+
}
|
|
2636
2710
|
}
|
|
2637
2711
|
|
|
2638
2712
|
/*
|
|
@@ -2642,7 +2716,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2642
2716
|
*/
|
|
2643
2717
|
|
|
2644
2718
|
|
|
2645
|
-
// The targets that keep their own pointer behavior
|
|
2719
|
+
// The targets that keep their own pointer and key behavior
|
|
2646
2720
|
const fields = 'input,textarea,select,[contenteditable]';
|
|
2647
2721
|
|
|
2648
2722
|
// The pointerdown events that a carousel took for a drag
|
|
@@ -2664,10 +2738,10 @@ const MOMENTUM = 325;
|
|
|
2664
2738
|
* @returns {number}
|
|
2665
2739
|
*/
|
|
2666
2740
|
function releaseDuration(path, velocity, curve, fallback) {
|
|
2667
|
-
if (Math.sign(path) !== Math.sign(velocity) ||
|
|
2741
|
+
if (Math.sign(path) !== Math.sign(velocity) || abs(velocity) < 0.05) {
|
|
2668
2742
|
return fallback;
|
|
2669
2743
|
}
|
|
2670
|
-
return clamp(
|
|
2744
|
+
return clamp(abs(path) * curve[1] / curve[0] / abs(velocity), 200, 1200);
|
|
2671
2745
|
}
|
|
2672
2746
|
|
|
2673
2747
|
/**
|
|
@@ -2732,9 +2806,9 @@ function releaseTarget({ location, from, velocity, current, snaps, min, max, per
|
|
|
2732
2806
|
|
|
2733
2807
|
let direction = 0;
|
|
2734
2808
|
|
|
2735
|
-
if (
|
|
2809
|
+
if (abs(velocity) > 0.3) {
|
|
2736
2810
|
direction = velocity < 0 ? 1 : -1;
|
|
2737
|
-
} else if (
|
|
2811
|
+
} else if (abs(moved) > 20) {
|
|
2738
2812
|
direction = moved < 0 ? 1 : -1;
|
|
2739
2813
|
}
|
|
2740
2814
|
|
|
@@ -2769,7 +2843,7 @@ function drag(context) {
|
|
|
2769
2843
|
// The selected snap at the drag start
|
|
2770
2844
|
snap = 0;
|
|
2771
2845
|
|
|
2772
|
-
const listen = (
|
|
2846
|
+
const listen = bind(window, [['pointermove', handle], ['pointerup', handle], ['pointercancel', handle]]);
|
|
2773
2847
|
|
|
2774
2848
|
function down(event) {
|
|
2775
2849
|
block = false;
|
|
@@ -2793,7 +2867,7 @@ function drag(context) {
|
|
|
2793
2867
|
axis = { x: geo.axis === 'x', sign: geo.sign, scale: geo.scale };
|
|
2794
2868
|
start = [event.clientX, event.clientY];
|
|
2795
2869
|
samples = [];
|
|
2796
|
-
listen(
|
|
2870
|
+
listen(true);
|
|
2797
2871
|
}
|
|
2798
2872
|
|
|
2799
2873
|
function handle(event) {
|
|
@@ -2816,10 +2890,10 @@ function drag(context) {
|
|
|
2816
2890
|
|
|
2817
2891
|
if (!context.dragging()) {
|
|
2818
2892
|
// A touch move across the axis scrolls the page
|
|
2819
|
-
if (event.pointerType !== 'mouse' &&
|
|
2893
|
+
if (event.pointerType !== 'mouse' && abs(cross) > abs(main)) {
|
|
2820
2894
|
return end();
|
|
2821
2895
|
}
|
|
2822
|
-
if (
|
|
2896
|
+
if (abs(main) < options.dragThreshold) {
|
|
2823
2897
|
return;
|
|
2824
2898
|
}
|
|
2825
2899
|
// Stop the motion, and start the drag at the location where the carousel is now
|
|
@@ -2849,7 +2923,7 @@ function drag(context) {
|
|
|
2849
2923
|
* @param {boolean} [up] true for a pointerup. A pointercancel releases with no velocity.
|
|
2850
2924
|
*/
|
|
2851
2925
|
function end(up) {
|
|
2852
|
-
listen(
|
|
2926
|
+
listen();
|
|
2853
2927
|
pointer = null;
|
|
2854
2928
|
|
|
2855
2929
|
if (!context.dragging()) {
|
|
@@ -2859,7 +2933,8 @@ function drag(context) {
|
|
|
2859
2933
|
const geo = context.geo(),
|
|
2860
2934
|
location = context.location(),
|
|
2861
2935
|
speed = up ? velocity(samples) : 0,
|
|
2862
|
-
|
|
2936
|
+
// The snaps and the limits come from the geometry, and skipSnaps, dragFree and dragFriction from the options
|
|
2937
|
+
target = releaseTarget({ ...geo, ...options, location, from: base, velocity: speed, current: snap }),
|
|
2863
2938
|
curve = options.dragFree === true ? GLIDE : EASE;
|
|
2864
2939
|
|
|
2865
2940
|
block = true;
|
|
@@ -2880,18 +2955,14 @@ function drag(context) {
|
|
|
2880
2955
|
// 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
2956
|
const select = (event) => context.dragging() && event.preventDefault();
|
|
2882
2957
|
|
|
2883
|
-
viewport
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
viewport.addEventListener('selectstart', select);
|
|
2958
|
+
const own = bind(viewport, [['pointerdown', down], ['click', click, true], ['dragstart', prevent], ['selectstart', select]]);
|
|
2959
|
+
|
|
2960
|
+
own(true);
|
|
2887
2961
|
|
|
2888
2962
|
return {
|
|
2889
2963
|
remove() {
|
|
2890
|
-
listen(
|
|
2891
|
-
|
|
2892
|
-
viewport.removeEventListener('click', click, true);
|
|
2893
|
-
viewport.removeEventListener('dragstart', prevent);
|
|
2894
|
-
viewport.removeEventListener('selectstart', select);
|
|
2964
|
+
listen();
|
|
2965
|
+
own();
|
|
2895
2966
|
}
|
|
2896
2967
|
};
|
|
2897
2968
|
}
|
|
@@ -2911,7 +2982,7 @@ function scrolls(node, viewport, axis, delta) {
|
|
|
2911
2982
|
for (; node && node !== viewport; node = node.parentElement) {
|
|
2912
2983
|
// Read the style only for an element with more content than space
|
|
2913
2984
|
if (node[size] > node[client] && /auto|scroll/.test(getComputedStyle(node)[overflow])) {
|
|
2914
|
-
const at =
|
|
2985
|
+
const at = abs(node[position]);
|
|
2915
2986
|
if (delta > 0 ? at + node[client] < node[size] - 1 : at > 0) {
|
|
2916
2987
|
return true;
|
|
2917
2988
|
}
|
|
@@ -2922,7 +2993,8 @@ function scrolls(node, viewport, axis, delta) {
|
|
|
2922
2993
|
}
|
|
2923
2994
|
|
|
2924
2995
|
/**
|
|
2925
|
-
* Make the wheel input of a viewport: in a vertical carousel, and with wheel: 'both' also in a horizontal carousel.
|
|
2996
|
+
* Make the wheel input of a viewport: in a vertical carousel, and with wheel: 'both' also in a horizontal carousel. With wheel: 'axis',
|
|
2997
|
+
* a horizontal carousel follows only a horizontal wheel motion, such as a trackpad swipe, and a vertical wheel scrolls the page. The carousel follows
|
|
2926
2998
|
* the wheel distance with a short motion on the compositor. When the wheel stops, the carousel goes to a snap. At the first and the last
|
|
2927
2999
|
* location, the page scrolls.
|
|
2928
3000
|
*
|
|
@@ -2952,31 +3024,28 @@ function wheel(context) {
|
|
|
2952
3024
|
|
|
2953
3025
|
function onWheel(event) {
|
|
2954
3026
|
const geo = context.geo(),
|
|
2955
|
-
side = geo.axis === 'x' &&
|
|
3027
|
+
side = geo.axis === 'x' && abs(event.deltaX) > abs(event.deltaY),
|
|
2956
3028
|
delta = side ? event.deltaX * geo.sign : event.deltaY;
|
|
2957
3029
|
|
|
2958
|
-
if (event.ctrlKey || !delta || context.dragging()) {
|
|
3030
|
+
if (event.ctrlKey || !delta || context.dragging() || (options.wheel === 'axis' && geo.axis === 'x' && !side)) {
|
|
2959
3031
|
return;
|
|
2960
3032
|
}
|
|
2961
3033
|
|
|
2962
3034
|
const now = event.timeStamp,
|
|
2963
3035
|
gesture = now - time < 200,
|
|
2964
|
-
size =
|
|
3036
|
+
size = abs(delta),
|
|
2965
3037
|
// A delta that does not grow is the inertia of the gesture, not a new push
|
|
2966
3038
|
inertia = gesture && size <= last;
|
|
2967
3039
|
|
|
3040
|
+
time = now;
|
|
2968
3041
|
last = size;
|
|
2969
|
-
|
|
2970
|
-
held = false;
|
|
2971
|
-
}
|
|
3042
|
+
held &&= gesture;
|
|
2972
3043
|
|
|
2973
3044
|
// 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;
|
|
3045
|
+
inner = (gesture && inner) || scrolls(event.target, viewport, side ? 'x' : 'y', side ? event.deltaX : delta);
|
|
3046
|
+
if (inner) {
|
|
2977
3047
|
return;
|
|
2978
3048
|
}
|
|
2979
|
-
inner = false;
|
|
2980
3049
|
|
|
2981
3050
|
const base = context.location(),
|
|
2982
3051
|
path = delta * (event.deltaMode === 1 ? 40 : event.deltaMode ? geo.view : 1);
|
|
@@ -2986,10 +3055,8 @@ function wheel(context) {
|
|
|
2986
3055
|
target = clamp(target, geo.min, geo.max);
|
|
2987
3056
|
}
|
|
2988
3057
|
|
|
2989
|
-
time = now;
|
|
2990
|
-
|
|
2991
3058
|
// At the end, the page scrolls. The inertia of a gesture that moved the carousel does not scroll the page.
|
|
2992
|
-
if (
|
|
3059
|
+
if (abs(target - base) < 0.5) {
|
|
2993
3060
|
if (held && inertia) {
|
|
2994
3061
|
event.preventDefault();
|
|
2995
3062
|
}
|
|
@@ -3018,23 +3085,23 @@ function wheel(context) {
|
|
|
3018
3085
|
const geo = context.geo(),
|
|
3019
3086
|
at = context.current(),
|
|
3020
3087
|
index = nearestSnap(geo.snaps, at, geo.period),
|
|
3021
|
-
away =
|
|
3088
|
+
away = abs(targetLocation(geo.snaps[index].location, at, geo.period) - at) > 10;
|
|
3022
3089
|
|
|
3023
3090
|
context.step(index, index === snap && away ? direction : 0);
|
|
3024
3091
|
}
|
|
3025
3092
|
|
|
3026
3093
|
// A wheel listener that is not passive makes the browser wait for the main thread before it scrolls the page.
|
|
3027
3094
|
// 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
|
-
}
|
|
3095
|
+
const listen = bind(viewport, [['wheel', onWheel, { passive: false }]]);
|
|
3032
3096
|
|
|
3033
|
-
|
|
3097
|
+
context.on('init reinit resize', () => {
|
|
3098
|
+
const geo = context.geo();
|
|
3099
|
+
listen(!geo.fit && (geo.axis === 'y' || options.wheel !== true));
|
|
3100
|
+
});
|
|
3034
3101
|
|
|
3035
3102
|
return {
|
|
3036
3103
|
remove() {
|
|
3037
|
-
|
|
3104
|
+
listen();
|
|
3038
3105
|
clearTimeout(timer);
|
|
3039
3106
|
}
|
|
3040
3107
|
};
|
|
@@ -3061,19 +3128,17 @@ function keys(context) {
|
|
|
3061
3128
|
direction = geo && !geo.fit && (geo.axis === 'y' ? { ArrowUp: -1, ArrowDown: 1 } : { ArrowLeft: -geo.sign, ArrowRight: geo.sign })[key],
|
|
3062
3129
|
name = direction > 0 ? 'Next' : 'Prev';
|
|
3063
3130
|
// At the first and the last slide, the key scrolls the page
|
|
3064
|
-
if (direction && !(event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || target.
|
|
3131
|
+
if (direction && !(event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || target.closest(fields)) && api['canGoTo' + name]()) {
|
|
3065
3132
|
event.preventDefault();
|
|
3066
3133
|
api['goTo' + name]();
|
|
3067
3134
|
}
|
|
3068
3135
|
}
|
|
3069
3136
|
|
|
3070
|
-
root
|
|
3137
|
+
const listen = bind(root, [['keydown', onKey]]);
|
|
3071
3138
|
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
}
|
|
3076
|
-
};
|
|
3139
|
+
listen(true);
|
|
3140
|
+
|
|
3141
|
+
return { remove: listen };
|
|
3077
3142
|
}
|
|
3078
3143
|
|
|
3079
3144
|
/**
|
|
@@ -3101,21 +3166,17 @@ function focus(context) {
|
|
|
3101
3166
|
if (!geo || index < 0 || event.timeStamp - pressed < 600) {
|
|
3102
3167
|
return;
|
|
3103
3168
|
}
|
|
3104
|
-
if (options.focus && !inView(geo
|
|
3169
|
+
if (options.focus && !inView(geo, context.location(), 1).map((item) => item % geo.count).includes(index)) {
|
|
3105
3170
|
api.goToSlide(index);
|
|
3106
3171
|
}
|
|
3107
3172
|
context.emit('slidefocus');
|
|
3108
3173
|
}
|
|
3109
3174
|
|
|
3110
|
-
container
|
|
3111
|
-
container.addEventListener('focusin', onFocus);
|
|
3175
|
+
const listen = bind(container, [['pointerdown', press], ['focusin', onFocus]]);
|
|
3112
3176
|
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
container.removeEventListener('focusin', onFocus);
|
|
3117
|
-
}
|
|
3118
|
-
};
|
|
3177
|
+
listen(true);
|
|
3178
|
+
|
|
3179
|
+
return { remove: listen };
|
|
3119
3180
|
}
|
|
3120
3181
|
|
|
3121
3182
|
/*
|
|
@@ -3158,32 +3219,25 @@ function nav(context) {
|
|
|
3158
3219
|
}
|
|
3159
3220
|
|
|
3160
3221
|
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
|
-
});
|
|
3222
|
+
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
3223
|
}
|
|
3170
3224
|
|
|
3171
3225
|
function update() {
|
|
3172
3226
|
const fit = api.isFit(),
|
|
3173
|
-
count = fit ? 0 :
|
|
3227
|
+
count = fit ? 0 : context.geo().logical.length,
|
|
3174
3228
|
selected = api.selectedSnap();
|
|
3175
3229
|
|
|
3176
|
-
buttons.forEach((
|
|
3230
|
+
buttons.forEach((item, index) => {
|
|
3177
3231
|
if (fit) {
|
|
3178
|
-
|
|
3232
|
+
item.remove();
|
|
3179
3233
|
} else {
|
|
3180
|
-
if (!
|
|
3234
|
+
if (!item.isConnected) {
|
|
3181
3235
|
// Before the dots, for the tab order
|
|
3182
|
-
list?.parentNode === arrows ? list.before(
|
|
3236
|
+
list?.parentNode === arrows ? list.before(item) : arrows.append(item);
|
|
3183
3237
|
}
|
|
3184
3238
|
const disabled = !(index ? api.canGoToNext() : api.canGoToPrev());
|
|
3185
|
-
if (
|
|
3186
|
-
|
|
3239
|
+
if (item.disabled !== disabled) {
|
|
3240
|
+
item.disabled = disabled;
|
|
3187
3241
|
}
|
|
3188
3242
|
}
|
|
3189
3243
|
});
|
|
@@ -3203,16 +3257,12 @@ function nav(context) {
|
|
|
3203
3257
|
}
|
|
3204
3258
|
|
|
3205
3259
|
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);
|
|
3260
|
+
const index = dots.length,
|
|
3261
|
+
dot = createButton(cls.dot, context.container, labels.dot.replace('%d', index + 1), () => api.goToSnap(index));
|
|
3211
3262
|
// The window of the dots hides the dots out of the zoom: keep them out of the tab order
|
|
3212
3263
|
if (windowed) {
|
|
3213
3264
|
dot.tabIndex = -1;
|
|
3214
3265
|
}
|
|
3215
|
-
dot.addEventListener('click', () => api.goToSnap(index));
|
|
3216
3266
|
dots.push(dot);
|
|
3217
3267
|
track.append(dot);
|
|
3218
3268
|
}
|
|
@@ -3248,23 +3298,16 @@ function nav(context) {
|
|
|
3248
3298
|
|
|
3249
3299
|
// Center the group of up to 5 dots in the window of the dots, and move the marker to the selected dot. The CSS moves the track and the marker with "translate".
|
|
3250
3300
|
if (windowed) {
|
|
3251
|
-
|
|
3301
|
+
setStyle(list, '--better-shift', 2 - (Math.max(0, selected - 2) + Math.min(count - 1, selected + 2)) / 2);
|
|
3252
3302
|
}
|
|
3253
|
-
|
|
3303
|
+
setStyle(list, '--better-dot', selected);
|
|
3254
3304
|
}
|
|
3255
3305
|
|
|
3256
|
-
|
|
3257
|
-
function variable(name, value) {
|
|
3258
|
-
if (list.style.getPropertyValue(name) !== String(value)) {
|
|
3259
|
-
list.style.setProperty(name, value);
|
|
3260
|
-
}
|
|
3261
|
-
}
|
|
3262
|
-
|
|
3263
|
-
['init', 'reinit', 'resize', 'select', 'slideschanged'].forEach((name) => context.on(name, update));
|
|
3306
|
+
context.on('init reinit resize select slideschanged', update);
|
|
3264
3307
|
|
|
3265
3308
|
return {
|
|
3266
3309
|
remove() {
|
|
3267
|
-
buttons.forEach((
|
|
3310
|
+
buttons.forEach((item) => item.remove());
|
|
3268
3311
|
list?.remove();
|
|
3269
3312
|
}
|
|
3270
3313
|
};
|
|
@@ -3294,11 +3337,11 @@ function sync(context) {
|
|
|
3294
3337
|
api.goToSlide(index, instant === true);
|
|
3295
3338
|
}
|
|
3296
3339
|
|
|
3340
|
+
// A click on a clone comes to its slide. See onClick() in carousel.js.
|
|
3297
3341
|
function click(event) {
|
|
3298
|
-
|
|
3299
|
-
const index = context.nodes().findIndex((node) => node.contains(event.target));
|
|
3342
|
+
const index = api.slideNodes().findIndex((node) => node.contains(event.target));
|
|
3300
3343
|
if (other && index > -1) {
|
|
3301
|
-
other.goToSlide(index
|
|
3344
|
+
other.goToSlide(index);
|
|
3302
3345
|
}
|
|
3303
3346
|
}
|
|
3304
3347
|
|
|
@@ -3307,21 +3350,22 @@ function sync(context) {
|
|
|
3307
3350
|
if (other) {
|
|
3308
3351
|
return;
|
|
3309
3352
|
}
|
|
3310
|
-
other = target.goToSnap ? target : find(typeof target === 'string' ? optionElement(target, context.root) : target);
|
|
3353
|
+
other = target.goToSnap ? target : context.find(typeof target === 'string' ? optionElement(target, context.root) : target);
|
|
3311
3354
|
if (other) {
|
|
3312
3355
|
other.on('select', show);
|
|
3313
3356
|
show(true);
|
|
3314
3357
|
}
|
|
3315
3358
|
}
|
|
3316
3359
|
|
|
3317
|
-
context.on('init', connect);
|
|
3318
|
-
|
|
3319
|
-
|
|
3360
|
+
context.on('init reinit', connect);
|
|
3361
|
+
const listen = bind(container, [['click', click]]);
|
|
3362
|
+
|
|
3363
|
+
listen(true);
|
|
3320
3364
|
|
|
3321
3365
|
return {
|
|
3322
3366
|
remove() {
|
|
3323
3367
|
other?.off('select', show);
|
|
3324
|
-
|
|
3368
|
+
listen();
|
|
3325
3369
|
}
|
|
3326
3370
|
};
|
|
3327
3371
|
}
|
|
@@ -3363,11 +3407,6 @@ BetterCarousel.get = find;
|
|
|
3363
3407
|
* @param {object} [options]
|
|
3364
3408
|
* @returns {object[]}
|
|
3365
3409
|
*/
|
|
3366
|
-
BetterCarousel.initAll = (selector, options) =>
|
|
3367
|
-
const elements = [...document.querySelectorAll(selector)];
|
|
3368
|
-
// Read the boxes of the lazy carousels before the first setup() writes. Then the reads cause one layout, not one for each carousel.
|
|
3369
|
-
readBoxes(elements.filter((element) => !find(element) && resolve(element, BetterCarousel.defaults, options).lazy));
|
|
3370
|
-
return elements.map((element) => BetterCarousel(element, options));
|
|
3371
|
-
};
|
|
3410
|
+
BetterCarousel.initAll = (selector, options) => [...document.querySelectorAll(selector)].map((element) => BetterCarousel(element, options));
|
|
3372
3411
|
|
|
3373
3412
|
export { BetterCarousel as default };
|