|
1 | 1 | (function () { |
2 | 2 |
|
| 3 | + const desktopMedia = window.matchMedia("(min-width: 769px)"); |
| 4 | + |
3 | 5 | function initProjectTechTicker() { |
4 | 6 | const techNodes = document.querySelectorAll(".project-tech"); |
5 | 7 |
|
6 | | - function isDesktop() { |
7 | | - return window.matchMedia("(min-width: 769px)").matches; |
8 | | - } |
9 | | - |
10 | 8 | function prepareNode(node) { |
11 | | - // Only prepare marquee for desktop |
12 | | - if (!isDesktop()) { |
| 9 | + if (!desktopMedia.matches) { |
13 | 10 | return; |
14 | 11 | } |
15 | 12 |
|
16 | 13 | if (node.querySelector(".project-tech-marquee")) { |
17 | 14 | return; |
18 | 15 | } |
19 | 16 |
|
20 | | - const rawText = node.textContent.trim(); |
| 17 | + const rawText = node.dataset.techText || node.textContent.trim(); |
| 18 | + node.dataset.techText = rawText; |
21 | 19 | node.textContent = ""; |
22 | 20 |
|
23 | 21 | const marquee = document.createElement("div"); |
|
38 | 36 | } |
39 | 37 |
|
40 | 38 | function removeMarquee(node) { |
41 | | - const marquee = node.querySelector(".project-tech-marquee"); |
42 | | - if (marquee) { |
43 | | - const text = marquee.textContent; |
44 | | - node.textContent = text; |
| 39 | + if (node.dataset.techText) { |
| 40 | + node.textContent = node.dataset.techText; |
45 | 41 | } |
46 | 42 | } |
47 | 43 |
|
48 | | - function updateNode(node) { |
49 | | - const marquee = node.querySelector(".project-tech-marquee"); |
50 | | - const firstItem = marquee ? marquee.querySelector(".project-tech-item") : null; |
51 | | - if (!marquee || !firstItem) { |
52 | | - return; |
53 | | - } |
54 | | - |
55 | | - const itemWidth = Math.max(40, Math.ceil(firstItem.scrollWidth)); |
56 | | - const pxPerSecond = 30; |
57 | | - const duration = Math.max(8, itemWidth / pxPerSecond); |
58 | | - |
59 | | - node.style.setProperty("--marquee-item-width", itemWidth + "px"); |
60 | | - node.style.setProperty("--marquee-duration", duration.toFixed(2) + "s"); |
| 44 | + function syncNodes() { |
| 45 | + techNodes.forEach(function (node) { |
| 46 | + if (desktopMedia.matches) { |
| 47 | + prepareNode(node); |
| 48 | + } else { |
| 49 | + removeMarquee(node); |
| 50 | + } |
| 51 | + }); |
61 | 52 | } |
62 | 53 |
|
63 | | - techNodes.forEach(function (node) { |
64 | | - if (isDesktop()) { |
65 | | - prepareNode(node); |
66 | | - updateNode(node); |
67 | | - } else { |
68 | | - removeMarquee(node); |
69 | | - } |
70 | | - }); |
| 54 | + syncNodes(); |
71 | 55 |
|
72 | | - let resizeTimeout = null; |
73 | | - window.addEventListener("resize", function () { |
74 | | - if (resizeTimeout) { |
75 | | - window.clearTimeout(resizeTimeout); |
76 | | - } |
77 | | - resizeTimeout = window.setTimeout(function () { |
78 | | - techNodes.forEach(function (node) { |
79 | | - if (isDesktop()) { |
80 | | - prepareNode(node); |
81 | | - updateNode(node); |
82 | | - } else { |
83 | | - removeMarquee(node); |
84 | | - } |
85 | | - }); |
86 | | - }, 150); |
87 | | - }); |
| 56 | + if (typeof desktopMedia.addEventListener === "function") { |
| 57 | + desktopMedia.addEventListener("change", syncNodes); |
| 58 | + } else if (typeof desktopMedia.addListener === "function") { |
| 59 | + desktopMedia.addListener(syncNodes); |
| 60 | + } |
88 | 61 | } |
89 | 62 |
|
90 | 63 | function createReadMoreForCard(card, index) { |
|
0 commit comments