js工具提示,带有延迟的mouseout,没有jQuery
js Tooltip with delayed mouseout without jQuery
我想在悬停div时显示工具提示。鼠标悬停tooltip-div
时也应该显示该工具提示。
添加一个事件侦听器可以完成这项工作,但如果两个div不重叠,当鼠标位于它们之间并且工具提示消失时,mouseout调用就会消失。
现在,我想为mouseout添加一个延迟,当它得到一个新的mouseover时,它会被取消,但我不知道怎么做。
document.getElementById("hoverElem").addEventListener("mouseover", function() {
document.getElementById("displayElem").style.visibility = "visible";
});
document.getElementById("hoverElem").addEventListener("mouseout", function() {
document.getElementById("displayElem").style.visibility = "hidden";
});
#hoverElem {
position: fixed;
height: 100px;
weidth: 200px;
top: 0px;
left: 50%;
background-color: white;
}
#displayElem {
position: fixed;
height: 100px;
weidth: 20px;
top: 150px;
left: 50%;
background-color: yellow;
visibility: hidden;
}
<div id="hoverElem">
A little Div
<div id="displayElem">
Tooltip to show
</div>
</div>
您可以在mouseleave
中初始化计时器,然后在的mouseenter
中清除它类displayElem
document.getElementById("hoverElem").addEventListener("mouseenter", function() {
document.getElementById("displayElem").style.visibility = "visible";
});
var hoverTimer;
document.getElementById("hoverElem").addEventListener("mouseleave", function() {
hoverTimer = setTimeout(function() {
document.getElementById("displayElem").style.visibility = "hidden";
}, 500);
});
document.getElementById("displayElem").addEventListener("mouseenter", function() {
clearTimeout(hoverTimer);
});
document.getElementById("displayElem").addEventListener("mouseleave", function() {
this.style.visibility = "hidden";
});
#hoverElem {
position: fixed;
height: 100px;
weidth: 200px;
top: 0px;
left: 50%;
background-color: white;
}
#displayElem {
position: fixed;
height: 100px;
weidth: 20px;
top: 150px;
left: 50%;
background-color: yellow;
visibility: hidden;
}
<div id="hoverElem">
A little Div
<div id="displayElem">
Tooltip to show
</div>
</div>
您是否考虑过使用纯CSS?
div {
position: fixed;
height: 100px;
width: 200px;
top: 0px;
left: 50%;
background-color: black;
}
div:hover span,
span:hover{
opacity:1;
}
span {
display:block;
opacity:0;
color:orange;
-webkit-transition-delay: .5s;
transition-delay: .5s;
-webkit-transition:opacity 1s ;
transition:opacity 1s ;
position: fixed;
height: 100px;
width: 100px;
top: 150px;
left: 50%;
background-color: yellow;
visibility: visible;
}
<div>
<span>lorem Ipsum</span>
</div>
相关文章:
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 音频控件在mouseover上显示,在mouseout上淡出
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 我的jQuery插件参数没有正确启动,遇到了问题
- 视频HTML没有'无法在Internet Explorer 11上工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- angular.js没有'无法在PhoneGap中处理视图标记
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 有没有一种方法可以防止img get请求使用css或js发生
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- JavaScript中的函数和对象之间没有区别吗?
- JSON.parse没有'不能使用Javascript
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Chrome扩展没有't在重新加载之前考虑期权价值
- 我的AngularJS表达式没有'不起作用
- Mouseover和Mouseout的行为没有达到预期
- js工具提示,带有延迟的mouseout,没有jQuery
- mouseout淡出问题没有应用JavaScript/jQuery