I am currently working on a way to determine if an open dropdown menu is within the user's view or has moved out of sight. I've been experimenting with the getBoundingClientRect()
method to compare coordinates and viewport dimensions, but it seems to be behaving unexpectedly.
If the dropdown menu is fully visible, I want it to open downward; if it is not visible, I want it to open upward. In my example, I simply log the direction to the console for now.
Take, for instance, the scenario where the dropdown selector is positioned at the bottom of the page. When opened, it becomes too large for the viewport, causing a scrollbar to appear. The function should recognize that the dropdown menu options are no longer in view and log open to top
, but it fails to do so.
You can view the sample code at Plunker.
How can I accurately determine whether my dropdown menu is within the viewport in order to decide whether to open it up or down?
I would prefer to avoid using jQuery unless absolutely necessary!