|
11 | 11 | </div> |
12 | 12 |
|
13 | 13 | <script> |
14 | | - // Initialize DOM elements using classes |
| 14 | + |
15 | 15 | const popup = document.querySelector(".state-popup") as HTMLElement; |
16 | 16 | const popupStateName = document.querySelector(".state-popup__name") as HTMLElement; |
17 | 17 | const popupStateInfo = document.querySelector(".state-popup__info") as HTMLElement; |
|
39 | 39 | const popupWidth = popup.offsetWidth; |
40 | 40 | const popupHeight = popup.offsetHeight; |
41 | 41 |
|
42 | | - // Get scroll position |
43 | 42 | const scrollX = window.pageXOffset || document.documentElement.scrollLeft; |
44 | 43 | const scrollY = window.pageYOffset || document.documentElement.scrollTop; |
45 | 44 |
|
46 | | - // Get click position relative to the page |
47 | 45 | const clickX = event.pageX; |
48 | 46 | const clickY = event.pageY; |
49 | 47 |
|
50 | | - // Calculate initial position (centered on click) |
51 | 48 | let left = clickX - (popupWidth / 2); |
52 | 49 | let top = clickY - (popupHeight / 2); |
53 | 50 |
|
54 | | - // Adjust horizontal position to keep within viewport |
55 | 51 | if (left + popupWidth > viewportWidth + scrollX - 20) { |
56 | 52 | left = viewportWidth + scrollX - popupWidth - 20; |
57 | 53 | } |
58 | 54 | if (left < scrollX + 20) { |
59 | 55 | left = scrollX + 20; |
60 | 56 | } |
61 | 57 |
|
62 | | - // Adjust vertical position to keep within viewport |
63 | 58 | if (top + popupHeight > viewportHeight + scrollY - 20) { |
64 | 59 | top = clickY - popupHeight - 20; |
65 | 60 | } |
66 | 61 | if (top < scrollY + 20) { |
67 | 62 | top = scrollY + 20; |
68 | 63 | } |
69 | 64 |
|
70 | | - // Apply the calculated position |
71 | 65 | popup.style.left = `${left}px`; |
72 | 66 | popup.style.top = `${top}px`; |
73 | 67 |
|
74 | | - // Make popup visible with transition |
| 68 | + |
75 | 69 | requestAnimationFrame(() => { |
76 | 70 | popup.style.opacity = "1"; |
77 | 71 | }); |
78 | 72 | } |
79 | 73 |
|
80 | | -// Event Listeners |
| 74 | + |
81 | 75 | document.addEventListener("showStatePopup", ((e: CustomEvent) => { |
82 | 76 | const { event, stateInfo } = e.detail; |
83 | 77 | showPopup(event, stateInfo); |
|
0 commit comments