<p style="text-align: center;"> <a…
- User
여기서 모달창이 내가 클릭한 위치 바로 아래서 열리게 하고 싶은데 어캐 하는거야
- Assistant
여기서 모달창이 내가 클릭한 위치 바로 아래서 열리게 하고 싶은데 어캐 하는거야
To have the modal window appear directly below the position where you clicked, you can modify your code to calculate the position of the clicked element and position the modal accordingly. Here's how you can do it:
1. Modify the HTML to Pass the Element:
Instead of using the href attribute to call the JavaScript function, use the onclick event and pass this (the clicked element) to the function.
<p style="text-align: center;">
<a href="javascript:void(0);" onclick="SITE.openModalMenu('m202409190f7dcc24ffda1', this);">뷰티/패션</a>
</p>
2. Update the JavaScript Function to Position the Modal:
Adjust your SITE.openModalMenu function to accept the clicked element, calculate its position, and set the modal's position accordingly.
SITE.openModalMenu = function(menuId, element) {
var modal = document.getElementById(menuId);
// Get the position of the clicked element
var rect = element.getBoundingClientRect();
// Set the modal's position relative to the clicked element
modal.style.position = 'absolute';
modal.style.top = rect.bottom + 'px'; // Position below the element
modal.style.left = rect.left + 'px';
// Show the modal
modal.style.display = 'block';
};
3. Ensure the Modal is Properly Styled:
Make sure that your modal has the correct styling to allow absolute positioning.
/* Example CSS for the modal */
#yourModalId {
position: absolute; /* Ensure the modal can be positioned absolutely */
display: none; /* Hidden by default */
/* Add additional styling as needed */
}
Notes:
getBoundingClientRect() method returns the size of an element and its position relative to the viewport. By using rect.bottom, you can position the modal directly below the clicked element.this in the onclick handler, you ensure that you're referencing the clicked <a> element.modal.style.display = 'block'; (or whatever display style is appropriate) to make the modal visible when it's opened.Alternative Using Event Object:
If you prefer, you can pass the event object to the function and use the mouse coordinates.
HTML:
<p style="text-align: center;">
<a href="javascript:void(0);" onclick="SITE.openModalMenu('m202409190f7dcc24ffda1', event);">뷰티/패션</a>
</p>
JavaScript:
SITE.openModalMenu = function(menuId, event) {
var modal = document.getElementById(menuId);
// Get mouse click position
var x = event.clientX;
var y = event.clientY;
// Set the modal's position
modal.style.position = 'absolute';
modal.style.top = y + 'px';
modal.style.left = x + 'px';
// Show the modal
modal.style.display = 'block';
};
However, using the element's position is generally more reliable, especially if your page can scroll, since clientX and clientY are relative to the viewport.
Remember to Handle Scrolling:
If your page scrolls, you might need to adjust for the scroll position.
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
modal.style.top = (rect.bottom + scrollTop) + 'px';
modal.style.left = (rect.left + scrollLeft) + 'px';
Summary:
By modifying your function to calculate and set the modal's position based on the clicked element, you can have the modal appear exactly where you want it—directly below the clicked link.
Not yet included in a published release.