显示前延迟几秒:无 [英] Delay of a few seconds before display:none
问题描述
我不知道如何实现 display: none
不能立即生效.我需要 #popUpBox 在几秒钟后消失.
I don't know how to achieve that display: none
works not immediately.
I need #popUpBox to disappear after a few seconds.
$(document).bind("mousedown", function(){
$('#popUpBox').css({'display':'none'});
<小时>
jQuery(function($) {
var $txt = '';
$('.selectiontext').bind("mouseup", function(e){
if (window.getSelection){
$txt = window.getSelection();
}
else if (document.getSelection){
$txt = document.getSelection();
}
else if (document.selection){
$txt = document.selection.createRange().text;
}
else return;
if ($txt!=''){
$('#popUpBox').css({'display':'block', 'left':e.pageX+5+'px', 'top':e.pageY+0+'px'});
}
});
$(document).bind("mousedown", function(){
setTimeout(function() {
$('#popUpBox').css({'display':'none'});
}, 2000);
});
不幸的是,当我选择文本时,现在总是 #popUpBox 消失,只有在选择被禁用时我才需要
Unfortunately, when i select text, now always #popUpBox disappears and i need only when selection is disabled
推荐答案
如果您希望函数在 mousedown
事件后几秒钟触发,请尝试此操作:
Try this if you want the function to trigger a few seconds after the mousedown
event:
$(document).bind("mousedown", function(){
setTimeout(function() {
$('#popUpBox').css({'display':'none'});
}, 5000);
});
关于 setTimeout 的阅读这里和此处.基本上,setTimeout()
允许您传递一个函数,然后在执行该函数之前等待一个间隔(以毫秒为单位).
Readup on setTimeout here and here. Basically, setTimeout()
allows you to pass a function and then an interval (in milliseconds) to wait before executing that function.
编辑(用于更新):要仅在没有选择时发生这种情况,请尝试:
Edit (for update): To only have this happen when there is no selection, try:
$(document).bind("mousedown", function(){
if (!window.getSelection) {
setTimeout(function() {
$('#popUpBox').css({'display':'none'});
}, 5000);
}
});
这篇关于显示前延迟几秒:无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!