简体   繁体   English

CSS 在<span>元素</span>上悬停时显示信息框

[英]CSS Display InfoBox on hovering over <span> Element

I want to implement a functionality in my React app where you hover over a <span> element and then an InfoBox appears over the <span> element.我想在我的 React 应用程序中实现一个功能,在该应用程序中,您在<span>元素上 hover ,然后在<span>元素上出现一个InfoBox

It should look like in Visual Studio code when you hover your cursor over a variable for example.例如,当您 hover 您的 cursor 在变量上时,它应该看起来像在 Visual Studio 代码中。 悬停时的 VSCode

The box should behave as in the following sketch I drew (basically it's the same behavior as in VSCode): The InfoBox is the box that contains This text .该框的行为应与我绘制的以下草图中的行为相同(基本上与 VSCode 中的行为相同): InfoBox 是包含This text的框。 The <span> contains the hello <span>包含你好

画出 infoBox 的行为方式

Unfortunately I'm not an expert in CSS and I don't even know if this is possible with CSS only or if you have to use javascript as well.不幸的是,我不是 CSS 方面的专家,我什至不知道这是否仅适用于 CSS 或者您是否也必须使用 javascript。

Is this what you are looking for?这是你想要的?

Where you hover over some text and text below it appears?你在hover上面的一些文字和下面的文字出现了吗?

 .thisText >.hello { display: none; }.thisText:hover >.hello { display: block; }
 <span class="thisText"> This Text <span class="hello"> Hello </span> </span>

This might help:这可能会有所帮助:

.thisText{
  position:relative;
  top: 20px;
 }

.thisText > .hello{
  display: none;
}

.thisText:hover> .hello {
  display: block;
  position:absolute;
  top:-20px;
}

<span class="thisText">
This Text
  <span class="hello">
    Hello
  </span>
</span>

I was looking into similar things so figured I might share what I ended up with.我正在研究类似的事情,所以想我可能会分享我最终得到的结果。

If you go to Help > Toggle Developer Tools it opens up the Dev Tools just like in Chrome on the side.如果您将 go 切换到Help > Toggle Developer Tools ,它会打开开发工具,就像在侧面的 Chrome 中一样。 Seems like most Electron based apps have this (although Ctrl + Shift + I —I'm on Windows—didn't work for me on VS Code somehow. Needed to open it via mouse through the aforementioned method).似乎大多数基于 Electron 的应用程序都有这个(尽管Ctrl + Shift + I - 我在 Windows 上 - 在 VS Code 上对我不起作用。需要通过上述方法通过鼠标打开它)。

With the Dev Tools you can then view what HTML, JavaScript, and CSS is used to make the displayed UI possible.然后,您可以使用开发工具查看 HTML、JavaScript 和 CSS 用于使显示的 UI 成为可能。 I opened up the console tab at the bottom so that I can type on it while I use my mouse to hover on things, then I entered debugger command in the console to pause the JS execution or something (don't quote me on this).我在底部打开了控制台选项卡,以便我可以在使用鼠标 hover 的同时在上面输入内容,然后我在控制台中输入debugger命令以暂停 JS 执行或其他内容(不要引用我的话) . Point is it pauses the state of everything so you can then keep the tooltip that is created with JavaScript in display.关键是它会暂停所有内容的 state,这样您就可以保留使用 JavaScript 创建的工具提示。

Seems like it's just standard Tooltip positioning with display: block and position: fixed , as well as max-width , top and left set by calculated JavaScript.似乎它只是标准的工具提示定位, display: blockposition: fixed ,以及max-widthtopleft由计算的 JavaScript 设置。 There's also a z-index rule set by a class selector but that's on the stylesheet.还有一个由 class 选择器设置的z-index规则,但这在样式表上。

element.style {
    position: fixed;
    display: block;
    visibility: inherit;
    max-width: 1152px;
    top: 85px;
    left: 147px;
}

Personally I would use position: absolute instead of fixed .我个人会使用position: absolute而不是fixed I would guess they used fixed in Visual Studio Code instead because of the way they calculated the position inside each "split view" (the windows), because the overridden CSS stylesheet did use position: absolute as seen here.我猜他们在 Visual Studio Code 中使用了fixed ,因为他们在每个“拆分视图”(窗口)内计算 position 的方式,因为被覆盖的 CSS 样式表确实使用position: absolute

.monaco-hover {
    cursor: default;
    position: absolute;
    overflow: hidden;
    z-index: 50;
    user-select: text;
    -webkit-user-select: text;
    -ms-user-select: text;
    box-sizing: initial;
    animation: fadein .1s linear;
    line-height: 1.5em;
}

I recognize this doesn't fully answer the question (nothing on how to get or set the dimensions) but I know where I need to go myself from here on, and for those answers I think you can go looking for answers elsewhere depending on if you're writing vanilla JS, or using some framework (in which case there might be an easier way to get, calculate, and set those things).我认识到这并不能完全回答问题(没有关于如何获取或设置尺寸),但我知道从这里开始我需要自己在哪里 go,对于这些答案,我认为你可以 go 根据是否在其他地方寻找答案你正在编写 vanilla JS,或者使用一些框架(在这种情况下,可能有更简单的方法来获取、计算和设置这些东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM