如何暂时禁用div的悬停?

时间:2019-04-12 18:06:57

标签: javascript html css

我正在制作一个网页。我创建了一个弹出窗口,当我将鼠标悬停在div上时,该弹出窗口将打开,如果没有,则关闭。 我想做一个函数,如果我单击div,弹出窗口将保持打开状态而无需悬停。我该怎么办?预先感谢。

我有此代码:

HTML:

<div class="password-lost-tooltip" onclick="blockTooltip()">Lost password?
        <span class="password-lost-tooltiptext" id="lostpasswordtooltip">LOL</span>
</div>

CSS:

.password-lost-tooltip {
    position: relative;
    display: inline-block;
    position: fixed;
    top: 55px;
    left: 380px;
    font-family: 'Nunito', sans-serif;
    font-size: 15;
    text-decoration: none;
    transition-duration: 0.3s;
    color: grey;
}

.password-lost-tooltip:hover {
    color: black;
    cursor: pointer;
}

.password-lost-tooltip .password-lost-tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: #555;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;

    position: fixed;
    top: 49px;
    left: 610px;
    margin-left: -60px;

    opacity: 0;
    transition: opacity 0.3s;
}

.password-lost-tooltip .password-lost-tooltiptext::after {
    content: "";
    position: fixed;
    transform: rotate(90deg);
    top: 57.5px;
    left: 542.5px;
    margin-left: -5px;
    border-width: 6px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

.password-lost-tooltip:hover .password-lost-tooltiptext {
    visibility: visible;
    opacity: 1;
}

1 个答案:

答案 0 :(得分:1)

您可以使用onClick和onBlur事件在div上设置或删除一个类,然后将该类添加到css中以控制可见性。

请查看以下运行示例:

function blockTooltip () {
  document.querySelector('.password-lost-tooltip').classList.add('showTip');
}

function hideTooltip () {
  document.querySelector('.password-lost-tooltip').classList.remove('showTip');
}
.password-lost-tooltip {
    position: relative;
    display: inline-block;
    position: fixed;
    top: 55px;
    left: 380px;
    font-family: 'Nunito', sans-serif;
    font-size: 15;
    text-decoration: none;
    transition-duration: 0.3s;
    color: grey;
}

.password-lost-tooltip:hover {
    color: black;
    cursor: pointer;
}

.password-lost-tooltip .password-lost-tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: #555;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;

    position: fixed;
    top: 49px;
    left: 610px;
    margin-left: -60px;

    opacity: 0;
    transition: opacity 0.3s;
}

.password-lost-tooltip .password-lost-tooltiptext::after {
    content: "";
    position: fixed;
    transform: rotate(90deg);
    top: 57.5px;
    left: 542.5px;
    margin-left: -5px;
    border-width: 6px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

.password-lost-tooltip:hover .password-lost-tooltiptext {
    visibility: visible;
    opacity: 1;
}

.password-lost-tooltip.showTip .password-lost-tooltiptext {
    visibility: visible;
    opacity: 1;
}
<div class="password-lost-tooltip" tabindex="0"
     onclick="blockTooltip()" onblur="hideTooltip()">Lost password?
        <span class="password-lost-tooltiptext" id="lostpasswordtooltip">LOL</span>
</div>