显示前的几秒延迟:无

Delay of a few seconds before display:none

本文关键字:几秒 延迟 显示      更新时间:2023-09-26

我不知道如何实现display: none不是立即工作。我需要#popUpBox在几秒钟后消失。

  $(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消失,我只需要当选择被禁用

下面的代码将在2秒后隐藏div

$("#popUpBox").delay(2000).hide();

如果你想要动画,你也可以使用fadeOut方法

$("#popUpBox").delay(2000).fadeOut('fast');

尝试使用setTimeout函数

$(document).bind("mousedown", function(){
    setTimeout(function() {
        $('#popUpBox').css({'display':'none'});
    }, *time you want(int)*);
});

编辑:给你的新问题

在禁用选择时添加if语句。:

$(document).bind("mousedown", function(){ 
    *if statement*
        setTimeout(function() { (...)

因为它是绑定到整个文档的,所以它总是不带任何条件地隐藏这个方框。

如果您希望该函数在mousedown事件发生几秒钟后触发,请尝试以下操作:

$(document).bind("mousedown", function(){
    setTimeout(function() {
        $('#popUpBox').css({'display':'none'});
    }, 5000);
});

在这里和这里读取setTimeout。基本上,setTimeout()允许你传递一个函数,然后在执行该函数之前等待一个间隔(以毫秒为单位)。

编辑(用于更新):要想只在没有选择的情况下进行编辑,请尝试:

$(document).bind("mousedown", function(){
    if (!window.getSelection) {
        setTimeout(function() {
            $('#popUpBox').css({'display':'none'});
        }, 5000);
    }
});