一段时间后隐藏工具提示
Hide tooltip after some time
我想要一个效果,当有人在元素上悬停时,他会看到一个工具提示几秒钟,然后工具提示消失,即使鼠标仍在元素上。这就是我所拥有的:
<div data-sentence-tooltip="yes" data-tooltip-content: "some content"> some text </div>
$('[data-sentence-tooltip="yes"]').tooltip({title: function(){return $(this).attr('data-tooltip-content')}});
基于其他相关的SO问题,我尝试了以下两个:
setTimeout(function(){$(".tooltip").fadeOut("fast");}, 2000);
和
jQuery.fn.delay = function(time,func){
return this.each(function(){
setTimeout(func,time);
});
};
$('[id^="tooltip"]').delay(2000, function(){
$('[id^="tooltip"]').fadeOut('fast');
}
);
但我想我知道为什么这些都不起作用。可能是因为.tooltip
或id=tooltip*
被动态添加到DOM中。
参考:
- jQuery工具提示,隐藏在..之后。。时间
- jquery工具提示设置超时
根据Zoheiry的回答,我终于做到了:
$('[data-sentence-tooltip="yes"]').on('mouseover', function(){
setTimeout(function(){
$('#enclosingParentDiv').find('.tooltip').fadeOut('fast');
}, 1000);
});
需要注意的几点:
- 我将"mouseover"应用于每个div,因为用户将鼠标悬停在div中的内容上
- 我在
parent div
中搜索.tittip,因为tooltip
被添加为同级
添加类似于so 的函数
$('[data-sentence-tooltip="yes"]').on('mouseover', function(){
// if the tooltip is a child of the element that is being hovered on
// then write this.
setTimeout(function(){
$(this).find('.tooltip').fadeOut();
}, 2000);
// if the tooltip is a sibling of the element being hovered on
// write this
setTimeout(function(){
$(this).parent().find('.tooltip').fadeOut();
}, 2000);
});
这样可以确保您的代码只在悬停在显示该工具提示的项目上后才查找该工具提示。
我通过查看chrome中被检查的元素来了解这一点。不确定这是否完全是万无一失的,是否可以与其他浏览器一起使用
$('input').on('mouseover', function() {
if(!$(this).is(":focus"))
$("#" + $(this).attr("aria-describedby")).delay(800).fadeOut(800);
});
if子句是可选的,这使得该代码只有在焦点不在text字段上时才有效。否则,工具提示将继续显示
相关文章:
- 如何防止高图工具提示弹出隐藏,在图形区域或浏览器窗口上调整大小
- 如果md工具提示中为空,则隐藏工具提示
- 是否可以使用 pointFormatter完全隐藏工具提示
- 防止在工具提示外部单击时隐藏/关闭剑道工具提示
- 避免工具提示在鼠标悬停时隐藏
- 隐藏 x 轴标签,但在图表.js中显示工具提示
- 高图表工具提示溢出处于隐藏状态
- q提示 2 - 鼠标按下以显示工具提示?鼠标向上隐藏
- 工具提示单击以隐藏
- 隐藏的“选择”元素上的工具提示不起作用
- 悬停时隐藏工具提示
- 如何根据选择隐藏谷歌图表工具提示操作
- 如何在鼠标移动时隐藏jQueryUI工具提示
- 工具提示定位和隐藏工具提示在鼠标移出后立即
- Javascript InfoVis Spacetree-动态隐藏/显示工具提示
- 如何在ie10和ie11中隐藏浏览器默认工具提示
- 一段时间后隐藏工具提示
- 三秒后隐藏引导工具提示
- 引导工具提示隐藏选择器/切换元素
- 工具提示隐藏/显示在引导中不起作用