如何使自定义菜单的内容向一个方向溢出?
我想做什么?
- 我的应用程序中有一个自定义菜单,其中包含一些选项(例如 8 个选项)
- 基于某些逻辑,我需要禁用菜单中的某些选项(例如最后 4 个选项)
- 如果用户将鼠标悬停在任何禁用的选项上,我需要显示一个弹出窗口解释为什么该选项被禁用
问题:
- 由于事先不知道菜单中的选项数量,因此菜单应该可以滚动以容纳更多选项(如果需要)
- 但是,将鼠标悬停在禁用选项上时的弹出窗口应该溢出菜单边界(如果需要)
- 这基本上归结为具有
overflow-x:visible
和overflow-y: auto
这根据规范是不可能的 - 我提到的一些帖子使用绝对定位来解决这个问题但这似乎对这里没有帮助
参考文献:
我尝试创建虚拟自定义菜单来重现该问题: https:// codepen.io/adiltan/pen/BaJJzOb
我尝试过使用绝对定位,但无法让它工作
What I am trying to do ?
- I have a custom Menu in my app with some options (say 8 options)
- Based on some logic I need to disable some options in the menu (say last 4 options)
- If the user hovers over any disabled option, I need to show a popup explaining why the option was disabled
Issue :
- Since the number of options in the menu are not known before-hand, the menu should be scrollable to accommodate for more options (if required)
- However, the popup upon hovering on a disabled option should overflow the menu boundaries (if required)
- This basically boils down to having
overflow-x: visible
andoverflow-y: auto
which is not possible as per the specs - Some of the posts that I referred use absolute positioning to get around this issue but that does not seem to help here
References :
- How do I enable scrolling only in one direction in CSS?
- Break out of overflow:hidden
- CSS overflow-x: visible; and overflow-y: hidden; causing scrollbar issue
I tried to create my dummy custom menu that reproduces the issue : https://codepen.io/adiltan/pen/BaJJzOb
I have tried using absolute positioning but I could not get it to work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dom方法
getBoundingClientRect
将为您解决此问题。此方法返回所选元素的位置,因此您无需将工具提示嵌套在菜单中。
制作一个包装器,该包装器将包含您的工具提示为
body
的直接子,然后将您从getBoundingClientRect
提供放置。A DOM method
getboundingclientrect
will solve this for you.This method returns selected element's placement so you don't need to nest the tooltip inside your menu.
Make a wrapper that will contain your tooltips as direct child of
body
and give them placement you got fromgetboundingclientrect
.