It seems like your question is a bit unclear, but I'll assume you're looking to adjust the position of your drop-down menus when there's limited space on the screen. One solution could be to check in your JavaScript if the drop-down's height is greater than the window's height. If it is, you can set a new position for the drop-down (using absolute positioning, adjusting the top attribute). This check should be done each time the document is loaded.
However, it's generally best to avoid situations where drop-down menus exceed the dimensions of the website as it can negatively impact the user experience.
EDIT: Based on additional information provided, here's a suggested approach using jQuery/JS to position drop-down menus relative to the main navigation. When hovering over the main navigation section, you can determine whether to display the drop-down above or below based on its position relative to the screen. The logic would involve displaying the drop-down container and calculating its position based on the main navigation's coordinates.
To position the drop-down above the main navigation, calculate the y-position by subtracting the drop-down's height from the main navigation's position. For positioning below, reverse the calculations. Adjustments may be needed for any desired offset between the drop-down and the main navigation item being hovered over.
Since both main navigation and drop-down positions are dynamic, it's crucial to ensure that their measurements remain relative to one another rather than static.
I've outlined the logic behind the code; feel free to reach out if any parts need further clarification.
Pat