-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.esm.js
104 lines (83 loc) · 3.15 KB
/
index.esm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
export const initMarqueeeSlider = (id, options = {}) => {
const swiper = document.getElementById(id);
const swiperWrapperInit = swiper.querySelector(".marquee-slider-wrapper");
const swiperSlidesWrapper = swiperWrapperInit.getElementsByClassName(
"marquee-slider-slides-wrapper"
);
const getDirOp = (dir) => {
if (dir === "right") return { sym: "+", axis: "X" };
if (dir === "left") return { sym: "-", axis: "X" };
if (dir === "up") return { sym: "-", axis: "Y" };
if (dir === "down") return { sym: "+", axis: "Y" };
};
const { stopOnHover = false, allowPointEvent = true, dir = "left" } = options;
if (!allowPointEvent) {
swiper.style.pointerEvents = "none";
}
const speed = parseFloat(swiper.getAttribute("data-speed")) || 5;
const space = parseFloat(swiper.getAttribute("data-space")) || 10;
const prefix = swiper.getAttribute("data-prefix") || "px";
const spaceBetween = { value: space, prefix: prefix };
const initStyle = () => {
swiper.style.overflow = "hidden";
swiperSlidesWrapper[0].style.gap = `0 ${spaceBetween.value}${spaceBetween.prefix}`;
swiperSlidesWrapper[0].style.margin = `0 ${spaceBetween.value / 2}${
spaceBetween.prefix
}`;
swiperSlidesWrapper[0].style.minWidth = `${swiperSlidesWrapper[0].offsetWidth}px`;
};
const clonedWrapper = swiperWrapperInit.cloneNode(true);
const init = (swiperWrapper) => {
const width = swiperSlidesWrapper[0].offsetWidth;
const height = swiperSlidesWrapper[0].offsetHeight;
if (dir === "right") {
swiperWrapper.style.left = `-${width + space}px`;
}
const keyframes = `
@keyframes ${id} {
0% {
transform: translate${getDirOp(dir).axis}(0);
}
100% {
transform: translate${getDirOp(dir).axis}(${getDirOp(dir).sym}${
width + space
}px);
}
}
`;
let delta = Math.ceil(swiper.offsetWidth / width);
if (delta < 1) {
delta = 1;
}
let swipersLength = swiperSlidesWrapper.length;
for (let i = 1; i <= delta; i++) {
for (let j = 0; j < swipersLength; j++) {
const elm = swiperSlidesWrapper[j];
const clone = elm.cloneNode(true);
swiperWrapper.appendChild(clone);
}
}
// Apply CSS animation to the swiper wrapper
swiperWrapper.style.animation = `${id} ${speed}s linear infinite`;
// Inject keyframes into a style element
const styleElement = document.createElement("style");
styleElement.innerHTML = keyframes;
document.head.appendChild(styleElement);
// Reset the position when animation completes
swiperWrapper.addEventListener("animationiteration", () => {
swiperWrapper.style.transform = `translate${getDirOp(dir).axis}(0)`;
});
//options handling
if (stopOnHover) {
swiperWrapper.addEventListener("mouseleave", () => {
swiperWrapper.style.animationPlayState = "running";
});
swiperWrapper.addEventListener("mouseenter", () => {
swiperWrapper.style.animationPlayState = "paused";
});
}
};
initStyle();
init(swiperWrapperInit);
window.addEventListener("resize", init(clonedWrapper));
};