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

问题描述 投票:0回答:1

试图使用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上,主题工具提示就会显示。

css tooltip game-development
1个回答
0
投票

编写选择器#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;
}

请注意,图标包装器上的position:relative;是您的工具提示绝对位置相对于图标包装器。

© www.soinside.com 2019 - 2024. All rights reserved.