如何使自定义菜单的内容向一个方向溢出?

发布于 2025-01-19 04:18:29 字数 1028 浏览 3 评论 0原文

我想做什么?

  1. 我的应用程序中有一个自定义菜单,其中包含一些选项(例如 8 个选项)
  2. 基于某些逻辑,我需要禁用菜单中的某些选项(例如最后 4 个选项)
  3. 如果用户将鼠标悬停在任何禁用的选项上,我需要显示一个弹出窗口解释为什么该选项被禁用

问题:

  • 由于事先不知道菜单中的选项数量,因此菜单应该可以滚动以容纳更多选项(如果需要)
  • 但是,将鼠标悬停在禁用选项上时的弹出窗口应该溢出菜单边界(如果需要)
  • 这基本上归结为具有 overflow-x:visibleoverflow-y: auto 这根据规范是不可能的
  • 我提到的一些帖子使用绝对定位来解决这个问题但这似乎对这里没有帮助

参考文献:

  1. 怎么办我在 CSS 中仅启用朝一个方向滚动?
  2. 突破溢出:隐藏
  3. CSS溢出-x: 可见的;和溢出-y:隐藏;导致滚动条问题

我尝试创建虚拟自定义菜单来重现该问题: https:// codepen.io/adiltan/pen/BaJJzOb

我尝试过使用绝对定位,但无法让它工作

What I am trying to do ?

  1. I have a custom Menu in my app with some options (say 8 options)
  2. Based on some logic I need to disable some options in the menu (say last 4 options)
  3. 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 and overflow-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 :

  1. How do I enable scrolling only in one direction in CSS?
  2. Break out of overflow:hidden
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一人独醉 2025-01-26 04:18:29

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 from getboundingclientrect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文