css - 工具提示通过 js 在悬停时显示,但在 css 中不显示

标签 css tooltip game-development

尝试使用 css 切换一些简单跨度的可见性,它似乎不起作用。当用 js 编写时,事件工作正常。有什么问题?

document.getElementById('theme-tooltip').style.display = 'none'
document.getElementById('theme-btn').onmouseover = function(){
    document.getElementById('theme-tooltip').style.display = 'block'
}
document.getElementById('theme-btn').onmouseout = function(){
    document.getElementById('theme-tooltip').style.display = 'none'
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<div id = 'startpanel'></div>
<span id = 'theme-tooltip'>tooltip</span>
<div class="icon-bar">
    <a id='theme-btn'><i class="fas fa-palette"></i></a> 
    <a id='hotkeys-btn'><i class="fas fa-keyboard"></i></a>
    <a id='settings-btn'><i class="fas fa-cog"></i></a>
    <a id='changelog-btn'><i class="fas fa-book"></i></a>
    <a id='discord-btn'><i class="fab fa-discord"></i></a>
</div>
#theme-tooltip{
    color: white;
    position: absolute;
    top: 500px;
    width: 200px;
    height: 30px;
    background-color: #000;
    border-radius: 5px;
    padding: 10px;
    font-size: 14px;
    line-height: 22px;
    text-align: center;
    display: none;
}
#theme-tooltip:after{
    content: ' ';
    width: 0px;
    height: 0px;
    border-top: 10px solid transparent;
    border-left: 10px solid transparent;
    border-bottom:10px solid #000;
    border-right:10px solid transparent;
    position: absolute;
    left: 50%;
    top: -40%;
    margin-left: -10px;
}
#theme-btn:hover #theme-tooltip{
    display: block;
}

只要鼠标悬停在 theme-btn 上,就会显示 theme-tooltip。

最佳答案

您编写选择器的方式 #theme-btn:hover #theme-tooltip 假设您的工具提示位于 #theme-btn 元素内,但事实并非如此。

您是要为每个图标显示相同的工具提示,还是为每个图标显示不同的工具提示?如果您需要为每个图标提供不同的工具提示,我会将工具提示元素放在每个标签之后。您还希望将这些图标-工具提示对中的每对包装在一个容器中,以便您可以正确定位每个工具提示:

<div class="icon-wrapper">
    <a id='theme-btn'><i class="fas fa-palette"></i></a> 
    <span id = 'theme-tooltip'>tooltip</span>
</div>

你的 CSS 看起来像这样:

.icon-wrapper {
    display: inline-block;
    position: relative;
}
#theme-tooltip{
    color: white;
    position: absolute;
    top: 30px;
    left: -100px;
    width: 200px;
    height: 30px;
    background-color: #000;
    border-radius: 5px;
    padding: 10px;
    font-size: 14px;
    line-height: 22px;
    text-align: center; 
    display: none;
}
#theme-tooltip:after{
    content: ' ';
    width: 0px;
    height: 0px;
    border-top: 10px solid transparent;
    border-left: 10px solid transparent;
    border-bottom:10px solid #000;
    border-right:10px solid transparent;
    position: absolute;
    left: 50%;
    top: -40%;
    margin-left: -10px;
}
#theme-btn:hover + #theme-tooltip{
    display: block;
}

请注意,icon-wrapper 上的 position:relative; 是为了使工具提示的绝对位置相对于 icon-wrapper。

关于css - 工具提示通过 js 在悬停时显示,但在 css 中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55739386/

相关文章:

javascript - 为什么在 array.map() 中会多次生成 React-Tooltip?

javascript - 如何为滚动条制作工具提示?

visual-c++ - C++/OpenGL国际象棋游戏程序设计建议

html - 使用一张图片作为菜单

ios - 使用 Swift 2 的 UIWebView 中的工具提示

php - 多个PHP脚本可以同时运行吗?

java - 识别游戏中的线程(也与 TCP 相关)

html - 如何在表页脚/页眉和正文内容之间留一点空间?

html - 在 CKEditor 中禁用切换到纯文本编辑器

html - 这个导航栏在不同的浏览器中输出不同,为什么会这样?