Skip to content

Commit c5c6743

Browse files
committed
Adapting cloud image component logic to take text elements.
1 parent a57b581 commit c5c6743

File tree

2 files changed

+92
-7
lines changed

2 files changed

+92
-7
lines changed

blocks/cloud-image/cloud-image.css

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@
3838
}
3939
}
4040

41+
.cloud-image-text-wrapper {
42+
width: min(1440px, 100% - 2 * 100px);
43+
margin-inline: auto;
44+
45+
.cloud-image-text {
46+
position: relative;
47+
height: 75vh;
48+
}
49+
50+
.cloud-image-text-element {
51+
position: absolute;
52+
text-align: center;
53+
animation: wobble 36s infinite;
54+
opacity: 0;
55+
z-index: 0;
56+
p {
57+
font-size: clamp(2rem, 3.5vw, 3.5rem);
58+
}
59+
}
60+
}
61+
4162
/* Keyframe cloud animation */
4263
@keyframes cloud {
4364
0% {
@@ -68,4 +89,35 @@
6889
100% {
6990
opacity: 0;
7091
}
71-
}
92+
}
93+
94+
/* Keyframe wobble animation */
95+
@keyframes wobble {
96+
0% {
97+
transform: scale(.25);
98+
filter: blur(5px);
99+
opacity: 0;
100+
}
101+
102+
6% {
103+
opacity: 1;
104+
}
105+
106+
20% {
107+
filter: blur(0);
108+
}
109+
110+
25% {
111+
opacity: 1;
112+
}
113+
114+
33% {
115+
transform: scale(.9);
116+
filter: blur(5px);
117+
opacity: 0;
118+
}
119+
120+
100% {
121+
opacity: 0;
122+
}
123+
}

blocks/cloud-image/cloud-image.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,35 @@ function addClassToDivsWithPicture() {
1818
});
1919
}
2020

21+
function addClassToDivsWithText() {
22+
const cloudImageElementsWithText = document.querySelectorAll('.cloud-image-text');
23+
cloudImageElementsWithText.forEach((cloudImageText) => {
24+
const childDivs = cloudImageText.querySelectorAll(':scope > div');
25+
26+
childDivs.forEach((div) => {
27+
if (!div.querySelector('picture')) {
28+
div.classList.add('cloud-image-text-element');
29+
}
30+
});
31+
});
32+
}
33+
2134
function setDataAttributes() {
2235
// Arrays for top and left values calculation (mocked from the original site that seems random)
2336
const topValues = [
2437
20, 0, 50, 70, 100, 60, 90, 20, 0, 40,
25-
90, 80, 60, 60, 90, 10, 40, 50, 80,
38+
90, 80, 60, 60, 90, 10, 40, 50, 80, 100,
2639
];
2740
const leftValues = [
28-
50, 90, 60, 80, 30, 80, 0, 20, 60, 90,
41+
50, 90, 60, 80, 30, 80, 0, 20, 60, 90, 100,
2942
10, 70, 100, 30, 90, 0, 50, 90, 90, 30,
3043
];
3144
const delayValues = [
3245
4, 5, 2, 34, 3, 7, 8,
3346
9, 15, 15, 12, 22, 20, 26, 33, 31, 25, 27,
3447
];
3548
const cloudImagePictureElements = document.querySelectorAll('.cloud-image-element');
49+
const cloudImageTextElements = document.querySelectorAll('.cloud-image-text-element');
3650

3751
cloudImagePictureElements.forEach((div, index) => {
3852
const leftIndex = index % leftValues.length;
@@ -42,23 +56,41 @@ function setDataAttributes() {
4256
div.setAttribute('data-top', topValues[topIndex]);
4357
div.setAttribute('data-delay', delayValues[delayIndex]);
4458
});
59+
60+
cloudImageTextElements.forEach((div, index) => {
61+
const leftIndex = index % leftValues.length;
62+
const topIndex = index % topValues.length;
63+
const delayIndex = index % delayValues.length;
64+
div.setAttribute('data-left', leftValues[leftIndex]);
65+
div.setAttribute('data-top', topValues[topIndex]);
66+
div.setAttribute('data-delay', delayValues[delayIndex]);
67+
});
4568
}
4669

4770
function positionImageInTheCloud() {
4871
const cloudImgContainer = document.querySelector('.cloud-image-wrapper');
4972
const cloudImagePictureElements = document.querySelectorAll('.cloud-image-element');
73+
const cloudImageTextElements = document.querySelectorAll('.cloud-image-text-element');
5074

5175
const {
52-
width: containerWidth,
53-
height: containerHeight,
76+
width: ImgContainerWidth,
77+
height: ImgContainerHeight,
5478
} = cloudImgContainer.getBoundingClientRect();
5579

5680
cloudImagePictureElements.forEach((element) => {
5781
const img = element.querySelector('img');
5882
if (!img) return;
5983
const { width: imgWidth, height: imgHeight } = img;
60-
const elementStyleTop = `${element.dataset.left * (1 - imgWidth / containerWidth)}`;
61-
const elementStyleLeft = `${element.dataset.top * (1 - imgHeight / containerHeight) * 0.96}`;
84+
const elementStyleTop = `${element.dataset.left * (1 - imgWidth / ImgContainerWidth)}`;
85+
const elementStyleLeft = `${element.dataset.top * (1 - imgHeight / ImgContainerHeight) * 0.96}`;
86+
element.style.left = (elementStyleLeft > 60) ? `${60}%` : `${elementStyleLeft}%`;
87+
element.style.top = (elementStyleTop > 57) ? `${57}%` : `${elementStyleTop}%`;
88+
element.style.animationDelay = `${element.dataset.delay - 36}s`;
89+
});
90+
91+
cloudImageTextElements.forEach((element) => {
92+
const elementStyleTop = `${100 - element.dataset.left}`;
93+
const elementStyleLeft = `${100 - element.dataset.top}`;
6294
element.style.left = (elementStyleLeft > 60) ? `${60}%` : `${elementStyleLeft}%`;
6395
element.style.top = (elementStyleTop > 57) ? `${57}%` : `${elementStyleTop}%`;
6496
element.style.animationDelay = `${element.dataset.delay - 36}s`;
@@ -68,4 +100,5 @@ function positionImageInTheCloud() {
68100
export default positionImageInTheCloud;
69101

70102
addClassToDivsWithPicture();
103+
addClassToDivsWithText();
71104
setDataAttributes();

0 commit comments

Comments
 (0)